我想让一个工作线程等待命令,执行它,然后将结果发送回调用者。这与常规生产者/消费者问题不同,因为回复。
这就是我的想法:
main
{
construct workers
push to shared input buffers
notify workers
wait
print results from shared output buffer
}
worker
{
wait
read from shared input buffer
do work
notify main
}
我是否在正确的轨道上?在主要开始等待之前,工人是否有可能做出回应?
(如果相关,我正在使用C ++)
答案 0 :(得分:2)
你是在正确的轨道上,但你可以简化一些事情,消除信号或等待信号的需要
main
{
push to shared input buffers
construct workers // create and immediately run. No need for signal
while more workers
join worker // will block until worker thread exits
print results from shared output buffer
}
worker
{
read from shared input buffer
do work
}
根据您对共享缓冲区进行分区的方式或不进行分区,您可能需要保护它不受并发写入的影响。