#include <windows.h>
#include <iostream>
#include<string>
using namespace std;
string producer_1="mother";
string producer_2="father";
string consumer_1="son";
string sonsumer_2="daughter";
int mother_sleep;
int father_sleep;
int son_sleep;
int daughter_sleep;
int in=0;
int out=0;
int thread_buffer[20];
bool thread_continue = true;
HANDLE thread_mutex;
HANDLE thread_fullitems;
HANDLE thread_emptyitems;
DWORD WINAPI producer_mother(LPVOID);
DWORD WINAPI producer_father(LPVOID);
DWORD WINAPI consumer_son(LPVOID);
DWORD WINAPI consumer_daughter(LPVOID);
int main()
{
cout<<"now the lamb shashlik party is ready! mother and father are preparing ...... "<<endl;
cout<<"please wait...... ";
cout<<endl;
thread_mutex = CreateMutex(NULL,FALSE,NULL);
thread_fullitems = CreateSemaphore(NULL,0,20,NULL);
thread_emptyitems = CreateSemaphore(NULL,20,20,NULL);
for (int i = 0; i<20;++i)
{
thread_buffer[i] = -1;
}
int producer_count =2;
int consumer_count =2;
int thread_count= 4;
HANDLE thread_thread[4];
DWORD producer_1;
DWORD producer_2;
DWORD consumer_1;
DWORD consumer_2;
thread_thread[0]=CreateThread(NULL,0,producer_mother,NULL,0,&producer_1);
if (thread_thread[0]==NULL)
{
return -1;
}
thread_thread[1]=CreateThread(NULL,0,producer_father,NULL,0,&producer_2);
if (thread_thread[1]==NULL)
{
return -1;
}
thread_thread[2]=CreateThread(NULL,0,consumer_son,NULL,0,&consumer_1);
if (thread_thread[2]==NULL)
{
return -1;
}
thread_thread[3]=CreateThread(NULL,0,consumer_daughter,NULL,0,&consumer_2);
if (thread_thread[3]==NULL)
{
return -1;
}
while(thread_continue)
{
if(getchar())
{
thread_continue = false;
}
}
return 0;
system("pause");
}
void produce_1()
{
static int x;
cout << "mother appending a lamb shashlik ...... ("
<<mother_sleep<<"seconds )";
cout<<endl;
thread_buffer[in] = in;
x=in;
in = (in+1)%20;
for (int i=0;i<20;++i)
{
if (thread_buffer[i]==-1)
{
cout << "[ ]";
cout <<endl;
}
else
{
if(i==x)
{
cout<<"[--8-8-8-8-8-8--] <------ mother ";
cout<<endl;
}
else
{
cout << "[--8-8-8-8-8-8--] ";
cout <<endl;
}
}
}
}
void produce_2()
{
static int y;
cout << "father appending a lamb shashlik ...... ("
<<father_sleep<<"seconds )";
cout<<endl;
thread_buffer[in] = in;
y=in;
in = (in+1)%20;
for (int i=0;i<20;++i)
{
if (thread_buffer[i]==-1)
{
cout << "[ ]";
cout<<endl;
}
else
{
if(i==y)
{
cout<<"[--8-8-8-8-8-8--] <------ father ";
cout<<endl;
}
else
{
cout << "[--8-8-8-8-8-8--] ";
cout<<endl;
}
}
}
}
void consume_1()
{
static int p;
cout <<"son taking a lamb shashlik ...... ("
<<son_sleep<<"seconds )";
cout <<endl;
thread_buffer[out]= -1;
p=out;
out = (out+1)%20;
for (int i=0;i<20;++i)
{
if (thread_buffer[i]==-1)
{
if(i==p)
{
cout<<"[ ] <------ son ";
cout<<endl;
}
else
{
cout<<"[ ]";
cout<<endl;
}
}
else
{
cout << "[--8-8-8-8-8-8--] ";
cout<<endl;
}
}
}
void consume_2()
{
static int q;
cout << "daughter taking a lamb shashlik ...... ("
<<daughter_sleep<<"seconds )";
cout<<endl;
thread_buffer[out]= -1;
q=out;
out = (out+1)%20;
for (int i=0;i<20;++i)
{
if (thread_buffer[i]==-1)
{
if(i==q)
{
cout<<"[ ] <------ daugther ";
cout<<endl;
}
else
{
cout<<"[ ]";
cout<<endl;
}
}
else
{
cout << "[--8-8-8-8-8-8--] ";
cout<<endl;
}
}
}
DWORD WINAPI producer_mother(LPVOID lpPara)
{
while(thread_continue)
{
mother_sleep=120+rand()%60;
Sleep(mother_sleep * 50);
WaitForSingleObject(thread_emptyitems,INFINITE);
WaitForSingleObject(thread_mutex,INFINITE);
produce_1();
ReleaseMutex(thread_mutex);
ReleaseSemaphore(thread_fullitems,1,NULL);
}
return 0;
}
DWORD WINAPI producer_father(LPVOID lpPara)
{
while(thread_continue)
{
father_sleep=180+rand()%60;
Sleep(father_sleep * 50);
WaitForSingleObject(thread_emptyitems,INFINITE);
WaitForSingleObject(thread_mutex,INFINITE);
produce_2();
ReleaseMutex(thread_mutex);
ReleaseSemaphore(thread_fullitems,1,NULL);
}
return 0;
}
DWORD WINAPI consumer_son(LPVOID lpPara)
{
while(thread_continue)
{
son_sleep=60+rand()%60;
Sleep(son_sleep * 50);
WaitForSingleObject(thread_fullitems,INFINITE);
WaitForSingleObject(thread_mutex,INFINITE);
consume_1();
ReleaseMutex(thread_mutex);
ReleaseSemaphore(thread_emptyitems,1,NULL);
}
return 0;
}
DWORD WINAPI consumer_daughter(LPVOID lpPara)
{
while(thread_continue)
{
daughter_sleep=120;
Sleep(daughter_sleep *50);
WaitForSingleObject(thread_fullitems,INFINITE);
WaitForSingleObject(thread_mutex,INFINITE);
consume_2();
ReleaseMutex(thread_mutex);
ReleaseSemaphore(thread_emptyitems,1,NULL);
}
return 0;
}