線程概念:共享進程地址空間的多任務結構函數
建立線程的相關函數:
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 <pthread.h>
2. int pthread_join(pthread_t thread, void **retval);
參數1:等待退出的線程ID
參數2:線程的結束信息,一般爲NULL,不爲NULL時注意參數爲void,
須要與pthread_exit配合使用。
返回值:0 成功,-1 失敗
#include <pthread.h>post
線程互斥(強調共享資源和資源的完整性):
1.int pthread_mutex_init(pthread_mutex_t *mutex,
pthread_mutexattr_t *attr)
功能:初始化鎖
參數1:初始化的鎖
參數2:鎖屬性,使用時一般爲空,使用默認屬性
返回值:0 成功,-1 失敗
#include <pthread.h>
2. int pthread_mutex_lock(pthread_mutex_t *mutex)
功能:加鎖
參數1:鎖
返回值:0 成功, -1 失敗
#include <pthread.h>
3.int pthread_mutex_unlock(pthread_mutex_t *mutex)
功能:解鎖
參數1:鎖
返回值:0 成功, -1 失敗
#include <pthread.h>
線程同步(強調線程順序):
1.int sem_init(sem_t *sem, int pshared, unsigned int value);
功能:初始化信號燈
參數1:被初始化的信號燈
參數2:使用範圍,0線程範圍內使用, 1進程範圍內使用
參數3:信號燈持有資源個數
返回值:0 成功, -1失敗
#include <semaphore.h>
2.int sem_wait(sem_t *sem)
功能:申請資源,申請成功後信號燈的資源個數減1,當資源個數爲0時阻塞
參數1:信號燈指針
返回值:0 成功, -1失敗
#include <semaphore.h>
3.int sem_post(sem_t *sem)
功能:釋放資源,釋放成功後信號燈的資源個數加1,釋放資源後喚醒等待的線程
參數1:信號燈指針
返回值:0 成功, -1失敗
#include <semaphore.h>spa
1 /*線程建立*/ 2 #include <stdio.h> 3 #include <pthread.h> 4 5 void *ThreadFunc(void *arg) 6 { 7 printf("hello"); 8 pthread_exit("thread eixt"); 9 } 10 11 int main () 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 <stdio.h> 3 #include <pthread.h> 4 #include <semaphore.h> 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 int main() 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 <stdio.h> 3 #include <unistd.h> 4 #include <pthread.h> 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 int main () 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 }