==============关于多线程使用的几个问题===============

studyvc6 2006-11-25 10:49:57
1、在C++builder中,线程使用thread object,还是_beginthread,还是使用createthread函数比较好?这几个有什么优劣?应该如何选择呢?
估计很多人会回答使用第一个,因为是cb特意提供的。但是从方便性上来讲,没有_beginthread方便,从功能强弱上来讲,又不如createthread。

2、有关消费者和生产者线程同步的例子,谁能给我讲清楚一下,最好有源码。
题目:有m个生产者,k个缓冲池,n个消费者,每次生产或消费的大小都是一个缓冲池的大小,开始时缓冲池都为空。写一个生产者消费者线程同步的例子。

3、很想学东西,所以倾尽所有的分,不够的话请包涵,望高手讲解详细一些!
...全文
254 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
sbkopoky 2007-02-17
  • 打赏
  • 举报
回复
有一个香港和悦网络电话的源文件 ,有兴趣妨进入我的BLOG看看
studyvc6 2006-11-27
  • 打赏
  • 举报
回复
两位都没有给出我需要的答案!
第一位给出的例子是一个生产者和第一个消费者的例子吧?这个源码我也有。我需要多生产者和多消费者的源码和详细讲解,想学习一下。
第二位讲的对我来说基本没什么参考,我都知道的。而且讲的似乎还不对,功能最强大的应该是createthread吧?也是最复杂的吧。期待高手以自己的实际经验讲解一下。

盼望高手继续回答



wz 2006-11-26
  • 打赏
  • 举报
回复
这种生产消费问题教科书上都有:
//---------------------------------------------------------------------------

#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);
}
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormCreate(TObject *Sender)
{
thread1=new TProducerThread(true,Edit1);
thread2=new TProducerThread(true,Edit2);
thread3=new TCustomerThread(true,ComboBox1);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
thread1->Resume();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
thread2->Resume();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button3Click(TObject *Sender)
{
thread3->Resume();
}
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
#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);
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{

}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormCreate(TObject *Sender)
{
thread1=new TProducerThread(true,Edit1);
thread2=new TProducerThread(true,Edit2);
thread3=new TCustomerThread(true,ComboBox1);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
thread1->Resume();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
thread2->Resume();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button3Click(TObject *Sender)
{
thread3->Resume();
}
//---------------------------------------------------------------------------

leonatcs 2006-11-26
  • 打赏
  • 举报
回复
1.TThread是面向对象的,vcl里的类,_beginthread是c runtime library里的,windows平台上c语言最终还是要调用CreateThread实现。CreateThread是Windows API。要说用哪个我觉得看线程的功能复杂与否了,复杂的线程用TThread,简单的用剩下两个。
2.看源码不如看伪码,找本操作系统的书看看就清楚了。
3.分不少了:)

1,316

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder 网络及通讯开发
社区管理员
  • 网络及通讯开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧