posix thread小程序

posix thread在头文件pthread.h中,基本操作有:

(1)int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void (*start)(void *), void *arg),创建一个线程(线程id,线程属性参数,线程调用的函数start,线程传递参数),成功返回0,失败返回error错误标志。可以由头文件errno.h中的strerror接收错误类型。

(2)void pthread_exit(void *retval)终止当前线程,成功返回0,失败返回error。retval线程结束或取消时返回的指针

(3)int pthread_join(pthread_t thread, void **thread_return)挂起当前线程,等待thread执行完后再执行。thread_return线程取消或者结束时返回的指针。

(4)pthread_t pthread_self(void)返回当前线程。

(5)int pthread_equal(pthread_t thread1, pthread_t thread2)判断两个线程是否相同,如果相同返回非零值,否则返回0。

(6)int pthread_detach(ptread_t thread)从进程中分离出资源来给该线程,当线程结束时收回。调用成功返回0,否则返回非零值。

线程的同步机制:互斥量(mutex),连接/合并(join),条件变量(condition vavriables)

互斥量mutex,通过mutex_lock_count判断当前互斥量是不是应经锁住。

(1)pthread_mutex_t mymutex;定义一个互斥量。静态初始化PTHREAD_MUTEX_INITIALIZER。动态初始化pthread_mutex_init()。静态初始化时头文件pthread.h中定义的一个宏。只能适用于定义是初始化。

(2)int pthread_mutex_lock(pthread_mutex_t *mymutex)加锁,mutex_lock_count++,成功返回0,失败返回错误标志,并阻塞当前线程等待mymutex可用时再执行。

(3)int pthread_mutex_trylock(pthread_mutex_t *mymutex)增加了一个try。

(4)int pthread_mutex_unlock(pthread_mutex_t *mymutex)解锁,mutex_lock_count--,释放资源。


/*
完成一个有趣的游戏(类似抢板凳):主线程中创建线程1,然后线程1再创建2个线程2,3,线程2,3分别为一个计数器,
操作counter(初值为0),线程2每使用一次加3,线程3每用一次加5,如果加到被15整除,那么counter加8,
看哪个线程先加到9999,并计算自己使用了多少次计算器。先到者胜利,并打印出相应信息。
*/
#include <iostream>
#include <pthread.h>
using namespace std;
#define MAXN 999999
struct node{
	int cnt;
	int key;
};
node res1, res2;
pthread_mutex_t mymutex = PTHREAD_MUTEX_INITIALIZER;
int counter;
void *add2(void *arg) {
	while( 1 ) {
		pthread_mutex_lock(&mymutex);
		if( counter < MAXN ) {
			counter += 5;
			if( counter%5 == 0 ) counter += 8;
			res2.cnt++;
			if( counter >= MAXN )
				res2.key = 1;
			cout << "add2 -> " << counter << endl;
		}
		else {
			pthread_mutex_unlock(&mymutex);
			break;
		}
		pthread_mutex_unlock(&mymutex);
	}
}
void *add1(void *arg) {
	while( 1  ) {
		pthread_mutex_lock(&mymutex);
		if( counter < MAXN ) {
			counter += 3;
			res1.cnt++;
			if( counter >= MAXN )
				res1.key = 1;
			cout << "add1 -> " << counter << endl; 
		}
		else {
			pthread_mutex_unlock(&mymutex);
			break;
		}
		pthread_mutex_unlock(&mymutex);
		
	}
}
void *start(void *arg) {
	int status;
	pthread_t thread_2;
	status = pthread_create(&thread_2, NULL, add1, NULL);
	cout << "thread_2 " << status << endl;
	if( status ) {
		cout << "thread_2 error" << endl;
		return NULL;
	}
	pthread_t thread_3;
	status = pthread_create(&thread_3, NULL, add2, NULL);
	cout << "thread_3" << status << endl;
	if( status ) {
		cout << "thread3_error" << endl;
		return NULL;	
	}	
	pthread_join(thread_2, NULL);
	pthread_join(thread_3, NULL);
	return NULL;
}
int main()
{
	res1.cnt = res1.key = 0;
	res2.cnt = res2.key = 0;
	counter = 0;
    	int status;
    	pthread_t thread_1;
    	status = pthread_create(&thread_1,NULL, start,NULL);
	cout << "main " << status << endl;
    	if( !status ) 
	{
		pthread_join(thread_1,NULL);
		cout << "res1: cnt=" << res1.cnt << ", key=" << res1.key << endl;
		cout << "res2: cnt=" << res2.cnt << ", key=" << res2.key << endl;
	}
	else
		cout << "thread_1 error" << endl;
	return 0;
}


你可能感兴趣的:(posix thread小程序)