多线程:如何“锁定”一个或多个线程的对象
我有一个“主”线程,它创建 SlowData
类的对象数组。这些对象的操作速度相对较慢。这就是为什么当我最初加载它们时,它们充满了存根数据。
加载数组中的每个对象后,我可以立即在 UI 中显示该数组。与此同时,我开始对数组进行计算。 SlowDataComputation
对象被指示向 UI 报告结果(UI 反过来会可视化特定 SlowData
对象的修改)。
下面是它的样子:
void main()
{
std::vector<SlowData> aData;
aData.reserve(1000000); // one million instances
Fast_PopulateVector(aData);
DataUi aUi;
SlowDataComputation aCalc;
aCalc.SetUiCallback(aUi); // instruct the object to periodically
// report computation results to the UI
aCalc.Slow_ComputeInSeveralThreads(aData);
aUi.VisualizeInMessageLoop(aData); // message loop that exits upon user signal
if ( aCalc.StillWorking() )
aCalc.Stop(); // terminate threads gradually
// after VisualizeInMessageLoop returns, aData will then be destroyed
}
我希望 SlowDataComputation
启动几个线程,每个线程都会处理数组中的某个段:
class SlowDataComputation
{
std::vector<SlowData> * myData;
DataUi * myUI;
public:
void Slow_ComputeInSeveralThreads(std::vector<SlowData> & theV)
{
myData = &theV;
size_t aSize = myData->size();
size_t aNumThreads = 10;
size_t aBlockSize = aSize / aNumThreads;
for (size_ti = 0; i < aNumThreads; i++)
{
ComputationThread aThr(myData);
aThr.SetBoundary(i * aBlockSize, (i+1) * aBlockSize);
aThr.SetUiCallback(myUI);
aThr.Run(); // process a given segment in the array
}
}
};
最后,这是 UI 的一些伪代码:
class DataUi
{
std::vector<SlowData> * myData;
public:
void VisualizeInMessageLoop()
{
Message aMsg;
while ( HasMessage(aMsg) )
if (aMsg.Code == Message::MSG_PROGRAM_QUIT)
SignalQuit(); // next time, HasMessage will return false
// return from message loop
}
};
问题是:如何确保 SlowData
实例的销毁安全,以便十个计算线程中的任何一个当前正在处理某个对象,销毁需要等到每个计算线程完成(要么因为它完成处理,要么因为调用了 SlowDataComputation::Stop
方法)?什么同步原语最适合此任务?
我设法用一个计算线程解决了这个问题。我使用了关键部分。但这一解决方案无法很好地适应多个计算线程。
I have one 'main' thread that creates an array of objects of class SlowData
. These objects are relatively slow to manipulate with. This is why, when I initially load them, they are filled with stub data.
Immediately after each object in the array is loaded, I can display the array in the UI. Parallel with that, I initiate the computation over the array. The SlowDataComputation
object is instructed to report results to the UI (which in turn will visualize the modification of a particular SlowData
object).
Here's how this would look:
void main()
{
std::vector<SlowData> aData;
aData.reserve(1000000); // one million instances
Fast_PopulateVector(aData);
DataUi aUi;
SlowDataComputation aCalc;
aCalc.SetUiCallback(aUi); // instruct the object to periodically
// report computation results to the UI
aCalc.Slow_ComputeInSeveralThreads(aData);
aUi.VisualizeInMessageLoop(aData); // message loop that exits upon user signal
if ( aCalc.StillWorking() )
aCalc.Stop(); // terminate threads gradually
// after VisualizeInMessageLoop returns, aData will then be destroyed
}
I want SlowDataComputation
to launch several threads, each of which would process a certain segment within the array:
class SlowDataComputation
{
std::vector<SlowData> * myData;
DataUi * myUI;
public:
void Slow_ComputeInSeveralThreads(std::vector<SlowData> & theV)
{
myData = &theV;
size_t aSize = myData->size();
size_t aNumThreads = 10;
size_t aBlockSize = aSize / aNumThreads;
for (size_ti = 0; i < aNumThreads; i++)
{
ComputationThread aThr(myData);
aThr.SetBoundary(i * aBlockSize, (i+1) * aBlockSize);
aThr.SetUiCallback(myUI);
aThr.Run(); // process a given segment in the array
}
}
};
Finally, here's some pseudo-code for the UI:
class DataUi
{
std::vector<SlowData> * myData;
public:
void VisualizeInMessageLoop()
{
Message aMsg;
while ( HasMessage(aMsg) )
if (aMsg.Code == Message::MSG_PROGRAM_QUIT)
SignalQuit(); // next time, HasMessage will return false
// return from message loop
}
};
The question is: how do I secure the destruction of SlowData
instances so that if any of the ten computation threads is currently working with an object, destruction needs to wait until each of these computation threads completes (either because it finished processing or because the SlowDataComputation::Stop
method was called)? What synchronization primitive is most suitable for this task?
I managed to solve this problem with one computation thread. I used Critical Sections. But this solution doesn't scale well for more than one computation threads.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
main
函数末尾使用 pthread_join就像......
你不需要锁定对象,这更昂贵。您只需要知道所有线程都完成了计算。
At the end of your
main
functionjoin
all your threads with pthread_joinSomething like ...
you don't need to lock the object, which is more expensive. You just need to know that all the threads finished their computation.