1 线程创建
#include <stdio.h> #include <pthread.h> #include <stdlib.h> void thread(void) { int i; for(i=0;i<3;i++) { printf("this is a pthread\n"); } } int main(void) { pthread_t id; int i,ret; ret = pthread_create(&id,NULL,(void *)thread,NULL); if(ret != 0){ printf("create pthread error\n"); exit(1); } for(i=0;i<3;i++) printf("this is main process\n"); pthread_join(id,NULL); printf("thread over here\n"); return 0; }
上面1的代码,使用pthread_create创建一个线程,在这个线程中,使用默认参数,即函数的第二个参数 pthread_attr_t *attr设为NULL。
属性值包括是否绑定、是否分离、堆栈地址、堆栈大小、优先级。默认的属性是非绑定、非分离、缺省1M的堆栈、与父进程同样级别的优先级。
关于线程的绑定,涉及另一个概念:轻线程。轻线程可以理解为内核线程,它位于用户层和系统层之间。系统对线程资源的分配、对线程的控制是通过轻线程来实现的,一个轻线程可以控制一个或多个线程。默认情况下,启动多少轻线程、哪些轻线程来控制哪些线程是由系统来控制,这种情况即称为非绑定的。绑定状态下,顾名思义,则某个线程固定的绑在一个轻线程之上。被绑定的线程具有较高的响应速度,这是因为cpu时间片的调度室面向轻进程的。绑定的进程可以保证在需要的时候它总有一个轻进程可用。通过设置被绑定的轻进程的优先级和调度级可以使得绑定的线程满足诸如实时反应之类的要求。
设置线程绑定状态的函数为pthread_attr_setscope,第一个参数是指向属性结构的指针,第二个参数是绑定类型:PTHREAD_SCOPE_SYSTEM(绑定类型) PTHREAD_SCOPE_PROCESS(非绑定)
#include <stdio.h> #include <pthread.h> void my_func(void); int main() { pthread_attr_t attr; pthread_t tid; pthread_attr_init(&attr); /*PTHREAD_SCOPE_SYSTEM means bonding thread ,PTHREAD_SCOPE_PROCESS :unbonding pthread ,can be unsed for action needed right time*/ pthread_attr_setscope(&attr,PTHREAD_SCOPE_SYSTEM); pthread_create(&tid,&attr,(void *)my_func,NULL); pthread_join(tid,NULL);//here is blocked printf("thread over\n"); return 0; } void my_func(void) { sleep(4); printf("thread process\n"); //while(1); }
使用pthread_create()的默认参数的话,在主线程(创建线程的线程)中,通过调用pthread_join()等待线程,这里会一直阻塞,直到线程退出为止。例子中,由于线程一直 在while循环中,所以主函数会没法退出
#include <stdio.h> #include <pthread.h> void *my_func(void *arg); int main() { pthread_t tid1,tid2; pthread_create(&tid1,NULL,(void *)my_func,"thread 1"); pthread_create(&tid2,NULL,(void *)my_func,"thread 2"); pthread_join(tid1,NULL);//here is blocked pthread_join(tid2,NULL); printf("thread over\n"); return 0; } void *my_func(void *arg) { printf("thread process\n"); while(arg) { sleep(2); printf("%s\n",(char *)arg); } }
#include <stdio.h> #include <pthread.h> #include <stdlib.h> #include <unistd.h> #include <string.h> pthread_mutex_t mymutex1 = PTHREAD_MUTEX_INITIALIZER; //pthread_mutex_t mymutex2 = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t mycond = PTHREAD_COND_INITIALIZER; void *mythread1(void *param) { printf("begin mythread1.\n"); pthread_mutex_lock(&mymutex1); printf("wait in my thread1.\n"); pthread_cond_wait(&mycond,&mymutex1); pthread_mutex_unlock(&mymutex1); printf("end mythread1.\n"); return NULL; } void *mythread2(void *param) { printf("begin mythread2.\n"); pthread_mutex_lock(&mymutex1); printf("wait in my thread2.\n"); pthread_cond_wait(&mycond,&mymutex1); pthread_mutex_unlock(&mymutex1); printf("end mythread2.\n"); return NULL; } int main(void) { printf("begin main thread\n"); int i; pthread_t tid1,tid2; pthread_create(&tid1,NULL,mythread1,NULL); pthread_create(&tid2,NULL,mythread2,NULL); sleep(5); printf("try to wake up mythread1 and mythread2 in main thread.\n"); #if 1 if(pthread_cond_broadcast(&mycond)){ printf("unlock error\n"); return 1; } #endif void *res; pthread_join(tid1,&res); pthread_join(tid2,&res); printf("end main thread.\n"); return 0; }
http://blog.csdn.net/hudashi/article/details/7709421
原文地址:http://blog.csdn.net/qaqwe/article/details/39183309