[轉]c++ pthread 多線程簡介

連接:https://blog.csdn.net/u013894427/article/details/83827173ios

pthread 入口函數類型說明
void * func1(void * t)
void* 表示無類型指針函數

void*做爲函數參數,表示函數接收一個指針,無論是什麼類型的指針均可以,可是傳遞以前要強制轉換爲無類型指針。this

基礎流程
pthread_t t1;//聲明一個線程
pthread_create(&t1, NULL, &test, (void *)this);//建立一個線程
pthread_exit(NULL);//退出線程spa

pthread_create().net

函數原型線程

int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg);
含有四個參數,指針

第一個參數表示線程id
第二個參數表示線程參數
第三個是線程的入口函數名字
第四個參數表示線程入口函數的參數名字blog


pthread_exit();進程

函數原型內存

void pthread_exit(void *retval)


寫在線程內部,用於強制退出當前線程

若是線程是joinable,能夠在函數參數裏面傳遞線程的退出信息給主線程

例如

#include <pthread.h>
#include <iostream>

using namespace std;

void *thread1(void *);
int status;
int main(void)
{
void *status_m;
cout << "status_m addr is " << &status_m << endl;
pthread_t t_a;
pthread_create(&t_a,NULL,thread1,NULL);/*建立進程t_a*/
pthread_join(t_a, &status_m);/*等待進程t_a結束*/
cout << "status_m value is " << status_m << endl;
int * re=(int *)status_m;
cout << "the value is " << *re << endl;
return 0;
}
void *thread1(void *junk)
{
status=23333;
cout << "status addr is " << &status << endl;
pthread_exit((void *)&status);
}

能夠打印出

status_m addr is 0x7ffe3cfd6170
status addr is 0x6021b4
status_m value is 0x6021b4
the value is 23333
線程的joinable和detached屬性
屬性的設置方法以下

pthread_attr_t attr;//聲明一個參數
pthread_attr_init(&attr);//對參數進行初始化
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);//設置線程爲可鏈接的
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);//設置線程爲可分離的
pthread_attr_destroy(&attr)//銷燬屬性,防止內存泄漏
屬性的相關操做以下:

pthread_detach()

函數原型

int pthread_detach(pthread_t tid);
1
detached的線程在結束的時候會自動釋放線程所佔用的資源

pthread_join()

函數原型

int pthread_join(pthread_t tid, void **status);

joinable的線程必須用pthread_join()函數來釋放線程所佔用的資源,若是沒有執行這個函數,那麼線程的資源永遠得不到釋放。

互斥鎖 mutex
互斥鎖是爲了多個線程在運行過程當中保持數據同步引入的機制。

基本流程

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;/*初始化互斥鎖*/
pthread_mutex_init(&mutex,NULL);/*動態初始化互斥鎖*/
pthread_mutex_lock(&mutex);//加鎖
pthread_mutex_unlock(&mutex);//解鎖
pthread_mutex_destroy(&mutex);//銷燬互斥鎖
舉個例子

#include <pthread.h>
#include <stdio.h>
#include <unistd.h>

pthread_mutex_t mutex ;
void *print_msg(void *arg){
int i=0;
pthread_mutex_lock(&mutex);
for(i=0;i<15;i++){
printf("output : %d\n",i);
usleep(100);
}
pthread_mutex_unlock(&mutex);
}
int main(int argc,char** argv){
pthread_t id1;
pthread_t id2;
pthread_mutex_init(&mutex,NULL);
pthread_create(&id1,NULL,print_msg,NULL);
pthread_create(&id2,NULL,print_msg,NULL);
pthread_join(id1,NULL);
pthread_join(id2,NULL);
pthread_mutex_destroy(&mutex);
return 1;
}
條件變量
基本流程

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;/*初始化互斥鎖*/
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;/*初始化條件變量*/
pthread_mutex_lock(&mutex);/*鎖住互斥量*/
pthread_cond_signal(&cond);//發送信號量 跟wait函數不在同一個線程中
pthread_cond_wait(&cond,&mutex);//阻塞線程,等待條件變量,同時解鎖互斥量
pthread_mutex_unlock(&mutex);//解鎖互斥量
pthread_mutex_destroy(&mutex);//銷燬互斥鎖
pthread_cond_destroy(&cond);//銷燬條件變量
舉個例子

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;/*初始化互斥鎖*/
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;/*初始化條件變量*/

void *thread1(void *);
void *thread2(void *);

int i=1;

int main(void)
{
pthread_t t_a;
pthread_t t_b;
pthread_create(&t_a,NULL,thread1,(void *)NULL);/*建立進程t_a*/
pthread_create(&t_b,NULL,thread2,(void *)NULL); /*建立進程t_b*/
pthread_join(t_a, NULL);/*等待進程t_a結束*/
pthread_join(t_b, NULL);/*等待進程t_b結束*/
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
exit(0);
}
void *thread1(void *junk)
{
for(i=1;i<=6;i++)
{
printf("thread1: Line: %d, i = %d\n", __LINE__, i);
pthread_mutex_lock(&mutex);/*鎖住互斥量*/
printf("thread1: lock %d\n", __LINE__);
if(i%3==0)
{
printf("thread1:signal 1 %d\n", __LINE__);
pthread_cond_signal(&cond);/*條件改變,發送信號,通知t_b進程*/
printf("thread1:signal 2 %d\n", __LINE__);
printf("%s will sleep 1s in Line: %d \n", __FUNCTION__, __LINE__);
sleep(1);
}
pthread_mutex_unlock(&mutex);/*解鎖互斥量*/
printf("thread1: unlock %d\n", __LINE__);
printf("%s will sleep 1s in Line: %d \n\n", __FUNCTION__, __LINE__);
sleep(1);
}
}


void *thread2(void *junk)
{
while(i<6)
{
printf("thread2: Line: %d, i = %d\n", __LINE__, i);
pthread_mutex_lock(&mutex);
printf("thread2: lock %d\n", __LINE__);
if(i%3!=0)
{
printf("thread2: wait 1 %d\n", __LINE__);
pthread_cond_wait(&cond,&mutex);/*解鎖mutex,並等待cond改變*/
printf("thread2: wait 2 %d\n", __LINE__);
}
pthread_mutex_unlock(&mutex);
printf("thread2: unlock %d\n", __LINE__);
printf("%s will sleep 1s in Line: %d \n\n", __FUNCTION__, __LINE__);
sleep(1);
}
}
結果爲:

thread1: Line: 29, i = 1
thread1: lock 31
thread1: unlock 41
thread1 will sleep 1s in Line: 42

thread2: Line: 52, i = 1
thread2: lock 54
thread2: wait 1 57
thread1: Line: 29, i = 2
thread1: lock 31
thread1: unlock 41
thread1 will sleep 1s in Line: 42

thread1: Line: 29, i = 3
thread1: lock 31
thread1:signal 1 34
thread1:signal 2 36
thread1 will sleep 1s in Line: 37
thread1: unlock 41
thread1 will sleep 1s in Line: 42

thread2: wait 2 59
thread2: unlock 62
thread2 will sleep 1s in Line: 63

thread1: Line: 29, i = 4
thread1: lock 31
thread1: unlock 41
thread1 will sleep 1s in Line: 42

thread2: Line: 52, i = 4
thread2: lock 54
thread2: wait 1 57
thread1: Line: 29, i = 5
thread1: lock 31
thread1: unlock 41
thread1 will sleep 1s in Line: 42

thread1: Line: 29, i = 6
thread1: lock 31
thread1:signal 1 34
thread1:signal 2 36
thread1 will sleep 1s in Line: 37
thread1: unlock 41
thread2: wait 2 59
thread2: unlock 62
thread2 will sleep 1s in Line: 63

thread1 will sleep 1s in Line: 42 注意:thread2 wait 1和thread2 wait 2的位置能夠知道wait函數在執行的時候會阻塞thread2,而且解鎖互斥量

相關文章
相關標籤/搜索