一、Exception类封装
pthread_t --> pthread_self() //线程idtid--> gettid() //线程真实id
__thread只能修饰POD类型POD类型(plain old data),与C兼容的原始数据,例如,结构体和整型等C语言中的类型是 POD 类型,但带有用户定义的构造函数或虚函数的类则不是
__thread string t_obj1(“simba”); // 错误,不能调用对象的构造函数__thread string* t_obj2 = new string; // 错误,初始化只能是编译期常量若不是POD数据类型,但也想作为线程局部存储,可以使用线程特定数据TSD,参见以前的文章。__thread string* t_obj3 = NULL; // 正确
__thread int t_cachedTid = 0; //线程真实pid(tid)的缓存,是减少系统调用::syscall(SYS_gettid),提高获取tid的效率。__thread char t_tidString[32]; // tid 的字符串表示__thread const char* t_threadName = "unknown"; //线程名称const bool sameType = boost::is_same<int, pid_t>::value; //判断类型是否相同BOOST_STATIC_ASSERT(sameType);
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
#include <stdio.h>
#include <time.h> #include <pthread.h> #include <unistd.h> void prepare( void) { printf( "pid = %d prepare ...\n", static_cast< int>(getpid())); } void parent( void) { printf( "pid = %d parent ...\n", static_cast< int>(getpid())); } void child( void) { printf( "pid = %d child ...\n", static_cast< int>(getpid())); } int main( void) { printf( "pid = %d Entering main ...\n", static_cast< int>(getpid())); pthread_atfork(prepare, parent, child); fork(); printf( "pid = %d Exiting main ...\n", static_cast< int>(getpid())); return 0; } |
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
#include <stdio.h> #include <time.h> #include <pthread.h> #include <unistd.h> pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; void *doit( void *arg) { printf( "pid = %d begin doit ...\n", static_cast< int>(getpid())); pthread_mutex_lock(&mutex); struct timespec ts = { 2, 0}; nanosleep(&ts, NULL); pthread_mutex_unlock(&mutex); printf( "pid = %d end doit ...\n", static_cast< int>(getpid())); return NULL; } int main( void) { printf( "pid = %d Entering main ...\n", static_cast< int>(getpid())); pthread_t tid; pthread_create(&tid, NULL, doit, NULL); struct timespec ts = { 1, 0}; nanosleep(&ts, NULL); if (fork() == 0) { doit( NULL); } pthread_join(tid, NULL); printf( "pid = %d Exiting main ...\n", static_cast< int>(getpid())); return 0; } |
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
#include <stdio.h>
#include <time.h> #include <pthread.h> #include <unistd.h> pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; void *doit( void *arg) { printf( "pid = %d begin doit ...\n", static_cast< int>(getpid())); pthread_mutex_lock(&mutex); struct timespec ts = { 2, 0}; nanosleep(&ts, NULL); pthread_mutex_unlock(&mutex); printf( "pid = %d end doit ...\n", static_cast< int>(getpid())); return NULL; } void prepare( void) { pthread_mutex_unlock(&mutex); } void parent( void) { pthread_mutex_lock(&mutex); } int main( void) { pthread_atfork(prepare, parent, NULL); printf( "pid = %d Entering main ...\n", static_cast< int>(getpid())); pthread_t tid; pthread_create(&tid, NULL, doit, NULL); struct timespec ts = { 1, 0}; nanosleep(&ts, NULL); if (fork() == 0) { doit( NULL); } pthread_join(tid, NULL); printf( "pid = %d Exiting main ...\n", static_cast< int>(getpid())); return 0; } |