⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit2.cpp

📁 C++Builder高级应用开发指南随书源码
💻 CPP
字号:
//---------------------------------------------------------------------------

#include <vcl.h>
#include <syncobjs.hpp>
#pragma hdrstop

#include "Unit2.h"

#pragma package(smart_init)

TEvent * BeginProduce=new TEvent(NULL,false,true,"TestEvent1");
TEvent * BeginConsume=new TEvent(NULL,false,false,"TestEvent2");
TCriticalSection * ProduceGuard=new TCriticalSection;
int Product;
bool HasProduct;

__fastcall TProducerThread::TProducerThread(bool CreateSuspended,TEdit * AEdit)
        : TThread(CreateSuspended)
{
    edResult=AEdit;
}
//---------------------------------------------------------------------------
void __fastcall TProducerThread::Execute()
{
    int i=0;
    while(i<100)
    {
        BeginProduce->WaitFor(INFINITE);
        ProduceGuard->Acquire();
        if(!HasProduct)
        {
            HasProduct=true;
            Product=i;
            i++;
            strResult=IntToStr(i);
            Synchronize(ShowResult);
            BeginConsume->SetEvent();
            Sleep(100);
        }
        ProduceGuard->Release();
    }
}
//---------------------------------------------------------------------------
void __fastcall TProducerThread::ShowResult()
{
    edResult->Text=strResult;
}
//---------------------------------------------------------------------------

__fastcall TCustomerThread::TCustomerThread(bool CreateSuspended,TComboBox * AComboBox)
        : TThread(CreateSuspended)
{
    cbResult=AComboBox;
}
//---------------------------------------------------------------------------
void __fastcall TCustomerThread::Execute()
{
    for(int i=0;i<200;i++)
    {
        BeginConsume->WaitFor(INFINITE);
        strResult=IntToStr(Product);
        Synchronize(ShowResult);
        HasProduct=false;
        Sleep(150);
        BeginProduce->SetEvent();
    }
}
//---------------------------------------------------------------------------
void __fastcall TCustomerThread::ShowResult()
{
    cbResult->Items->Add(strResult);
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -