keep_simple

导航

设置线程属性

 1 #include <pthread.h>
 2 #include <unistd.h>
 3 #include <cstdio>
 4 #include <cstdlib>
 5 #include <iostream>
 6 
 7 void* threadRun(void  * arg)
 8 {
 9     sleep(1);
10 
11     return 0;
12 }
13 
14 int main()
15 {
16     char szLog[256] = {0};
17     int const threadNum_ = 10;
18     pthread_t * writeThreadID = new pthread_t[threadNum_];
19     for (int nOffset=0; nOffset<threadNum_; ++nOffset)
20     {
21         size_t stackSize = 1024*20;
22         pthread_attr_t attr;
23         if(0 != pthread_attr_init(&attr))
24             std::cout<<"attr init error"<<std::endl;
25         if(0 != pthread_attr_setstacksize(&attr, stackSize))
26             std::cout<<"attr set error"<<std::endl;
27         if(0 != pthread_create(writeThreadID+nOffset, &attr, threadRun, NULL))
28         {
29             snprintf(szLog, sizeof(szLog)-1,"***writeThread:%d, create failed!\n", nOffset+1); 
30             std::cout << szLog << std::endl;
31         }
32 
33          if(0 != pthread_attr_destroy(&attr))
34             std::cout<<"attr destroy error"<<std::endl;
35 
36     }
37     for(int i=0; i<threadNum_;i++)
38     {
39         long utime =0;
40         if(pthread_join(writeThreadID[i],(void**) &utime) == 0)
41             std::cout << utime <<std::endl;
42     }
43 
44     sleep(1000);
45     return 0;
46 }

 

posted on 2013-01-11 10:04  keep_simple  阅读(354)  评论(0)    收藏  举报