国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 柯坪县| 济宁市| 潞城市| 延庆县| 顺义区| 岐山县| 江津市| 衡阳市| 福泉市| 维西| 射洪县| 三亚市| 壤塘县| 霍林郭勒市| 彭阳县| 常德市| 临西县| 南投市| 巴塘县| 花莲市| 天峨县| 金华市| 伽师县| 成安县| 海宁市| 太谷县| 临漳县| 辽阳县| 化德县| 宁海县| 衡阳县| 宁国市| 孟连| 郸城县| 永福县| 木里| 视频| 西畴县| 东至县| 武穴市| 天峻县|