C++ 操作 9.6 线程池中的并发使用thread_local
C++ Concurrency in Action 9.6 threadpool use thread_local
据说
,thread_local,每个线程有一个副本。
所以如果 threadPool(称它为 A)的线程创建了另一个线程(称它为 B),则线程 A 和 B 中的thread_local变量(local_work_queue)是两回事。
所以我混淆了当池线程 A(int main())中的池峰会任务时,它如何访问子线程 B 中的local_work_queue??? 它们完全无关紧要。
函数提交在池线程中,local_work_queue只在 sub thred 中初始化,所以在提交函数中,local_work_queue将始终为 nullptr,不是吗?
下面是代码:
class thread_pool
{
typedef std::queue<function_wrapper> local_queue_type;
static thread_local std::unique_ptr<local_queue_type> local_work_queue;
std::vector<std::thread> threads;`
thread_pool()
{
unsigned const thread_count=std::thread::hardware_concurrency();
for(unsigned i=0;i<thread_count;++i)
{
threads.push_back(
std::thread(&thread_pool::worker_thread,this,i));
}
}
void worker_thread()
{
local_work_queue.reset(new local_queue_type); //just init in the sub thread
while(1)
{
function_wrapper task;
if(local_work_queue && !local_work_queue->empty())
{
task=std::move(local_work_queue->front());
local_work_queue->pop();
task();
}
else
{
std::this_thread::yield();
}
}
}
template<typename FunctionType>
std::future<typename std::result_of<FunctionType()>::type>submit(FunctionType f)
{
typedef typename std::result_of<FunctionType()>::type result_type;
std::packaged_task<result_type()> task(f);
std::future<result_type> res(task.get_future());
if(local_work_queue) //function submit is in pool thread, and local_work_queue only init in sub thred, so in this ,local_work_queue will away nullptr, isn't it? so confuse how the code work.
{
local_work_queue->push(std::move(task));
}
return res;
}
};
void func()
{
std::cout<<"test"<<std::endl;
}
int main()
{
thread_pool p;
p.submit(func);
}
但你把它变成了静态的。所以它可以通过课堂访问吗?这凌驾于我假设的thread_local之上。
请参阅存储类
5) thread_local 关键字只允许用于在命名空间范围内声明的对象、在块范围内声明的对象和静态数据成员。它指示对象具有线程存储持续时间。它可以与 static 或 extern 结合使用,分别指定内部或外部链接(始终具有外部链接的静态数据成员除外),但额外的静态不会影响存储持续时间。
它的生命周期是线程本地的,但我想链接是类范围的。
相关文章:
- 控制允许动态运行c++的并发操作数
- 在std::thread中,joinable()然后join()线程安全吗
- 节俭并发:未解决的外部问题
- <Windows>为什么 std::thread::native_handle 返回类型为"long long unsigned int"的值,而不是 void*(又名 HANDLE)?
- 分离一个静态常量 std::thread?
- 尝试使用 std::vector<std::thread时出现静态断言失败错误>
- 使用 thread 类在 C++ 中构造线程的动态数组时出错
- 当指向对象的指针作为参数传递给 std::thread 时,内存可见性
- 如何从 std::thread 返回值
- 并发/多线程:是否可以以这种方式生成相同的输出?
- 在C++中使用并行化的预期速度是多少(不是 OpenMp,而是 <thread>)
- 用于在并发环境中访问 MMIO 的软件模式
- 将 std::thread by 值推送到列表中
- 转发变量参数列表以模拟 std::thread
- C++中的并发哈希表
- 嵌入式设备 -> std::thread -> FreeRTOS?
- 对 'std::thread::_M_start_thread CMake 的未定义引用进行基准测试
- 提升 asio 并发计时器取消问题与链
- C++具有基元类型的并发队列
- std::thread 增加 DLL 引用计数,从而防止卸载 DLL