多線程編程之線程同步與互斥實例

函數接口

線程創建

pthread_create()
Create a thread
Synopsis:
#include <pthread.h>

int pthread_create( pthread_t* thread,
                    const pthread_attr_t* attr,
                    void* (*start_routine)(void* ),
                    void* arg );
Arguments:
thread
NULL, or a pointer to a pthread_t object where the function can store the thread ID of the new thread. 
attr
A pointer to a pthread_attr_t structure that specifies the attributes of the new thread. Instead of manipulating the members of this structure directly, use pthread_attr_init() and the pthread_attr_set_* functions. For the exceptions, see "QNX Neutrino extensions," below. 
If attr is NULL, the default attributes are used (see pthread_attr_init()). 
start_routine
The routine where the thread begins, with arg as its only argument. If start_routine() returns, there's an implicit call to pthread_exit(), using the return value of start_routine() as the exit status. 
The thread in which main() was invoked behaves differently. When it returns from main(), there's an implicit call to exit(), using the return value of main() as the exit status. 
arg
The argument to pass to start_routine. 

線程等待

pthread_cond_wait()
Wait on condition variable
Synopsis:
#include <pthread.h>

int pthread_cond_wait( pthread_cond_t* cond,
                       pthread_mutex_t* mutex );
Arguments:
cond
A pointer to the pthread_cond_t object that you want the threads to block on. 
mutex
The mutex that you want to unlock. 

線程掛起

pthread_join()
Join thread
Synopsis:
#include <pthread.h>

int pthread_join( pthread_t thread,
                  void** value_ptr );
Arguments:
thread
The target thread whose termination you're waiting for. 
value_ptr
NULL, or a pointer to a location where the function can store the value passed to pthread_exit() by the target thread. 
Library:
libc
Use the -l c option to qcc to link against this library. This library is usually included automatically. 
Description:
The pthread_join() function blocks the calling thread until the target thread thread terminates, unless thread has already terminated. If value_ptr is non-NULL and pthread_join() returns successfully, then the value passed to pthread_exit() by the target thread is placed in value_ptr. If the target thread has been canceled then value_ptr is set to PTHREAD_CANCELED. 

pthread_join函數將阻塞調用線程直至目標線程運行結束。

示例代碼

#include <stdlib.h>
#include <stdio.h>
#include <sys/neutrino.h>

pthread_t thread1_tid;
pthread_t thread2_tid;

pthread_mutex_t mutexTest;
pthread_cond_t  condTest;


void *Thread1_Handler(void *arg)
{
	int i=0;
	while(i<20){
		printf("Thread 1 running...i=%d\n",i);
		sleep(1);
		i++;
		if(i==5)
		{
			pthread_mutex_lock (&mutexTest);
			printf("Thread 1 waiting signal...\n");//當i=5以後線程1掛起等待線程2的信號,之後才被喚醒 繼續執行
			pthread_cond_wait (&condTest, &mutexTest);
			pthread_mutex_unlock (&mutexTest);
		}

	}
	printf("Thread 1 stop\n");//i>20以後Thread1運行結束
}
void *Thread2_Handler(void *arg)
{
	int i=0;
	while(i<20)
	{
		printf("Thread 2 running...i=%d\n",i);
		sleep(1);
		i++;
		if(i == 10)
		{
			printf("Thread 2 send sigal...,i=%d\n",i);//當i=10時給線程1發送信號,喚醒正在掛起的線程1
			pthread_cond_broadcast(&condTest);
		}
	}
	printf("Thread 2 stop\n");//i>20以後Thread2運行結束
}
int main(int argc, char *argv[]) {
	pthread_mutex_init( &mutexTest, NULL);
	pthread_cond_init( &condTest, NULL );

	pthread_create(&thread1_tid, NULL, (void *)Thread1_Handler, NULL);
	pthread_create(&thread2_tid, NULL, (void *)Thread2_Handler, NULL);

	if(pthread_join(thread1_tid, NULL) != 0)//main線程被掛起等待pthread1運行結束
	{
		printf("pthread_join 1 error:\n");
	}
	if(pthread_join(thread2_tid, NULL) != 0)//main線程被掛起等待pthread2運行結束
	{
		printf("pthread_join 2 error:\n");
	}


	for(;;)
	{
		printf("Thread main running...\n");
		sleep(1);
	}
	return EXIT_SUCCESS;
}

main函數運行後創建線程1和線程2,main線程分別調用pthread_join函數等待相應的線程執行結束此時main線程會被掛起 直至等待的線程運行結束。

運行結果

# ./pthreadTest  
Thread 1 running...i=0
Thread 2 running...i=0
Thread 1 running...i=1
Thread 2 running...i=1
Thread 1 running...i=2
Thread 2 running...i=2
Thread 1 running...i=3
Thread 2 running...i=3
Thread 1 running...i=4
Thread 2 running...i=4
Thread 1 waiting signal...
Thread 2 running...i=5
Thread 2 running...i=6
Thread 2 running...i=7
Thread 2 running...i=8
Thread 2 running...i=9
Thread 2 send sigal...,i=10
Thread 2 running...i=10
Thread 1 running...i=5
Thread 2 running...i=11
Thread 1 running...i=6
Thread 2 running...i=12
Thread 1 running...i=7
Thread 2 running...i=13
Thread 1 running...i=8
Thread 2 running...i=14
Thread 1 running...i=9
Thread 2 running...i=15
Thread 1 running...i=10
Thread 2 running...i=16
Thread 1 running...i=11
Thread 2 running...i=17
Thread 1 running...i=12
Thread 2 running...i=18
Thread 1 running...i=13
Thread 2 running...i=19
Thread 1 running...i=14
Thread 2 stop
Thread 1 running...i=15
Thread 1 running...i=16
Thread 1 running...i=17
Thread 1 running...i=18
Thread 1 running...i=19
Thread 1 stop
Thread main running...
Thread main running...
Thread main running...
Thread main running...

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章