线程概念:共享进程地址空间的多任务结构
创建线程的相关函数:
1. int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg);
参数1 :线程ID指针
参数2 :线程属性,使用时通常为NULL,使用默认属性
参数3 :线程函数指针
参数4 :线程函数入参
返回值:0 成功,-1 失败
#include
2. int pthread_join(pthread_t thread, void **retval);
参数1:等待退出的线程ID
参数2:线程的结束信息,通常为NULL,不为NULL时注意参数为void,
需要与pthread_exit配合使用。
返回值:0 成功,-1 失败
#include
线程互斥(强调共享资源和资源的完整性):
1.int pthread_mutex_init(pthread_mutex_t *mutex,
pthread_mutexattr_t *attr)
功能:初始化锁
参数1:初始化的锁
参数2:锁属性,使用时通常为空,使用默认属性
返回值:0 成功,-1 失败
#include
2. int pthread_mutex_lock(pthread_mutex_t *mutex)
功能:加锁
参数1:锁
返回值:0 成功, -1 失败
#include
3.int pthread_mutex_unlock(pthread_mutex_t *mutex)
功能:解锁
参数1:锁
返回值:0 成功, -1 失败
#include
线程同步(强调线程顺序):
1.int sem_init(sem_t *sem, int pshared, unsigned int value);
功能:初始化信号灯
参数1:被初始化的信号灯
参数2:使用范围,0线程范围内使用, 1进程范围内使用
参数3:信号灯持有资源个数
返回值:0 成功, -1失败
#include
2.int sem_wait(sem_t *sem)
功能:申请资源,申请成功后信号灯的资源个数减1,当资源个数为0时阻塞
参数1:信号灯指针
返回值:0 成功, -1失败
#include
3.int sem_post(sem_t *sem)
功能:释放资源,释放成功后信号灯的资源个数加1,释放资源后唤醒等待的线程
参数1:信号灯指针
返回值:0 成功, -1失败
#include
1 /*线程创建*/
2 #include
3 #include
4
5 void *ThreadFunc(void *arg)6 {7 printf("hello");8 pthread_exit("thread eixt");9 }10
11 intmain ()12 {13 pthread_t tID=0;14 if(0 != pthread_create(&tID,NULL,ThreadFunc,NULL))15 {16 printf("creat error\r\n");17 }18 //sleep(1);
19 char *pMsg=NULL;20 pthread_join(tID,(void **)&pMsg);21 printf("%s\r\n",pMsg);22 return 0;23 }
1 /*线程同步*/
2 #include
3 #include
4 #include
5 sem_t g_sem1;6 sem_t g_sem2;7 void *Func1(void *arg)8 {9 int i = 10;10 while(i--)11 {12 sem_wait(&g_sem1);13 printf("hello\r\n");14 sleep(1);15 sem_post(&g_sem2);16 }17 }18 void *Func2(void *arg)19 {20 int i = 10;21 while(i--)22 {23 sem_wait(&g_sem2);24 printf("world\r\n");25 sleep(1);26 sem_post(&g_sem1);27 }28 }29
30 intmain()31 {32 pthread_t tID1 = 0;33 pthread_t tID2 = 0;34
35 if (0 != sem_init(&g_sem1, 0, 1) || 0 != sem_init(&g_sem2, 0, 0))36 {37 return -1;38 }39
40 if (0 != pthread_create(&tID1, NULL, Func1, NULL))41 {42 return -1;43 }44 if (0 != pthread_create(&tID2, NULL, Func2, NULL))45 {46 return -1;47 }48 pthread_join(tID1, NULL);49 pthread_join(tID2, NULL);50 return 0;51 }
1 /*互斥锁*/
2 #include
3 #include
4 #include
5
6 #define ARR_SIZE 10
7
8 pthread_mutex_t g_mutex;9
10 void *Func(void *arg)11 {12 if(NULL==arg)13 {14 return (void *)NULL;15 }16 pthread_mutex_lock(&g_mutex);17 char *pTmp=(char *)arg;18 static char s_arr[ARR_SIZE+2] = {0};19 int 1=0;20 for (;i<10;i++)21 {22 s_arr[i]=pTmp[i];23 usleep(2000);24 }25 pthread_mutex_unlock(&g_mutex);26 printf("%s\r\n",s_arr);27 }28
29 intmain ()30 {31 char arr1[ARR_SIZE]={‘a‘,‘b‘,‘c‘,‘d‘,‘e‘,‘f‘,‘g‘,‘h‘,‘i‘,‘j‘};32 char arr2[ARR_SIZE]={‘1‘,‘2‘,‘3‘,‘4‘,‘5‘,‘6‘,‘7‘,‘8‘,‘9‘,‘0‘};33 pthread_t tID1=0;34 pthread_t tID2=0;35
36 pthread_mutex_init(&g_mutex,NULL);37
38
39 if(0!=pthread_create(&tID1,NULL,Func,(void *)arr1))40 {41 return -1;42 }43 if(0!=pthread_create(&tID2,NULL,Func,(void *)arr12))44 {45 return -1;46 }47 pthread_join(tID1,NULL);48 pthread_join(tID2,NULL);49 }