Multithreaded Programming examples of thread synchronization and mutual exclusion

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/yuewen2008/article/details/88210010

Function Interface

Thread Creation

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. 

Thread waits

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. 

Thread Suspension

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 function will block the calling thread until the end of the target thread running.

Sample Code

#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;
}

1 to create threads and thread the main function run 2, main thread are calling pthread_join function waits for the end of the corresponding thread execution is suspended at this time the main thread will run until the end of the thread to wait.

operation result

# ./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...

Guess you like

Origin blog.csdn.net/yuewen2008/article/details/88210010
Recommended