使用pthreads(在C++中)将数据传递给线程
Passing data to threads using pthreads (in C++)
大家好,
情况是这样的,我有两组线程,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
相关文章:
- 在cuda线程之间共享大量常量数据
- 将线程中的数据存储到全局容器的最佳方法?
- 线程之间的实时数据共享
- C++:在多个线程中访问同一数组/向量的不同单元格是否会产生数据竞赛?
- 提供对不同类型的数据(建议、代码审查)的线程安全访问的类
- 在线程函数中处理数据向量时进行线程竞速
- 使来自线程的数据流对所有其他线程都可读
- 简单使用 std::atomic 在两个线程之间共享数据
- 为什么添加延迟会提高此多线程环境中的数据吞吐量?
- 跨线程共享数据集与拆分每个线程的数据
- 标准 cpp 数据结构和线程互斥锁?
- 在std::线程中使用已分配的结构数据
- 多个线程将数据发送到数组C++
- Qt C++ - 如何将数据从工作线程传递到主线程?
- 在使用 std::cout 和多线程程序中如何避免数据竞争<iomanip>?
- 充分私有化 OpenMP 线程中的数据
- 用于在C++中计算用户数据的线程
- 在不减慢线程速度的情况下保存大量数据
- 线程清理器检测到数据争用,问题出在哪里?
- Qt 如何将数据从一个 GUI 传递到另一个线程