我怎样才能让生产者和消费者双向沟通?
How can I have a producer and consumer communicate both ways?
我想让工作线程等待一个命令,执行它,然后将结果发送回调用者。由于返回的响应,这与常规 producer/consumer 问题不同。
这就是我的想法:
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
}
我走在正确的轨道上吗? worker 有没有可能在 main 开始等待之前做出响应?
(如果相关,我正在使用 C++)
你走在正确的轨道上,但你可以稍微简化一下,消除发出信号或等待信号的需要
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
}
根据您对共享缓冲区进行分区或不进行分区的方式,您可能需要保护它免受并发写入。
我想让工作线程等待一个命令,执行它,然后将结果发送回调用者。由于返回的响应,这与常规 producer/consumer 问题不同。
这就是我的想法:
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
}
我走在正确的轨道上吗? worker 有没有可能在 main 开始等待之前做出响应?
(如果相关,我正在使用 C++)
你走在正确的轨道上,但你可以稍微简化一下,消除发出信号或等待信号的需要
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
}
根据您对共享缓冲区进行分区或不进行分区的方式,您可能需要保护它免受并发写入。