【发布时间】:2010-01-19 18:44:51
【问题描述】:
我有一个设计问题。为发送和接收定义单独的类是否更好。或者,最好定义一个 Thread 类?我喜欢单个 Thread 类的想法,因为它更容易共享一个可以被互斥锁锁定的队列。
设计选项#1(独立):
mySendThread = new SendThread(); // Have thread properties and separate members
myRcvThread = new RcvThread(); // Have thread properties and separate members
设计选项#2(主):
主线程-
Execute()
{
if (threadType == RCV_THREAD)
{
globalVar = new MasterThread(serialPortHandle);
}
while (!Terminated)
{
if (threadType == RCV_THREAD)
{
if(globalVar)
{
// do work
}
}
if (threadType == SND_THREAD)
{
tCountSnd = GetTickCount() / SND_THREAD_DELAY;
if (tCountSnd != tCountSnd2) {
tCountSnd2 = tCountSnd;
if (globalVar) {
// do sending work
}
}
}
}
}
【问题讨论】:
-
现有线程类(例如来自 Boost 的线程类)有什么问题?
-
什么都没有。我不是在设计线程类。我是在设计实现
标签: c++ windows multithreading mutex c++builder