據說thread_local每個線程有一個副本。C++併發操作9.6 threadpool使用thread_local
所以,如果線程池(稱爲A)創造另一個線程(稱之爲B),該thread_local變量(local_work_queue)在線程A和B的線程是兩個不同的事情。
所以我迷惑的是,當在池線程A(int main())中的池頂峯任務,它如何訪問子線程B中的local_work_queue?他們完全不相關。
函數提交在池線程中,而local_work_queue只在子進程中初始化,所以在提交函數中,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);
}
究竟是什麼問題? –
問題是,submit()中的local_work_queue始終爲nullptr,因此線程池無法訪問將任務推送到子線程中的local_work_queue。以及如何解決它? – swtybb