本文整理汇总了C++中unique_lock::lock方法的典型用法代码示例。如果您正苦于以下问题:C++ unique_lock::lock方法的具体用法?C++ unique_lock::lock怎么用?C++ unique_lock::lock使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类unique_lock
的用法示例。
在下文中一共展示了unique_lock::lock方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: do_job
void ThreadPool::do_job(ThreadPool::TaskType& task, unique_lock< mutex >& exclusion)
{
exclusion.unlock();
//__sync_synchronize();
auto start = std::chrono::system_clock::now();
string account = task.account;
auto task_future = task.task.get_future();
task.task();
try
{
task_future.get();
}
catch(std::exception&e)
{
log_file << "Exception thrown in job: " << e.what() << endl;
breakpoint();
}
catch(...)
{
log_file << "Exception thrown... but it's a mystery... results undefined" << endl;
breakpoint();
}
auto end = std::chrono::system_clock::now();
int elapsed_seconds = std::chrono::duration_cast<std::chrono::seconds>
(end-start).count();
//atomic_thread_fence(std::memory_order_seq_cst);
//__sync_synchronize();
exclusion.lock();
accounts[account] += elapsed_seconds;
work_queue_changed.notify_all();
}
示例2: DoLoad
double SpriteQueue::DoLoad(unique_lock<mutex> &lock) const
{
for(int i = 0; !toLoad.empty() && i < 30; ++i)
{
Item item = toLoad.front();
toLoad.pop();
lock.unlock();
item.sprite->AddFrame(item.frame, item.image, item.mask);
lock.lock();
++completed;
}
// Wait until we have completed loading of as many sprites as we have added.
// The value of "added" is protected by readMutex.
unique_lock<mutex> readLock(readMutex);
// Special cases: we're bailing out, or we are done.
if(added <= 0 || added == completed)
return 1.;
return static_cast<double>(completed) / static_cast<double>(added);
}