我有一段代码tbb::parallel_for
用于多线程循环,由主线程调用。在那个循环中,我需要主线程来更新 UI 以反映进度。根据我的观察,tbb::parallel_for
总是使用调用者线程 + N 个工作线程。但是,我想知道,调用线程的使用是否得到保证,或者恰好是这种情况?
这是示例代码:
static thread_local bool _mainThread = false; // false in all threads
_mainThread = true; // now true in main thread, but false in others
tbb::parallel_for(start, end, *this);
void Bender::processor::operator()(size_t i) const
{
...
if(_mainThread) // only main thread will issue events
ProgressUpdatedEvent(progress);
}
谢谢!