class CPthread
{
public:
static int CreatePthread(void* func, void* arg);
};
int CPthread::CreatePthread(void* func, void* arg)
{
pthread_t pid;
pthread_attr_t attr;
if(pthread_attr_init(&attr) != 0)
{
return -1;
}
if(pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) != 0)
{
return -1;
}
if(pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM) != 0)
{
return -1;
}
if(pthread_create(&pid, &attr, (void*(*)(void*))func, arg) != 0)
{
return -1;
}
pthread_attr_destroy(&attr);
return 0;
}
CPthread::CreatePthread((void*)ClientThread, (void*)arg);
或者:
int CPthread::CreatePthread(void *(*func)(void *), void* arg)
{
pthread_t pid;
pthread_attr_t attr;
if(pthread_attr_init(&attr) != 0)
{
return -1;
}
if(pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) != 0)
{
return -1;
}
if(pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM) != 0)
{
return -1;
}
if(pthread_create(&pid, &attr, func, arg) != 0)
{
return -1;
}
pthread_attr_destroy(&attr);
return 0;
}
CPthread::CreatePthread(ClientThread, (void*)arg);
=========================================
测试程序:
#include <iostream>
#include <string>
#include <cstdio>
#include <pthread.h>
#include <unistd.h>
volatile static int cnt = 0;
int lib_thread_create(pthread_t *threadid, void *pfunction, void *arg, int flag);
int lib_thread_create(pthread_t *threadid, void *pfunction, void *arg, int flag)
{
int ret = 0;
pthread_attr_t attr;
pthread_attr_init(&attr);
if(0 == flag)
{
pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
}
else if(1 == flag)
{
ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
if(ret < 0)
{
pthread_attr_destroy(&attr);
return ret;
}
}
pthread_create(threadid, &attr, (void*(*)(void*))pfunction, arg);
pthread_attr_destroy(&attr);
return ret;
}
void getplus(void * arg)
{
//std::cout << pthread_self() << std::endl;
int i=0;
do{
++i;
cnt = cnt + 1;
std::cout<< pthread_self()<<":"<<cnt << std::endl;
sleep(2);
}while(i < 50);
}
void getminus(void* arg)
{
int i=0;
do{
++i;
cnt = cnt - 1;
std::cout<< pthread_self()<<":"<<cnt << std::endl;
sleep(1);
}while(i < 100);
}
int main(int argc, char *argv[])
{
pthread_t pid[50] = {0};
for(int i=0; i<50;i++)
{
lib_thread_create(&pid[i], (void*)getplus, NULL, 1);
// if( i%2)
// {
// lib_thread_create(&pid[i], (void*)getplus, NULL, 1);
// }
// else
// {
// lib_thread_create(&pid[i], (void*)getminus, NULL, 1);
// }
}
getchar();
return 0;
}
带volatile属性时:
打印结果:
2687417248:2481
2666437536:2482
2991627168:2483
2970643360:2484
2949663648:2485
2939173792:2486
2981137312:2487
2960153504:2488
2928683936:2489
2918194080:2490
2907704224:2491
2655947680:2492
2645457824:2493
2634967968:2494
2624478112:2495
2613988256:2496
2603498400:2497
2593008544:2498
2582518688:2499
2572028832:2500
不带volatile属性的打印结果:
2666408864:91
2655919008:92
2645429152:93
2634939296:94
2624449440:95
2613959584:96
2603469728:97
2592979872:98
2582490016:99
2572000160:100
2928655264:66
2918165408:67
2907675552:68
2897185696:69
2886695840:70
2876205984:71
2865716128:72
2855226272:73
2844736416:74
Volatile变量修饰符如果使用恰当的话,它比synchronized的使用和执行成本会更低,因为它不会引起线程上下文的切换和调度。
它变量的操作具有原子性