Void __fast call TProducerThread:: Execute ()
{
//---- Place thread code here ----
Int i = 0;
Int j;
while(i<100) //每个生产者线程生产100个商品
{
Sleep(1000);//延迟,为清楚得显示执行效果
if(Form1->buffer_size > 0)//缓冲池不空,通知消费者消费
{
Form1->Begin Consumer->Set Event ();
}
Form1->Produce Guard->Acquire ();
i++;
StrResult = IntToStr (i);
J = Form1->buffer_size;
Form1->Product [j] = i;
Form1->buffer_size++;
Synchronize(Show Result);//刷新界面,显示最新生产-消费状况
Form1->Begin Consumer->Set Event();//通知消费者消费
if(Form1->buffer_size == 5)//缓冲池满,挂起生产者线程,直到通知再生产
{
Form1->Begin Produce->Wait For (INFINITE);
}
Sleep (1000);
Form1->Produce Guard->Release ();
}
While (Form1->buffer_size > 0)
{
Form1->Begin Consumer->Set Event ();
}
}
Void __fast call TConsumerThread::Execute()
{
//---- Place thread code here ----
Int j;
For (int i = 0;i < 200;i++)
{
Sleep(100); //延迟,为清楚得显示执行效果
Form1->Begin Consumer->Wait For(INFINITE);//挂起消费者线程,直到通知再消费
J = Form1->buffer_size - 1;
StrResult = IntToStr (Form1->Product [j]);
Form1->buffer_size--;
Synchronize(Show Result); //刷新界面,显示最新生产-消费状况
if(Form1->buffer_size == 4)//缓冲池不再full,唤醒由于缓冲池full而挂起的生产者线程
{
Form1->Begin Produce->Set Event ();
}
Sleep (100);
}
}
新闻热点
疑难解答