91av视频/亚洲h视频/操亚洲美女/外国一级黄色毛片 - 国产三级三级三级三级

  • 大小: 3KB
    文件類型: .zip
    金幣: 2
    下載: 0 次
    發(fā)布日期: 2021-05-07
  • 語(yǔ)言: 其他
  • 標(biāo)簽: reader??and??writter??

資源簡(jiǎn)介

用三個(gè)算法實(shí)現(xiàn)讀者寫(xiě)者問(wèn)題,包括寫(xiě)優(yōu)先、無(wú)優(yōu)先、讀優(yōu)先等,下載即可使用。

資源截圖

代碼片段和文件信息

#include?
#include??
#include?“fstream.h“?
#include
int?readcount=0;??????????//讀者數(shù)目
int?writecount=0;?????????//寫(xiě)者數(shù)目

CRITICAL_SECTION?RP_Write;?//臨界區(qū)
CRITICAL_SECTION?cs_Write;
CRITICAL_SECTION?cs_Read;

struct?ThreadInfo???????????????//線程信息
{ int?Threadhao;??????????????//線程序號(hào)
char?ThreadClass;???????????//線程類別
double?ThreadStartTime;?????//線程開(kāi)始時(shí)間
double?ThreadRunTime;???????//線程讀寫(xiě)持續(xù)時(shí)間
};
void?ReaderFun(char*?file);//讀者優(yōu)先函數(shù)
void?R_ReaderThread(void?*p);//處理讀者優(yōu)先讀者線程
void?R_WriterThread(void?*p);//處理讀者優(yōu)先寫(xiě)者線程
void?WriterFun(char*?file);??
void?W_ReaderThread(void?*p);
void?W_WriterThread(void?*p);
void?NotFun(char*?file);
void?N_ReaderThread(void?*p);
void?N_WriterThread(void?*p);
int?main()//主函數(shù)
{
char?select;
while?(true)
{
cout<<“***************本程序?qū)崿F(xiàn)讀者-寫(xiě)者問(wèn)題*******\n“< cout<<“??????????????1:讀者優(yōu)先“<???????cout<<“??????????????2:寫(xiě)者優(yōu)先“< ????cout<<“??????????????3:無(wú)優(yōu)先“< cout<<“??????????????4:退出“< ??cout<<“\n*********************************************“< cout<<“請(qǐng)選擇要進(jìn)行的操作:“<???? do
{
cin>>select;
if(select!=‘1‘?&&?select!=‘2‘?&&?select!=‘3‘)
cout<<“你操作有誤,請(qǐng)重試!“< }while?(select!=‘1‘?&&?select!=‘2‘?&&?select!=‘3‘);
system(“cls“);
if?(select==‘4‘)
return?0;//退出
else?if?(select==‘1‘)//調(diào)用讀者優(yōu)先
ReaderFun(“peizhi.txt“);
?else?if(select==‘2‘)//調(diào)用寫(xiě)者優(yōu)先
????WriterFun(“peizhi.txt“);
else?if(select==‘3‘)
NotFun(“peizhi.txt“);
cout<<“\n是否還有繼續(xù)???1.?繼續(xù)???2.退出“< do
{
cin>>select;
if(select!=‘1‘?&&?select!=‘2‘?)
cout<<“你操作有誤,請(qǐng)重試!“< }while?(select!=‘1‘?&&?select!=‘2‘);
if(select==‘2‘)
return?0;// 退出
system(“cls“);
}
return?0;
}

//讀者優(yōu)先函數(shù)
void?ReaderFun(char*?file)
{
DWORD?n_thread=0;????????//線程數(shù)初值為0?DWORD?是unsigned?long
DWORD?thread_ID;?????????//線程ID
DWORD?wait_for_all;??????//等待所有線程結(jié)束
//臨界資源
HANDLE?h_Mutex;
//互斥對(duì)象(h_Mutex)確保線程擁有對(duì)單個(gè)資源的互斥訪問(wèn)權(quán)
h_Mutex=CreateMutex(NULLFALSE“mutex_for_readcount“);???//創(chuàng)建互斥體
HANDLE?h_Thread[64]; //線程對(duì)象數(shù)組,最大64
ThreadInfo?thread_info[64];
readcount=0;?????
InitializeCriticalSection(&RP_Write);??//初始化臨界區(qū)
ifstream?inFile;
inFile.open(file);?????????????????????
cout<<“讀者優(yōu)先:\n“< while?(inFile)
{//讀入每一個(gè)讀者、寫(xiě)者的信息
inFile>>thread_info[n_thread].Threadhao
>>thread_info[n_thread].ThreadClass
?>>thread_info[n_thread].ThreadStartTime
>>thread_info[n_thread].ThreadRunTime;
if?(-1?==?inFile.get())???//從輸入流讀取一個(gè)字符,并將它轉(zhuǎn)成int值?送返
break;
n_thread++;//線程數(shù)加一
}
for?(int?i=0;i<(int)(n_thread);i++)
{
if?(thread_info[i].ThreadClass==‘r‘)
//創(chuàng)建讀者線程
h_Thread[i]=CreateThread(NULL0\
(LPTHREAD_START_ROUTINE)(R_ReaderThread)\
&thread_info[i]0&thread_ID);
else
//創(chuàng)建寫(xiě)者線程
h_Thread[i]=CreateThread(NULL0\
(LPTHREAD_START_ROUTINE)(R_WriterThread)\
&thread_info[i]0&thread_ID);
}
wait_for_all=WaitForMultipleobjects(n_thread

?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????文件??????????46??2015-12-18?13:58??peizhi.txt
?????文件???????13354??2015-12-22?09:38??reader_writer.cpp

評(píng)論

共有 條評(píng)論