Linux之线程同步

03-23 1912阅读 0评论

目录

Linux之线程同步,Linux之线程同步,词库加载错误:未能找到文件“C:\Users\Administrator\Desktop\火车头9.8破解版\Configuration\Dict_Stopwords.txt”。,使用,我们,访问,第1张
(图片来源网络,侵删)

一、问题引入

二、实现线程同步的方案——条件变量

1、常用接口:

2、使用示例


一、问题引入

我们再次看看上次讲到的多线程抢票的代码:这次我们让一个线程抢完票之后不去做任何事。

Linux之线程同步,Linux之线程同步,词库加载错误:未能找到文件“C:\Users\Administrator\Desktop\火车头9.8破解版\Configuration\Dict_Stopwords.txt”。,使用,我们,访问,第2张
(图片来源网络,侵删)
#include 
#include 
#include 
#include 
#include 
using namespace std;
#define THREAD_NUM 5
class threaddata
{
public:
    threaddata(const string &s, pthread_mutex_t *m)
        : name(s), mtx(m)
    {}
public:
    string name;
    pthread_mutex_t *mtx;
};
int ticket = 100;
void *getticket(void *arg)
{
    threaddata *td = (threaddata *)arg;
    while (true)
    {
        pthread_mutex_lock(td->mtx); 
        if (ticket > 0)              
        {
            usleep(rand() % 10000);
            cout name cond_, td->mtx_); // 在加锁和解锁之间进行等待
        if (ticket > 0)
        {
            usleep(rand() % 10000);
            cout name_ 

免责声明
本网站所收集的部分公开资料来源于AI生成和互联网,转载的目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。
文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。

发表评论

快捷回复: 表情:
评论列表 (暂无评论,1912人围观)

还没有评论,来说两句吧...

目录[+]