国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 操作系統(tǒng) > 正文

操作系統(tǒng)同步互斥經(jīng)典問題——讀者寫者問題

2024-06-28 13:23:50
字體:
供稿:網(wǎng)友
操作系統(tǒng)同步互斥經(jīng)典問題——讀者寫者問題讀者優(yōu)先
#include <iostream>#include <mutex>#include <thread>using namespace std;mutex mt, wsem;int readcount = 0;#define READ(a) cout << a << " is reading..." << endl;#define FINISH(a) cout << a << " finished reading." << endl;#define WRITE cout << "writing..." << endl;void P(mutex &mt){    mt.lock();}void V(mutex &mt){    mt.unlock();}// 加入unistd.h出現(xiàn)問題,似乎與thread的兼容性比較差,于是重寫void delay(){    int sum=0;    for(int i = 0; i < 10000000; i++)        sum += i}void read(string a){    for(int i = 0; i < 3; i++)    {        P(mt);        readcount++;        if(readcount == 1)            P(wsem);        V(mt);        READ(a);        delay();        P(mt);        readcount--;        FINISH(a);        if(readcount == 0)            V(wsem);        V(mt);    }}void write(){    for(int i = 0; i < 5; i++)    {        P(wsem);        WRITE;        delay();        V(wsem);    }}int main(){    thread reader1(read, "svtter");    thread reader2(read, "sr");    thread reader3(read, "yym");    thread reader4(read, "xiaoniu");    thread writer(write);    reader1.join();    reader2.join();    reader3.join();    reader4.join();    writer.join();    return 0;}
寫者優(yōu)先

使rsem中只有一個讀者在等待,保證了如果有寫者,寫者優(yōu)先寫。

#include <iostream>#include <mutex>#include <thread>using namespace std;#define READ(a,i) cout << a << i<<" is reading..." << endl;#define FINR(a,i) cout << a << i<<" finished reading." << endl;#define WRITE(a,i) cout << a <<i<<" is writing..." << endl;#define FINW(a, i) cout << a <<i<<" finished writing..." << endl;void P(mutex &mt){    mt.lock();}void V(mutex &mt){    mt.unlock();}// 加入unistd.h出現(xiàn)問題,似乎與thread的兼容性比較差,于是重寫void delay(){    int sum=0;    for(int i = 0; i < 10000000; i++)        sum += i;}mutex mr, wsem, rsem, mrc, mwc;int readcount = 0;int writecount = 0;void read(string a){    for(int i = 0; i < 5; i++)    {        P(mr);        P(rsem);        P(mrc);        readcount++;        if(readcount == 1)            P(wsem);        V(mrc);        V(rsem);        V(mr);        READ(a, i);        P(mrc);        FINR(a, i);        readcount--;        if(readcount == 0)            V(wsem);        V(mrc);    }}void write(string a){    for(int i = 0; i < 3; i++)    {        P(mwc);        writecount++;        if(writecount == 1)            P(rsem);        V(mwc);        P(wsem);        WRITE(a, i);        FINW(a, i);        V(wsem);        P(mwc);        writecount--;        if(writecount == 0)            V(rsem);        V(mwc);    }}int main(){    thread reader1(read, "svtter");    thread writer1(write, "sr");    thread writer2(write, "zs");    thread writer3(write, "yym");    reader1.join();    writer1.join();    writer2.join();    writer3.join();    return 0;}

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 即墨市| 家居| 芒康县| 洛浦县| 辛集市| 岑溪市| 兴和县| 射阳县| 万安县| 和政县| 芮城县| 廊坊市| 定兴县| 紫金县| 休宁县| 临猗县| 宁城县| 旬阳县| 临朐县| 镇原县| 阿拉善盟| 南和县| 宝应县| 夏津县| 县级市| 轮台县| 黄龙县| 江达县| 土默特右旗| 甘肃省| 红原县| 进贤县| 蓬溪县| 张北县| 上杭县| 洛隆县| 大理市| 璧山县| 广德县| 嵊泗县| 江城|