多线程间的同步-pthread_cond_wait、pthread_cond_signal的用法
来源:互联网 发布:java web应用监控 编辑:程序博客网 时间:2024/05/22 11:47
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);
}
- 多线程间的同步-pthread_cond_wait、pthread_cond_signal的用法
- 多线程中pthread_cond_signal和pthread_cond_wait的使用
- pthread_cond_signal和pthread_cond_wait的标准用法
- pthread_cond_wait()和pthread_cond_signal() 的理解
- 多线程-pthread_cond_wait/pthread_cond_signal
- pthread_cond_wait()函数的理解和pthread_cond_signal函数
- C语言pthread_cond_wait与pthread_cond_signal的使用
- pthread_cond_signal()和pthread_cond_wait()的典型使用方法
- pthread_cond_wait和pthread_cond_signal 用法详解
- 条件锁(条件Mutex)pthread_cond_wait、pthread_cond_signal、pthread_cond_broadcast的使用
- 关于 pthread_cond_wait 和 pthread_cond_signal , signal 无效的问题
- pthread_cond_signal只能唤醒已经处于pthread_cond_wait的线程
- 条件锁(条件Mutex)pthread_cond_wait、pthread_cond_signal、pthread_cond_broadcast的使用
- 多线程机制(条件变量):pthread_cond_wait && pthread_cond_signal
- pthread_cond_signal pthread_cond_wait
- pthread_cond_wait()的用法分析
- pthread_cond_wait多线程同步
- 线程同步之条件变量:pthread_cond_signal和pthread_cond_wait
- MethodSwizzle
- mac下svn下 “.a”(静态库)文件无法上传
- Myeclipse代码提示快捷键
- s3c6410的时钟初始化
- mybatis3.x 插入完整日期
- 多线程间的同步-pthread_cond_wait、pthread_cond_signal的用法
- java网络编程之简单客户端服务器
- 清除MySQL历史bin-log日志文件
- 如何成为一个性格沉稳的人
- fstream的open方法使用——错误笔记
- RabbitMQ消息确认机制
- 关于"[MYSQL ERROR(2006):MySQL server has gone away]"的解决方法
- 使用Zend Studio开发PHP项目
- span的width无效解决办法