#include "lib/common.h"
pthread_mutex_t mutex;
pthread_cond_t cond1;
pthread_cond_t cond2;
pthread_cond_t cond3;
int testcount = 0;
void block_queue_init()
{
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&cond1, NULL);
pthread_cond_init(&cond2, NULL);
pthread_cond_init(&cond3, NULL);
}
void thread_run1(void* arg) {
int count = 0;
while (1) {
pthread_mutex_lock(&mutex);
while (testcount != 0)
{
pthread_cond_wait(&cond1, &mutex);
}
testcount++;
printf("1111111111111111\n");
pthread_cond_signal(&cond2);
pthread_mutex_unlock(&mutex);
if (++count >= 3)
{
break;
}
}
}
void thread_run2(void* arg) {
int count = 0;
while (1) {
pthread_mutex_lock(&mutex);
while (testcount != 1)
{
pthread_cond_wait(&cond2, &mutex);
}
testcount++;
printf("2222222222222222\n");
pthread_cond_signal(&cond3);
pthread_mutex_unlock(&mutex);
if (++count >= 3)
{
break;
}
}
}
void thread_run3(void* arg) {
int count = 0;
while (1) {
pthread_mutex_lock(&mutex);
while (testcount != 2)
{
pthread_cond_wait(&cond3, &mutex);
}
printf("3333333333333333\n");
testcount=0;
pthread_cond_signal(&cond1);
pthread_mutex_unlock(&mutex);
if (++count >= 3)
{
break;
}
}
}
int main(int c, char **v) {
block_queue_init();
pthread_t thread_tid1;
pthread_t thread_tid2;
pthread_t thread_tid3;
pthread_create(&thread_tid1, NULL, &thread_run1, NULL);
pthread_create(&thread_tid2, NULL, &thread_run2, NULL);
pthread_create(&thread_tid3, NULL, &thread_run3, NULL);
pthread_join(thread_tid1, NULL);
pthread_join(thread_tid2, NULL);
pthread_join(thread_tid3, NULL);
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond1);
pthread_cond_destroy(&cond2);
pthread_cond_destroy(&cond3);
return 0;
}
#include <iostream>
#include <deque>
#include <thread>
#include <mutex>
#include <condition_variable>
std::deque<int> q;
std::mutex mu;
std::condition_variable cond;
int testcount = 0;
void function_1() {
int count = 3;
while (1) {
std::unique_lock<std::mutex> locker(mu);
while(testcount!=0)
cond.wait(locker); // Unlock mu and wait to be notified
testcount++;
std::cout << "aaaaaaaaaaaaaa: " << testcount << std::endl;
locker.unlock();
cond.notify_all(); // Notify one waiting thread, if there is one.
//std::this_thread::sleep_for(std::chrono::seconds(1));
count--;
if(count<=0)
{
break;
}
}
}
void function_2() {
int count = 3;
while (1) {
std::unique_lock<std::mutex> locker(mu);
while(testcount!=1)
cond.wait(locker); // Unlock mu and wait to be notified
testcount++;
std::cout << "bbbbbbbbbbbbbbbbb: " << testcount << std::endl;
locker.unlock();
cond.notify_all(); // Notify one waiting thread, if there is one.
//std::this_thread::sleep_for(std::chrono::seconds(1));
count--;
if(count<=0)
{
break;
}
}
}
void function_3() {
int count = 3;
while (1) {
std::unique_lock<std::mutex> locker(mu);
while(testcount!=2)
cond.wait(locker); // Unlock mu and wait to be notified
testcount=0;
std::cout << "ccccccccccccccccc: " << testcount << std::endl;
locker.unlock();
cond.notify_all(); // Notify one waiting thread, if there is one.
//std::this_thread::sleep_for(std::chrono::seconds(1));
count--;
if(count<=0)
{
break;
}
}
}
int main() {
std::thread t3(function_3);
std::thread t1(function_1);
std::thread t2(function_2);
t1.join();
t2.join();
t3.join();
return 0;
}
pthread_cond_signal只能通知一个要通知多个可用c++11
std::condition_variable cond; cond.notify_all