線程的條件變量實例

情景1:

Jack開著一輛出租車來到一個網站停車。看見沒人就走了。過段時間。Susan來到網站準備乘車。可是沒有來,于是就等著。過了一會Mike開著車來到了這個網站,Sunsan就上了Mike的車走了。如圖所看到的:


程序實現該情景:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>pthread_cond_t taxicond = PTHREAD_COND_INITIALIZER;
pthread_mutex_t taximutex = PTHREAD_MUTEX_INITIALIZER;void *traveler_arrive(void *name)
{char *p = (char *)name;printf ("Travelr: %s need a taxi now!\n", p);pthread_mutex_lock(&taximutex);pthread_cond_wait(&taxicond, &taximutex);pthread_mutex_unlock(&taximutex);printf ("traveler: %s now got a taxi!\n", p);pthread_exit(NULL);
}void *taxi_arrive(void *name)
{char *p = (char *)name;printf ("Taxi: %s arrives.\n", p);pthread_cond_signal(&taxicond);pthread_exit(NULL);
}int main (int argc, char **argv)
{char *name;pthread_t thread;pthread_attr_t threadattr;pthread_attr_init(&threadattr);name = "Jack";pthread_create(&thread, &threadattr, taxi_arrive, name);sleep(1);name = "Susan";pthread_create(&thread, &threadattr, traveler_arrive, name);sleep(1);name = "Mike";pthread_create(&thread, &threadattr, taxi_arrive, name);sleep(1);return 0;
} /* ----- End of main() ----- */

情景2:

Jack開著一輛出租車來到一個網站停車,看見沒人就等著。過段時間,Susan來到網站準備乘車看見了Jack的出租車,于是就上去了。過了一會Mike開著車來到了這個網站,看見沒人救等著。

如圖所看到的:


程序實現該情景:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>int travelercount = 0;
pthread_cond_t taxicond = PTHREAD_COND_INITIALIZER;
pthread_mutex_t taximutex = PTHREAD_MUTEX_INITIALIZER;void *traveler_arrive(void *name)
{char *p = (char *)name;pthread_mutex_lock(&taximutex);printf ("traveler: %s need a taxi now!\n", p);travelercount++;pthread_cond_wait(&taxicond, &taximutex);pthread_mutex_unlock(&taximutex);printf ("traveler: %s now got a taxi!\n", p);pthread_exit(NULL);
}void *taxi_arrive(void *name)
{char *p = (char *)name;printf ("Taxi: %s arrives.\n", p);for(;;){if(travelercount){pthread_cond_signal(&taxicond);travelercount--;break;}}pthread_exit(NULL);
}int main (int argc, char **argv)
{char *name;pthread_t thread;pthread_attr_t threadattr;pthread_attr_init(&threadattr);name = "Jack";pthread_create(&thread, &threadattr, taxi_arrive, name);sleep(1);name = "Susan";pthread_create(&thread, &threadattr, traveler_arrive, name);sleep(3);name = "Mike";pthread_create(&thread, &threadattr, taxi_arrive, name);sleep(4);return 0;
} /* ----- End of main() ----- */

author: fulinux

E-mail: fulinux@sina.com

blog: blog.csdn.net/fulinus








轉載于:https://www.cnblogs.com/clnchanpin/p/6910099.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/456393.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/456393.shtml
英文地址,請注明出處:http://en.pswp.cn/news/456393.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Flask 模板 之 變量和過濾器

1.模板 在前面的示例中&#xff0c;視圖函數的主要作用是生成請求的響應&#xff0c;這是最簡單的請求。實際上&#xff0c;視圖函數有兩個作用&#xff1a;處理業務邏輯和返回響應內容。在大型應用中&#xff0c;把業務邏輯和表現內容放在一起&#xff0c;會增加代碼的復雜度…

架設證書服務器 及 讓IIS啟用HTTPS服務

http://blog.csdn.net/dier4836/article/details/7719532轉載于:https://www.cnblogs.com/kunlunmountain/p/6428838.html

淺談互聯網寒冬與經濟形勢

文章目錄前言&#xff1a;閑扯總結&#xff1a;前言&#xff1a; 作為剛進入職場一年多的小白來說&#xff0c;剛準備“猥瑣發育漲經驗”&#xff0c;卻碰到這股互聯網寒冬&#xff0c;也感到鴨梨山大。也許有人認為我在販賣焦慮&#xff0c;但是現實真的很嚴峻。接下說下過去…

jenkins 配置

build periodically 周期進行項目構建poll scm 定時檢查遠嗎變更&#xff0c;如果有更新就checkout最新code下來&#xff0c;然后執行構建動作日程表&#xff1a;H 9 *** : 每天9點固定構建一次H/30 8-17 *** : 每天的8-17點每隔三十分鐘檢查一次轉載于:https://…

Java集合---LinkedList源碼解析

一、源碼解析1、 LinkedList類定義2、LinkedList數據結構原理3、私有屬性4、構造方法5、元素添加add()及原理6、刪除數據remove()7、數據獲取get()8、數據復制clone()與toArray()9、遍歷數據&#xff1a;Iterator()二、ListItr 一、源碼解析 1、 LinkedList類定義。 public cla…

Mysql面試熱身題集總結

文章目錄前言1、熱身題實踐其他前言 一直有個想法&#xff1a;把面試需要的知識點全都總結一下&#xff0c;包括數據庫&#xff0c;語言&#xff0c;算法&#xff0c;數據結構等知識&#xff0c;形成一個面試總結筆記&#xff0c;這樣以后面試的時候只看這些文章回顧下就行了。…

Flask中的 url_for() 函數

最近在學Python Flask框架&#xff0c;并且學會做一些簡單的項目&#xff0c;如做一個網站的登錄頁面等&#xff0c;其中學到了一個路由反向解析的函數 url_for&#xff08;&#xff09;&#xff0c;這里做一個小結&#xff0c;供日后復習。 1.URL反轉&#xff1a;根據視圖函數…

Oracle執行計劃解釋

為什么80%的碼農都做不了架構師&#xff1f;>>> 一&#xff0e;相關的概念 Rowid的概念&#xff1a;rowid是一個偽列&#xff0c;既然是偽列&#xff0c;那么這個列就不是用戶定義&#xff0c;而是系統自己給加上的。 對每個表都有一個rowid的偽列&#xff0c;但是表…

鄧迎春繪畫201702作品10

轉載于:https://www.cnblogs.com/hanxiangmin/p/6439376.html

解決:FLASK中Warning: (1366, Incorrect string value: '\\xD6\\xD0\\xB9\\xFA\\xB1\\xEA...'

Flask項目中使用mysql數據庫啟動項目是發出警告&#xff1a; Warning: (1366, “Incorrect string value: ‘\xD6\xD0\xB9\xFA\xB1\xEA…’ for column ‘VARIABLE_VALUE’ at row 479/480/481”) 但是項目可以正常啟動使用 如果想將次警告去掉需要導入一個模塊 解決方法&…

Pandas常用操作總結

文章目錄前言1、DF常用的兩種創建方式方式一&#xff1a;通過np來生成方式二&#xff1a;通過字典來生成2、pandas常用的屬性及方法3、Pandas Select(數據選擇)4、Pandas Set_values設置值5、Pandas Nan處理缺失值方法一&#xff1a;剔除缺失值所在的行或列方法二&#xff1a;填…

jmeter跨線程組傳值

在測試過程中&#xff0c;有時候需要jmeter跨線程組傳值&#xff0c;jmeter本身又不具備此功能&#xff0c;那么&#xff0c;又該如何實現呢&#xff1f; 其實&#xff0c;我們可以通過BeanShell去實現。 實現過程如下&#xff1a; 1.線程組A中&#xff0c;使用正則表達式提取器…

BZOJ 3093: [Fdu校賽2012] A Famous Game

3093: [Fdu校賽2012] A Famous Game Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 248 Solved: 133[Submit][Status][Discuss]Description Mr. B and Mr. M like to play with balls. They have many balls colored in blue and red. Firstly, Mr. B randomly picks up N …

Python第三方庫的安裝及路徑查看總結

文章目錄1、python第三方庫的四種安裝方法2、更換安裝的第三方庫的鏡像源3、如何查看查看python安裝路徑以及pip安裝的包列表及路徑3.1、Linux環境下查看Python路徑3.2、查看使用pip安裝的軟件包3.3、Windows 環境 查看Python路徑實際工作中&#xff0c;經常會在公司的服務器中…

解決 Cannot open D:\Program Files\Anaconda3\Scripts\pip-script.py 問題

報錯&#xff1a; Collecting pip Using cached https://files.pythonhosted.org/packages/30/db/9e38760b32e3e7f40cce46dd5fb107b8c73840df38f0046d8e6 514e675a1/pip-19.2.3-py2.py3-none-any.whl Installing collected packages: pip Found existing installation: pi…

解決:flask-sqlalchemy.exc.DataError: (pymysql.err.DataError) (1406數據庫字段超出長度錯誤)

這個問題是說數據庫user的password字段長度超出&#xff0c;修改長度后&#xff0c; 硬核方法刪除migrations 文件夾 然后刪除數據庫中模型表格 python manage.py db init python manage.py db migrate python manage.py dbupgrate 三連擊后&#xff0c;在重新運行后方能生…

Android應用程序結構解析

界面布局文件 <?xml version"1.0" encoding"utf-8"?> 聲明了XML的版本號和編碼方式 <RelativeLayout>:聲明本界面采用的布局為RelativeLayout相對布局&#xff0c;該標簽下定義了諸如 layout_width/layout_height和paddingLeft相對布局等…

淺談開發中的MVVM模式及與MVP和MVC的區別

2019獨角獸企業重金招聘Python工程師標準>>> 我記得前段時間分享了一篇文章《 淺談Andorid開發中的MVP模式》&#xff08;點擊可跳轉&#xff09;&#xff0c;反響不錯&#xff0c;為了進一步介紹MVVM模式&#xff0c;還提前分享了實現Android中MVVM模式的一個關鍵技…

Azkaban簡介及安裝教程

前言&#xff1a; 最近在實際工作中玩到了Azkaban&#xff0c;雖然之前有簡單的接觸&#xff0c;但是真正用到的時候&#xff0c;才能體會到這個工具的實用性有多強。下面就寫個系列文章來記錄下azkaban從簡介及安裝配置再到簡單使用的一個過程。 1、概述 1.1、為什么需要工…

面向對象:實例化的對象調用類方法 db.Column()

請問 類的對象調用另一個類的對象 是什么意思&#xff1f;&#xff1f;&#xff1f; from flask_sqlalchemy import SQLAlchemydb SQLAlchemy()class User(db.Model):__tablename__ usersid db.Column(db.Integer, primary_keyTrue)username db.Column(db.String…