就好比两个小朋友都只有5毛;两个小朋友都想吃1块钱的棒棒糖;两位小朋友在商量的过程中,都拿不到对方的五毛钱更不用说吃棒棒糖了。
肯定是可以避免死锁的情况出现
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;int pthread_cond_init(pthread_cond_t *restrict cond,const pthread_condattr_t *restrict attr);
参数:
cond:要初始化的条件变量
attr:NULL
int pthread_cond_destroy(pthread_cond_t *cond)
int pthread_cond_wait(pthread_cond_t *restrict cond,pthread_mutex_t *restrict mutex);
参数:
cond:要在这个条件变量上等待
mutex:互斥量(线程互斥篇里的锁)
int pthread_cond_broadcast(pthread_cond_t *cond);
全部线程一起唤醒
int pthread_cond_signal(pthread_cond_t *cond);
线程一个一个按顺序来唤醒
引入同步: 主要是为了解决,访问临界资源合理性问题的
条件变量
当我们申请临界资源前-> 先要做临界资源是否存在的检测->要做检测的本质也是访问临界资源!
条件变量
#include
#include
#include using namespace std;#define THREAD_NUM 4 // 线程个数
typedef void (*funcs)(const std::string &name,pthread_mutex_t *pmtx, pthread_cond_t *pcond); // 函数指针
// pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
// pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;volatile bool quit = false; // 退出条件变量class threadData
{
public:threadData(const string &name, funcs func, pthread_mutex_t *mtx, pthread_cond_t *cond): _name(name), _func(func), _mtx(mtx), _cond(cond){} // 类初始化
public:string _name; // 线程名字funcs _func;pthread_mutex_t *_mtx;pthread_cond_t *_cond;
};void Func1(const std::string &name,pthread_mutex_t *pmtx, pthread_cond_t *pcond)
{while (!quit){pthread_mutex_lock(pmtx); // 申请锁pthread_cond_wait(pcond, pmtx); // 默认该线程在执行的时候,wait代码被执行,当前线程会被立即被阻塞cout << name << "...我正在执行数据扫描任务..." << endl;pthread_mutex_unlock(pmtx); // 解锁}
}
void Func2(const std::string &name,pthread_mutex_t *pmtx, pthread_cond_t *pcond)
{while (!quit){pthread_mutex_lock(pmtx); // 申请锁pthread_cond_wait(pcond, pmtx); // 默认该线程在执行的时候,wait代码被执行,当前线程会被立即被阻塞cout << name << "...我正在执行下载任务..." << endl;pthread_mutex_unlock(pmtx); // 解锁}
}
void Func3(const std::string &name,pthread_mutex_t *pmtx, pthread_cond_t *pcond)
{while (!quit){pthread_mutex_lock(pmtx); // 申请锁pthread_cond_wait(pcond, pmtx); // 默认该线程在执行的时候,wait代码被执行,当前线程会被立即被阻塞cout << name << "...我正在执行数据刷新任务..." << endl;pthread_mutex_unlock(pmtx); // 解锁}
}
void Func4(const std::string &name,pthread_mutex_t *pmtx, pthread_cond_t *pcond)
{while (!quit){pthread_mutex_lock(pmtx); // 申请锁pthread_cond_wait(pcond, pmtx); // 默认该线程在执行的时候,wait代码被执行,当前线程会被立即被阻塞cout << name << "...我正在执行广播任务..." << endl;pthread_mutex_unlock(pmtx); // 解锁}
}
void *Entry(void *args)
{threadData *td = (threadData *)args;td->_func(td->_name, td->_mtx, td->_cond); // 函数回调delete td;return nullptr;
}int main()
{pthread_mutex_t mtx;pthread_cond_t cond;// mtx && cond 初始化pthread_mutex_init(&mtx, nullptr);pthread_cond_init(&cond, nullptr);pthread_t tids[THREAD_NUM]; // 线程idfuncs func_t[THREAD_NUM] = {Func1, Func2, Func3, Func4};for (int i = 0; i < THREAD_NUM; ++i){string name = "thread ";name += to_string(i + 1);threadData *td = new threadData(name, func_t[i], &mtx, &cond);// 创建线程pthread_create(tids + i, nullptr, Entry, td);}sleep(5);// cond---唤醒int cnt = 10;while (cnt){cout << "线程正在唤醒 " << cnt-- << endl;pthread_cond_signal(&cond);sleep(1);cout << "唤醒成功" << endl;}quit = true;pthread_cond_broadcast(&cond); //这一次,让其它线程出来(剩下的线程全部唤醒)for (int i = 0; i < THREAD_NUM; ++i){// 线程等待pthread_join(tids[i], nullptr);cout << "thread " << tids[i] << "...quit" << endl;}cout << THREAD_NUM << "个线程任务结束" << endl;// mtx && cond销毁pthread_mutex_destroy(&mtx);pthread_cond_destroy(&cond);return 0;
}
上一篇:Pandas数据清洗函数大全