C/C++语言实现两个线程交替打印奇偶数

C/C++两个线程交替打印

    • C语言第一种方式
    • C语言第二种方式
    • C++实现的第一种方式

C语言第一种方式

实现思想主要是让两个线程互相唤醒对方来交替打印数字

#include 
#include 
#include 
#include 

int g_num = 1;
pthread_mutex_t mutex;
pthread_cond_t cond1,cond2;

void* thread1(void* arg)
{
	while(1)
	{
		pthread_mutex_lock(&mutex);
        //如果需要交替打印一定范围(例如1-10)内的数字,那么可以加上下面两行代码
        //if(g_num > 10)
           //exit(1);
        printf("Thread1: %d \n",g_num);
        g_num ++;
        pthread_cond_signal(&cond2);
        pthread_cond_wait(&cond1,&mutex);		
		pthread_mutex_unlock(&mutex);
		sleep(1);
	}
	return NULL;
}

void* thread2(void* arg)
{
	while(1)
	{
        //这个sleep(1)加在前面是因为开启线程时有可能是线程2先打印,
        //导致变成thread2输出奇数,threa1输出偶数。为了避免这种情况,可以在延迟下线程2的打印。
        sleep(1);
		pthread_mutex_lock(&mutex);
        printf("Thread2: %d \n",g_num);
		g_num++;
        pthread_cond_signal(&cond1);
        pthread_cond_wait(&cond2,&mutex);
		pthread_mutex_unlock(&mutex);
		
	}
	return NULL;
}
int main()
{
	pthread_t p1,p2;
	
	pthread_mutex_init(&mutex,NULL);
	pthread_cond_init(&cond1,NULL);
	pthread_cond_init(&cond2,NULL);	
	
	pthread_create(&p1,NULL,thread1,NULL);
	pthread_create(&p2,NULL,thread2,NULL);
    pthread_join(p1,NULL);
	pthread_join(p2,NULL);
	
	pthread_mutex_destroy(&mutex);
	pthread_cond_destroy(&cond1);
	pthread_cond_destroy(&cond2);

	return 0;
}

运行结果如下:
C/C++语言实现两个线程交替打印奇偶数_第1张图片

C语言第二种方式

思想主要是引入第三个线程来管理唤醒信号

#include 
#include 
#include 
#include 


int g_num = 0;
pthread_mutex_t mutex;
pthread_cond_t cond1,cond2;

void* thread1(void* arg)
{
	while(1)
	{
		pthread_mutex_lock(&mutex);
		pthread_cond_wait(&cond1,&mutex);
		printf("Thread1: %d \n",g_num);
		pthread_mutex_unlock(&mutex);
		sleep(1);
	}
	return NULL;
}

void* thread2(void* arg)
{
	while(1)
	{
		pthread_mutex_lock(&mutex);
		pthread_cond_wait(&cond2,&mutex);
		printf("Thread2: %d \n",g_num);
		pthread_mutex_unlock(&mutex);
		sleep(1);
	}
	return NULL;
}

void* thread3(void* arg)
{
	while(1)
	{
        //有可能出现线程3都运行了1次了,线程1还没开始,导致不是从1开始打印,为了避免这种情况,所以先让管理线程休眠一会。
        sleep(1);
		pthread_mutex_lock(&mutex);
		++g_num;
		pthread_mutex_unlock(&mutex);
		if((g_num % 2) == 0)
			pthread_cond_signal(&cond2);
		else if((g_num % 2) == 1){
			pthread_cond_signal(&cond1);}
	}
	return NULL;
}

int main()
{
	pthread_t p1,p2,p3;
	
	pthread_mutex_init(&mutex,NULL);
	pthread_cond_init(&cond1,NULL);
	pthread_cond_init(&cond2,NULL);	
	
	pthread_create(&p1,NULL,thread1,NULL);
	pthread_create(&p2,NULL,thread2,NULL);
	pthread_create(&p3,NULL,thread3,NULL);

	pthread_join(p1,NULL);
	pthread_join(p2,NULL);
	pthread_join(p3,NULL);

	pthread_mutex_destroy(&mutex);
	pthread_cond_destroy(&cond1);
	pthread_cond_destroy(&cond2);

	return 0;
}

C++实现的第一种方式

#include 
#include 
#include 
#include 
#include 

using namespace std;

mutex mut;
condition_variable cond1, cond2;
int g_nums = 1;

void thread1() {
	while (1) {
		unique_lock locker(mut);
		cout <<"Thread1:"<< g_nums << endl;
		g_nums++;
		cond2.notify_one();
		cond1.wait(locker);
		locker.unlock();
		Sleep(1000);
	}
}

void thread2() {
	while (1) {
		Sleep(1000);
		unique_lock locker(mut);
		cout << "Thread2:" << g_nums << endl;
		g_nums++;
		cond1.notify_one();
		cond2.wait(locker);
		locker.unlock();	
	}
}

int main() {
	thread t1(thread1);
	thread t2(thread2);
	t1.join();
	t2.join();
	system("pause");
	return 0;
}

C++实现的第二种方式同理也很容易写出

你可能感兴趣的:(多线程)