首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

创造线程

2012-09-05 
创建线程pthread_create:创建新的控制流pthread_exit:从现有的控制流中退出pthread_join:从控制流中得到退

创建线程
pthread_create:创建新的控制流
pthread_exit:从现有的控制流中退出
pthread_join:从控制流中得到退出状态
pthread_cleanup_push:注册在退出控制流时调用的函数
pthread_self:获取控制流的ID
pthread_cancel:请求控制流的非正常退出


#include"apue.h"//#include<stdio.h>#include<pthread.h>pthread_t ntid;void printids(const char* s){    pid_t   pid;    pthread_t   tid;    pid = getpid();    tid = pthread_self();    printf("%s pid %u tid %u (0x%x)\n",s,(unsigned int)pid,(unsigned int)tid,(unsigned int)tid);}void * thr_fn(void * arg){    printids("new thread:");    return ((void *)0);}void* testCallBack(void *arg){    printf("this is my thread create");}int main(){    int err;    err = pthread_create(&ntid,NULL,thr_fn,NULL);    if(err!=0)        err_quit("can't create thread: %s\n",strerror(err));    printids("main thread:");    sleep(1);//这时就会调用thr_fn    exit(0);}main thread: pid 2862 tid 3078543056 (0xb77ed6d0)new thread: pid 2862 tid 3078536048 (0xb77ebb70)

热点排行