当 pthread 启动时,它是否需要互斥锁来访问之前在生成它的线程中写入的全局数据
When a pthread starts does it need a mutex to access global data written previously in the thread that spawned it
我在Linux上有一个带有两个线程的c ++程序线程 A 将一些数据写入 std::map不会将其他数据写入此地图然后线程 A 创建线程 B线程 B 从映射中读取数据
那么在这种情况下我需要使用互斥体吗?
如果没有,那么在哪里指定此保证。
No.线程创建是一个同步点,因此在创建第二个线程之前第一个线程产生的所有效果将在创建第二个线程时"发生"(以标准语言可以精确的方式)。
更准确地说,在一个线程创建并随后加入另一个线程的上下文中......
- 线程创建之前的所有内容 - 在线程函数内的任何内容之前,以及 线程
- 函数中的所有内容都发生在线程连接之后的任何内容之前。
不,仅当您要同时从两个或多个线程读取/写入内存时,才需要互斥锁。由于当您有多个线程时,您的 std::map 是不可变的,因此您不需要任何同步。您永远不需要在不可变数据上使用互斥锁。即使在你的情况下,线程 B 会写入 std::map,仍然只有一个线程会同时在这个内存上 r/w,你不需要同步一个线程。
事实上,只要有可能,主导模式应该是:
(可变)数据随任务移动
因此,数据归使用它的线程所有。现在,它不需要"物理"迁移,但使用移动语义使图片更加清晰:
#include <map>
#include <string>
using namespace std;
using Map = map<string, int>;
Map worker(Map&& data)
{
for (auto& el : data)
el.second *= -1;
return std::move(data);
}
#include <cassert>
#include <iostream>
#include <future>
int main()
{
Map global
{ { "one", 1 },
{ "two", 2 },
{ "three", 3 } };
auto background = async(worker, std::move(global));
assert(global.empty()); // data now owned by the worker thread!
global = background.get(); // await the result from the worker
for (auto const& el: global) // now the data is owned by the main thread
std::cout << el.first << ": " << el.second << "n";
}
请注意main
中间的assert
:甚至不可能并发访问。
在科利鲁上观看直播。
相关文章:
- 在cuda线程之间共享大量常量数据
- 将线程中的数据存储到全局容器的最佳方法?
- 线程之间的实时数据共享
- C++:在多个线程中访问同一数组/向量的不同单元格是否会产生数据竞赛?
- 提供对不同类型的数据(建议、代码审查)的线程安全访问的类
- 在线程函数中处理数据向量时进行线程竞速
- 使来自线程的数据流对所有其他线程都可读
- 简单使用 std::atomic 在两个线程之间共享数据
- 为什么添加延迟会提高此多线程环境中的数据吞吐量?
- 跨线程共享数据集与拆分每个线程的数据
- 标准 cpp 数据结构和线程互斥锁?
- 在std::线程中使用已分配的结构数据
- 多个线程将数据发送到数组C++
- Qt C++ - 如何将数据从工作线程传递到主线程?
- 在使用 std::cout 和多线程程序中如何避免数据竞争<iomanip>?
- 充分私有化 OpenMP 线程中的数据
- 用于在C++中计算用户数据的线程
- 在不减慢线程速度的情况下保存大量数据
- 线程数据封装最佳实践
- Apache 崩溃并显示错误 R6016 没有足够的空间来存储线程数据