使用pthreads(在C++中)将数据传递给线程

Passing data to threads using pthreads (in C++)

本文关键字:数据 线程 pthreads C++ 使用      更新时间:2023-10-16

大家好,

情况是这样的,我有两组线程,Mapper和Reducers

映射器解析文本文件,并基于哈希函数将带有行号的单个单词发送到reducer线程

Reducer有一个固定的缓冲区大小,并将该缓冲区中的每个字放在一个列表中

我有映射器和还原器线程到单独的Pthread数组

For example:
[Step 1]  For the word "example" Mapper thread produces a hash value of 3
[Step 2]  Mapper thread puts "example" and its line number in the 4th 
          reducer's threads (0,1,2,3) buffer
[Step 3]  Reducer thread #4 will place "example" with its 
          line number to the list

如果不清楚,请通知我。

问题
我不知道如何将信息从映射器线程传递到适当的用户线程,所以步骤2。

有人能告诉我如何做到这一点吗?

我希望我的描述足够清晰,但如果有帮助的话,我可以分享一些代码。

有很多方法可以在线程之间共享数据。你的例子表明你需要某种管道。这意味着一个线程将数据推送到共享数据结构中,而目标线程将从同一结构中提取数据。此数据结构可以是一个矢量,也可以是一份受锁保护的列表。也可以使用免锁机构(请参阅http://msdn.microsoft.com/en-us/library/windows/desktop/ms684121(v=vs.85).aspx)。您可以在此处阅读有关管道的更多信息:http://msdn.microsoft.com/en-us/library/gg663538.aspx