- 论坛徽章:
- 0
|
先看一段非常“提神的代码“
9 using namespace std;
10 void * fun(void *m)
11 {
12 pthread_attr_t attr;
13 if(pthread_attr_init(&attr) != 0)
14 {
15 cout<<"init error"<<endl;
16 }
17
18 size_t stack;
19 pthread_attr_getstacksize(&attr, &stack);为啥我这输出还是系统默认的线程栈大小就是ulimit -s得到的那个值,困惑
20 cout<<stack<<endl;
21 if(pthread_attr_destroy(&attr) != 0)
22 {
23 cout<<"destory error"<<endl;
24 }
25 return NULL;
26 }
27
28 int main(int argc, char *argv[])
29 {
30 struct rlimit rt;
31 rt.rlim_cur = 1000000;
32 setrlimit(RLIMIT_STACK, &rt);
33 pthread_attr_t attr;
34 if(pthread_attr_init(&attr) != 0)
35 {
36 cout<<"init error"<<endl;
37 }
38 pthread_t thid;
39 size_t s = 70000;
40 if(pthread_attr_setstacksize(&attr, s)!= 0)cout<<"set stack size error"<<endl;;设置一个新的线程栈大小
41 s = 0;
42 pthread_attr_getstacksize(&attr, &s);
43 cout<<"s:"<<s<<endl;
44 int ret;
45 if((ret = pthread_create(&thid, &attr, fun, NULL))!= 0)指定了大小
46 {
47 cout<<"ret:"<<ret<<endl;
48 }
49 sleep(10);
50 getrlimit(RLIMIT_STACK, &rt);
51 cout<<rt.rlim_cur<<" "<<rt.rlim_max<<endl;
52 return 0;
53 }
这样的代码提神吧 |
|