1. 程式人生 > >pthread多執行緒的練習

pthread多執行緒的練習

pthread庫進行多執行緒程式設計

pthread庫的函式: 1、pthread_create(),建立執行緒 原型int pthread_create(pthread_t *restrict tidp,const pthread_attr_t *restrict attr,void *(*start_rtn)(void),void *restrict arg); 返回值:0代表建立成功,否則是錯誤號 使用方法: 宣告pthread_t tid,用來標示執行緒id; pthread_create(&tids,NULL,SayHello,NULL); 第一個引數是標識執行緒Id,第二個引數是執行緒屬性,無特殊需要NULL即可,第三個引數是開啟執行緒的函式,需要宣告為void* Fun(void* args),第四個引數是傳入函式的引數,需要強制型別轉換為void*,且可以用結構體傳入多個引數

2、pthread_join(),等待執行緒結束 原型:int pthread_join( pthread_t thread, void **value_ptr); 返回值:0代表成功,否則錯誤號 使用方法: pthread_join將會阻塞,直到相應id的執行緒返回,回收資源;第二個引數用來接收pthread_exit()存放的返回值;如果主程序退出了,直接返回。

3、pthread_exit(),退出當前執行緒 原型:void pthread_exit(void* retval) 使用方法: 呼叫退出當前執行緒,類似程序中exit()退出程序,可以傳遞一個指標,join二級指標接收這個指標

4、pthread_attr_init(),初始化執行緒屬性變數 原型:int pthread_attr_init(pthread_attr_t*attr); 返回值:0成功 使用方法: 宣告pthread_attr_t attr,用attr變數來標識執行緒屬性; pthread_attr_init(&attr),初始化attr變數,然後可以傳給pthread_create()的第二個引數

5、pthread_mutex_init();pthread_mutex_lock(),pthread_mutex_unlock()互斥鎖的初始化與鎖的使用 原型:int pthread_mutex_init(pthread_mutex_t *restrict mutex,const pthread_mutexattr_t *restrict attr);       int pthread_mutex_lock(pthread_mutex_t *mutex)       int pthread_mutex_unlock(pthread_mutex_t *mutex) init函式是以動態方式建立互斥鎖的,引數attr指定了新建互斥鎖的屬性。如果引數attr為NULL,則使用預設的互斥鎖屬性,預設屬性為快速互斥鎖 。 lock,unlock引數就是初始化生成的鎖,不同執行緒訪問共享變數的時候用lock與unlock,其他執行緒呼叫lock就會阻塞,就可以安全的在lock與unlock間訪問資源了。

//pthread庫練習
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
//#include <pthread.h>
using namespace std;

int num=0;
pthread_mutex_t mutex;


void* SayHello(void* args){
	while(1){
	pthread_mutex_lock(&mutex);
	num++;
	cout<<"hello,num="<<num<<" here is "<<*((int*)args)<<endl;
	sleep(*(int*)args);
	pthread_mutex_unlock(&mutex);
	sleep(*(int*)args);
	};
	pthread_exit(NULL);

}

void* SayWorld(void* args){
	pthread_mutex_lock(&mutex);
	num++;
	cout<<"world,num="<<num<<endl;
	int* out=new int;
	*out = num;
	
	pthread_mutex_unlock(&mutex);
	sleep(50);
	pthread_exit((void*)out);
	
}

int main(){
	pthread_mutex_init(&mutex,NULL);
	pthread_t tids[5];
	int ret;
	int *out;
	int a[5]={0,1,2,3,4};
	pthread_create(&tids[0],NULL,SayWorld,NULL);
	for(int i=1;i<5;i++){
		ret = pthread_create(&tids[i],NULL,SayHello,(void*)&a[i]);
		if(0!=ret)	cout<<"pthread created failed"<<endl;
	}
	cout<<"waiting for World"<<endl;
	pthread_join(tids[0],(void**)&out);
	cout<<"waiting ok"<<endl;
	cout<<"out="<<*out<<endl;
	delete out;
	return 0;
}


執行結果
world,num=1
hello,num=2 here is 1
waiting for World
hello,num=3 here is 2
hello,num=4 here is 3
hello,num=5 here is 4
hello,num=6 here is 1
hello,num=7 here is 2
hello,num=8 here is 3
hello,num=9 here is 1
hello,num=10 here is 4
hello,num=11 here is 2
hello,num=12 here is 1
hello,num=13 here is 3
hello,num=14 here is 4
hello,num=15 here is 2
hello,num=16 here is 1
hello,num=17 here is 3
hello,num=18 here is 4
hello,num=19 here is 2
hello,num=20 here is 1
hello,num=21 here is 3
hello,num=22 here is 4
waiting ok
out=1

以上實現了thread的幾個函式的應用。pthread_create建立執行緒,傳進去引數a[i],World函式建立完以後建立其餘Hello,Hello們用互斥鎖的方式實現對共享資源的使用,主程序阻塞在join處等待World的返回,並且用exit與join接收了World的返回值。 出的小問題是一開始傳進去的引數是&i,所以一直輸出here is 5,因為i在外面變成5。