http://blog.csdn.net/lzx_bupt/article/details/6915117
上篇说了下互斥量的用法,今儿说一下条件信号量的用法,这两种多线程变量的用法其实取决于情景,需要体会,见文:
- #include <iostream>
- #include <pthread.h>//带头文件
- #include <stdio.h>
- using namespace std;
- #define BOUNDARY 5
- int tasks = 10;
- pthread_mutex_t tasks_mutex;//因为两个线程要修改一个全局变量,需要互斥量;
- pthread_cond_t tasks_cond;//因为两个线程间有条件关系:当tasks>5时,hello2处理它,处理一次减少1;反之hello1处理,直到tasks减为零;
- void* say_hello2(void* args)//hello2处理函数
- {
- pthread_t pid = pthread_self();//打印当前线程id便于跟踪
- cout << "["<< pid << "] hello in thread " << *((int*)args) << endl;
- bool is_signaled = false;//随便一个标志位
- while(1)//无限循环
- {
- pthread_mutex_lock(&tasks_mutex);//要修改了,加锁
- if (tasks > BOUNDARY)//>5才修改
- {
- cout << "["<< pid << "] take task: "<< tasks << " in thread "<< *((int*)args) << endl;
- --tasks;//减少1
- }
- else if (!is_signaled)
- {
- cout << "["<< pid << "] pthread_cond_signal in thread " << *((int*)args) << endl;
- pthread_cond_signal(&tasks_cond);//表明已经不是>5了告诉hello1进程去处理:发送信号;
- is_signaled = true;//表示信号已经发送了
- }
- pthread_mutex_unlock(&tasks_mutex);//操作完解锁
- if (tasks == 0) break;//必须等待tasks全部减为零即hello1完成操作,才跳出循环结束这个进程
- }
- }
- <p>void* say_hello1(void* args)//<=5处理函数
- {
- pthread_t pid = pthread_self();
- cout << "["<< pid << "] hello in thread " << *((int*)args) << endl;</p><p> while(1)
- {
- pthread_mutex_lock(&tasks_mutex);
- if (tasks > BOUNDARY)//如果>5说明需要hello2处理,那么该线程就需要等待
- {
- cout << "["<< pid << "] pthread_cond_wait in thread " << *((int*)args) << endl;
- pthread_cond_wait(&tasks_cond, &tasks_mutex);//等待信号量生效,当hello2发出信号,这里就跳出wait,执行后续;
- }
- else
- {
- cout << "["<< pid << "] take task: "<< tasks << " in thread "<< *((int*)args) << endl;
- --tasks;//<=5就--
- }
- pthread_mutex_unlock(&tasks_mutex);</p><p> if (tasks == 0) break;//为零时退出,同hello2一样
- }</p><p>}</p><p>int main()
- {
- pthread_attr_t attr;//线程创建为joinable的,使得主进程可以和两个线程同步,两个线程完成工作退出后,主进程再退出;
- pthread_attr_init(&attr);
- pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);</p><p> pthread_mutex_init(&tasks_mutex, NULL);//初始化互斥量
- pthread_cond_init(&tasks_cond, NULL);//初始化条件信号量</p><p> pthread_t tid1, tid2;//用于保存两个线程的id号
- int index1 = 1;
- int ret = pthread_create( &tid1, &attr, say_hello1, (void *)&index1);
- if (ret != 0)
- {
- cout << "pthread_create error: error_code=" << ret << endl;
- }</p><p> int index2 = 2;
- ret = pthread_create( &tid2, &attr, say_hello2, (void *)&index2);
- if (ret != 0)
- {
- cout << "pthread_create error: error_code=" << ret << endl;
- }</p><p> </p><p> pthread_join(tid1, NULL);//连接两个线程
- pthread_join(tid2, NULL);</p><p> pthread_attr_destroy(&attr);//该销毁的销毁
- pthread_mutex_destroy(&tasks_mutex);
- pthread_cond_destroy(&tasks_cond);</p><p> //正常退出
- }</p><p> </p>
惯例:g++ -lpthread -o ex_cond ex_cond.cpp
执行结果:
- [cpp@node2 pthread]$ ./ex_cond
- [140009886947088] hello in thread 2
- [140009886947088] take task: 10 in thread 2
- [140009886947088] take task: 9 in thread 2
- [140009886947088] take task: 8 in thread 2
- [140009886947088] take task: 7 in thread 2
- [140009886947088] take task: 6 in thread 2
- [140009886947088] pthread_cond_signal in thread 2
- [140009897436944] hello in thread 1
- [140009897436944] take task: 5 in thread 1
- [140009897436944] take task: 4 in thread 1
- [140009897436944] take task: 3 in thread 1
- [140009897436944] take task: 2 in thread 1
- [140009897436944] take task: 1 in thread 1