多线程间的同步-pthread_cond_wait、pthread_cond_signal的用法

本文介绍了多线程同步中如何使用pthread_cond_wait和pthread_cond_signal进行线程间的通信。通过示例代码展示了如何在不同线程间设置条件并触发信号,实现线程的等待与唤醒。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

pthread_cond_wait(&cond, &mutex);////pthread_cond_wait用于等待某个特定的条件为真

pthread_cond_signal(&cond);//用于通知阻塞的线程某个特定的条件为真了

 

#include<stdio.h>

#include<stdlib.h>

#include<pthread.h>

#include<errno.h>

#include<unistd.h>

 

typedef void* (*fun)(void*);

 

int g_Flag=0;

static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

 

void* thread1(void*);

void* thread2(void*);

void* thread3(void*);

void* thread4(void*);

 

/*

 *  when program is started, a single thread is created, called the initial thread or main thread.

 *  Additional threads are created by pthread_create.

 *  So we just need to create two thread in main().

 */

 

int main(int argc, char** argv)

{

         printf("enter main\n");

         pthread_t tid1, tid2, tid3, tid4;

         int rc1=0, rc2=0, rc3=0, rc4=0;

         int time=2;

 

         rc4 = pthread_create(&tid4, NULL, thread4, NULL);

         if(rc4 != 0)

                   printf("%s: %d\n",__func__, strerror(rc4));

         printf("create thread4...\n");

 

         rc3 = pthread_create(&tid3, NULL, thread3, NULL);

         if(rc3 != 0)

                   printf("%s: %d\n",__func__, strerror(rc3));

         printf("create thread3...\n");

 

         rc2 = pthread_create(&tid2, NULL, thread2, NULL);

         if(rc2 != 0)

                   printf("%s: %d\n",__func__, strerror(rc2));

         printf("create thread2...\n");

        

         rc1 = pthread_create(&tid1, NULL, thread1, &tid2);

         if(rc1 != 0)

                   printf("%s: %d\n",__func__, strerror(rc1));

         printf("create thread1...\n");

 

 

         pthread_mutex_lock(&mutex);

         pthread_cond_wait(&cond, &mutex);////pthread_cond_wait用于等待某个特定的条件为真,pthread_cond_signal用于通知阻塞的线程某个特定的条件为真了

         pthread_mutex_unlock(&mutex);

        //pthread_cond_timedwait(&cond, &mutex,&time);

         printf("leave main\n");

         exit(0);    

}

 

/*

 * thread1() will be execute by thread1, after pthread_create()

 * it will set g_Flag = 1;

 */

void* thread1(void* arg)

{

         printf("enter thread1\n");

         printf("this is thread1, g_Flag: %d, thread id is %u\n",g_Flag, (unsigned int)pthread_self());

         pthread_mutex_lock(&mutex);

         if(g_Flag == 2)

         {        pthread_cond_signal(&cond); //pthread_cond_wait用于等待某个特定的条件为真,pthread_cond_signal用于通知阻塞的线程某个特定的条件为真了

                   printf("pthread_cond_signal....\n");

         }

         g_Flag = 1;

         printf("this is thread1, g_Flag: %d, thread id is %u\n",g_Flag, (unsigned int)pthread_self());

         pthread_mutex_unlock(&mutex);

         printf("wait pthread 2 exit...\n");

         pthread_join(*(pthread_t*)arg, NULL);

         printf("leave thread1\n");

         pthread_cond_signal(&cond);

         pthread_exit(0);

}

 

/*

 * thread2() will be execute by thread2, after pthread_create()

 * it will set g_Flag = 2;

 */

void* thread2(void* arg)

{

         int i;

         printf("enter thread2\n");

         printf("this is thread2, g_Flag: %d, thread id is %u\n",g_Flag, (unsigned int)pthread_self());

         pthread_mutex_lock(&mutex);

         if(g_Flag == 1)

         {

                   pthread_cond_signal(&cond);////pthread_cond_wait用于等待某个特定的条件为真,pthread_cond_signal用于通知阻塞的线程某个特定的条件为真了

                   printf("pthread_cond_signal....\n");

         }

         g_Flag = 2;

         printf("this is thread2, g_Flag: %d, thread id is %u\n",g_Flag, (unsigned int)pthread_self());

         pthread_mutex_unlock(&mutex);

         for(i=0;i<5;i++)

         {

                   printf("this is the thread2 i=%d\n",i);

                   sleep(1);

         }

         printf("leave thread2\n");

         pthread_exit(0);

}

 

void* thread3(void* arg)

{

         int i;

         printf("enter thread3\n");

         printf("this is thread3, g_Flag: %d, thread id is %u\n",g_Flag, (unsigned int)pthread_self());

         pthread_mutex_lock(&mutex);

         printf("thread3 wait for cond...\n");

         pthread_cond_wait(&cond, &mutex);////pthread_cond_wait用于等待某个特定的条件为真,pthread_cond_signal用于通知阻塞的线程某个特定的条件为真了

         printf("thread3 wait for cond success!\n");

         printf("this is thread3, g_Flag: %d, thread id is %u\n",g_Flag, (unsigned int)pthread_self());

         pthread_mutex_unlock(&mutex);

 

 

         pthread_mutex_lock(&mutex);

         printf("thread3 wait for cond....\n");

//      pthread_cond_signal(&cond);                   //在同一个线程中,不能用pthread_cond_signal唤醒自身,必须由其他的线程唤醒?

         pthread_cond_wait(&cond, &mutex);////pthread_cond_wait用于等待某个特定的条件为真,pthread_cond_signal用于通知阻塞的线程某个特定的条件为真了

         printf("thread3 wait for cond success!!\n");

         printf("this is thread3, g_Flag: %d, thread id is %u\n",g_Flag, (unsigned int)pthread_self());

         pthread_cond_signal(&cond);

         pthread_mutex_unlock(&mutex);

         pthread_exit(0);

}

 

void* thread4(void* arg)

{

         int i;

         printf("enter thread4\n");

         printf("this is thread4, g_Flag: %d, thread id is %u\n",g_Flag, (unsigned int)pthread_self());

         pthread_mutex_lock(&mutex);

         printf("thread4 wait for cond...\n");

         pthread_cond_wait(&cond, &mutex);////pthread_cond_wait用于等待某个特定的条件为真,pthread_cond_signal用于通知阻塞的线程某个特定的条件为真了

         printf("thread4 wait for cond success!\n");

         printf("this is thread4, g_Flag: %d, thread id is %u\n",g_Flag, (unsigned int)pthread_self());

         pthread_cond_signal(&cond);

         pthread_mutex_unlock(&mutex);

}

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值