Posix Thread 多线程 Pthreads
时间:2006-04-23 来源:oliliango
这取决于exec的implementation。main函数的执行,是在一个主线程里面,而创建,管理其他thread,可以参照以下列举的一些函数:
1.int pthread_create(pthread_t * threadid,const pthread_attr_t * attr,void * (*func)(void *),void * arg);
OR:
typedef void * (func)(void *);
int pthread_create(pthread_t * threadid,const pthread_attr_t * attr,func * fuction,void * arg);
哈哈,可能刚刚开始看的时候有一点点迷惑。仔细想想就可以了。
NOTE:The last two parameters have the type of "void *",this means that we can pass any data we want to the thread function,meanwhile,we can return any data we want to the calling process.
2.pthread_t pthread_self(void);
一般用来在线程内部调用,以在线程内部获得本线程的threadid。
NOTE:试试看,在main函数里面调用pthread_self(),看看结果。
3.int pthread_exit(void * thread_return);
在线程内部,使用pthread_exit((void *)0);可以返回线程的退出状态给调用pthread_join(thread_t threadid,void ** status)的function。
4.int pthread_join(pthread_t threadid,void ** status);
调用此函数的thread将block,直到threadid所代表的那个thread调用pthread_exit(void *).
如果调用时将status设置为NULL,则所等待的那个thread的退出状态被丢弃。
5,int pthread_detach(pthread_t threadid);
如果要将join之后的thread变成非join的,就调用这个。非join的thread终止之后,所有的资源都释放了。即不保留退出状态。
NOTE:如果不显式detach,那么就是join的。状态保留到直到调用了对这个线程的pthread_join为止。
6.int pthread_cancle(pthread_t threadid);
调用此函数将导致threadid所指定的那个线程退出,行为相当于threadid所指定的那个线程调用pthread_exit(PTHREAD_CANCELED).哈哈。
基本上这几个就可以用来编制并行程序了。但是并行程序中的同步,嘿嘿,以后有空再说了。
1.int pthread_create(pthread_t * threadid,const pthread_attr_t * attr,void * (*func)(void *),void * arg);
OR:
typedef void * (func)(void *);
int pthread_create(pthread_t * threadid,const pthread_attr_t * attr,func * fuction,void * arg);
哈哈,可能刚刚开始看的时候有一点点迷惑。仔细想想就可以了。
NOTE:The last two parameters have the type of "void *",this means that we can pass any data we want to the thread function,meanwhile,we can return any data we want to the calling process.
2.pthread_t pthread_self(void);
一般用来在线程内部调用,以在线程内部获得本线程的threadid。
NOTE:试试看,在main函数里面调用pthread_self(),看看结果。
3.int pthread_exit(void * thread_return);
在线程内部,使用pthread_exit((void *)0);可以返回线程的退出状态给调用pthread_join(thread_t threadid,void ** status)的function。
4.int pthread_join(pthread_t threadid,void ** status);
调用此函数的thread将block,直到threadid所代表的那个thread调用pthread_exit(void *).
如果调用时将status设置为NULL,则所等待的那个thread的退出状态被丢弃。
5,int pthread_detach(pthread_t threadid);
如果要将join之后的thread变成非join的,就调用这个。非join的thread终止之后,所有的资源都释放了。即不保留退出状态。
NOTE:如果不显式detach,那么就是join的。状态保留到直到调用了对这个线程的pthread_join为止。
6.int pthread_cancle(pthread_t threadid);
调用此函数将导致threadid所指定的那个线程退出,行为相当于threadid所指定的那个线程调用pthread_exit(PTHREAD_CANCELED).哈哈。
基本上这几个就可以用来编制并行程序了。但是并行程序中的同步,嘿嘿,以后有空再说了。
相关阅读 更多 +