本文整理汇总了C++中boost::unique_lock类的典型用法代码示例。如果您正苦于以下问题:C++ unique_lock类的具体用法?C++ unique_lock怎么用?C++ unique_lock使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了unique_lock类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: HPX_ASSERT
bool coalescing_message_handler::flush(
boost::unique_lock<mutex_type>& l,
bool stop_buffering)
{
HPX_ASSERT(l.owns_lock());
if (!stopped_ && stop_buffering) {
stopped_ = true;
util::unlock_guard<boost::unique_lock<mutex_type> > ul(l);
timer_.stop(); // interrupt timer
}
if (buffer_.empty())
return false;
detail::message_buffer buff (buffer_.capacity());
std::swap(buff, buffer_);
l.unlock();
HPX_ASSERT(NULL != pp_);
buff(pp_); // 'invoke' the buffer
return true;
}
示例2: assert
void
condition_variable::wait( boost::unique_lock< mutex >& lock )
{
assert( lock.owns_lock( ) );
boost::unique_lock< mutex > local_lock( *lock.release( ), boost::adopt_lock );
better_lock lock_std( mt );
global_thr_pool.yield( [&]( coroutine running ) {
lock_unlocker< better_lock > l_unlock_std_mt( lock_std );
lock_unlocker< boost::unique_lock< mutex > > l_unlock_co_mt( local_lock );
waiting_cors.emplace_back( std::move( running ) );
} );
// If this coroutine is resumed by a thread different from the one that
// yielded,
// then it is possible that the changes to internal state of the 'lock'
// variable will
// not have been yet acknowledged by the resuming thread.
// This special case will lead to a difficult to debug race condition
// involving
// the owns_lock member variable.
// This is why we use local_lock.
assert( !local_lock.owns_lock( ) );
lock = boost::unique_lock< mutex >( *local_lock.release( ) );
}
示例3: wait_until
future_status result::wait_until(time_point const& wait_timeout_time,
boost::unique_lock<boost::mutex>& lock) const
{
if(ready(lock))
return future_status::ready;
while(!expired() && !(clock_type::now() > wait_timeout_time))
{
// note: don't set to wait for 0 ms, it might not timeout
if(m_condition.wait_for(lock, boost::chrono::milliseconds(1),
boost::bind(&result::ready, this, boost::ref(lock))))
{
return future_status::ready;
}
if(expired() || (clock_type::now() > wait_timeout_time))
break;
lock.unlock();
io_runner::poll_one(*m_io_service);
lock.lock();
}
if(clock_type::now() > wait_timeout_time)
return future_status::timeout;
BOOST_ASSERT(expired());
m_exception = boost::make_shared<remote_error>
(remote_error::timeout, "remote call timeout");
m_ready = true;
return future_status::ready;
}
示例4: main
int main()
{
boost::mutex m;
m.lock();
#if ! defined(BOOST_NO_CXX11_AUTO_DECLARATIONS)
auto
#else
boost::unique_lock<boost::mutex>
#endif
lk = boost::make_unique_lock(m, boost::adopt_lock);
BOOST_TEST(lk.mutex() == &m);
BOOST_TEST(lk.owns_lock() == true);
return boost::report_errors();
}
示例5: resume
// assumes that this thread has acquired l
void resume(boost::unique_lock<mutex_type>& l)
{ // {{{
HPX_ASSERT(l.owns_lock());
// resume as many waiting LCOs as possible
while (!thread_queue_.empty() && !value_queue_.empty())
{
ValueType value = value_queue_.front().val_;
HPX_ASSERT(0 != value_queue_.front().count_);
if (1 == value_queue_.front().count_)
{
value_queue_.front().count_ = 0;
value_queue_.pop_front();
}
else
--value_queue_.front().count_;
naming::id_type id = thread_queue_.front().id_;
thread_queue_.front().id_ = naming::invalid_id;
thread_queue_.pop_front();
{
util::unlock_guard<boost::unique_lock<mutex_type> > ul(l);
// set the LCO's result
applier::trigger(id, std::move(value));
}
}
} // }}}
示例6: accept_locked
connection_ptr accept_locked(boost::unique_lock<mutex_type> & header_lock)
{
connection_ptr res;
util::mpi_environment::scoped_try_lock l;
if(l.locked)
{
MPI_Status status;
if(request_done_locked(hdr_request_, &status))
{
header h = new_header();
l.unlock();
header_lock.unlock();
res.reset(
new connection_type(
status.MPI_SOURCE
, h
, pp_
)
);
return res;
}
}
return res;
}
示例7: signal_locked
void signal_locked(boost::int64_t count,
boost::unique_lock<mutex_type> l)
{
HPX_ASSERT(l.owns_lock());
mutex_type* mtx = l.mutex();
// release no more threads than we get resources
value_ += count;
for (boost::int64_t i = 0; value_ >= 0 && i < count; ++i)
{
// notify_one() returns false if no more threads are
// waiting
if (!cond_.notify_one(std::move(l)))
break;
l = boost::unique_lock<mutex_type>(*mtx);
}
}
示例8: setPlanet
void Bank::setPlanet(boost::unique_lock<boost::mutex>& lock, int8_t planet) {
planet_ = planet;
auto dispatcher = GetEventDispatcher();
lock.unlock();
dispatcher->DispatchMainThread(std::make_shared<CreatureObjectEvent>("CreatureObject::PersistHomeBank", owner_->GetCreature()));
}
示例9: setCredits
void Bank::setCredits(boost::unique_lock<boost::mutex>& lock, uint32 credits) {
credits_ = credits;
auto dispatcher = GetEventDispatcher();
lock.unlock();
dispatcher->Dispatch(std::make_shared<CreatureObjectEvent>("CreatureObject::BankCredits", owner_->GetCreature()));
dispatcher->DispatchMainThread(std::make_shared<CreatureObjectEvent>("CreatureObject::PersistBankCredits", owner_->GetCreature()));
}
示例10: wait_locked
void wait_locked(boost::int64_t count,
boost::unique_lock<mutex_type>& l)
{
HPX_ASSERT(l.owns_lock());
while (value_ < count)
{
cond_.wait(l, "counting_semaphore::wait_locked");
}
value_ -= count;
}
示例11: change_received
bool StatelessReader::change_received(CacheChange_t* change, boost::unique_lock<boost::recursive_mutex> &lock)
{
// Only make visible the change if there is not other with bigger sequence number.
// TODO Revisar si no hay que incluirlo.
if(!mp_history->thereIsUpperRecordOf(change->writerGUID, change->sequenceNumber))
{
if(mp_history->received_change(change, 0))
{
if(getListener() != nullptr)
{
lock.unlock();
getListener()->onNewCacheChangeAdded((RTPSReader*)this,change);
lock.lock();
}
mp_history->postSemaphore();
return true;
}
}
return false;
}
示例12: add_new_always
bool add_new_always(std::size_t& added, thread_queue* addfrom,
boost::unique_lock<mutex_type> &lk, bool steal = false)
{
HPX_ASSERT(lk.owns_lock());
#ifdef HPX_HAVE_THREAD_CREATION_AND_CLEANUP_RATES
util::tick_counter tc(add_new_time_);
#endif
if (0 == addfrom->new_tasks_count_.load(boost::memory_order_relaxed))
return false;
// create new threads from pending tasks (if appropriate)
boost::int64_t add_count = -1; // default is no constraint
// if we are desperate (no work in the queues), add some even if the
// map holds more than max_count
if (HPX_LIKELY(max_count_)) {
std::size_t count = thread_map_.size();
if (max_count_ >= count + min_add_new_count) { //-V104
HPX_ASSERT(max_count_ - count <
static_cast<std::size_t>((std::numeric_limits
<boost::int64_t>::max)()));
add_count = static_cast<boost::int64_t>(max_count_ - count);
if (add_count < min_add_new_count)
add_count = min_add_new_count;
if (add_count > max_add_new_count)
add_count = max_add_new_count;
}
else if (work_items_.empty()) {
add_count = min_add_new_count; // add this number of threads
max_count_ += min_add_new_count; // increase max_count //-V101
}
else {
return false;
}
}
std::size_t addednew = add_new(add_count, addfrom, lk, steal);
added += addednew;
return addednew != 0;
}
示例13: add_new_if_possible
bool add_new_if_possible(std::size_t& added, thread_queue* addfrom,
boost::unique_lock<mutex_type> &lk, bool steal = false)
{
HPX_ASSERT(lk.owns_lock());
#ifdef HPX_HAVE_THREAD_CREATION_AND_CLEANUP_RATES
util::tick_counter tc(add_new_time_);
#endif
if (0 == addfrom->new_tasks_count_.load(boost::memory_order_relaxed))
return false;
// create new threads from pending tasks (if appropriate)
boost::int64_t add_count = -1; // default is no constraint
// if the map doesn't hold max_count threads yet add some
// FIXME: why do we have this test? can max_count_ ever be zero?
if (HPX_LIKELY(max_count_)) {
std::size_t count = thread_map_.size();
if (max_count_ >= count + min_add_new_count) { //-V104
HPX_ASSERT(max_count_ - count <
static_cast<std::size_t>((std::numeric_limits
<boost::int64_t>::max)()));
add_count = static_cast<boost::int64_t>(max_count_ - count);
if (add_count < min_add_new_count)
add_count = min_add_new_count;
}
else {
return false;
}
}
std::size_t addednew = add_new(add_count, addfrom, lk, steal);
added += addednew;
return addednew != 0;
}
示例14: assert_owns_lock
void assert_owns_lock(boost::unique_lock<Mutex> const& l, long)
{
HPX_ASSERT(l.owns_lock());
}
示例15: f
void f()
{
#if defined BOOST_THREAD_USES_CHRONO
{
time_point t0 = Clock::now();
#if ! defined(BOOST_NO_CXX11_AUTO_DECLARATIONS)
auto
#else
boost::unique_lock<boost::mutex>
#endif
lk = boost::make_unique_lock(m, boost::try_to_lock);
BOOST_TEST(lk.owns_lock() == false);
time_point t1 = Clock::now();
ns d = t1 - t0 - ms(250);
// This test is spurious as it depends on the time the thread system switches the threads
BOOST_TEST(d < ns(50000000)+ms(1000)); // within 50ms
}
{
time_point t0 = Clock::now();
#if ! defined(BOOST_NO_CXX11_AUTO_DECLARATIONS)
auto
#else
boost::unique_lock<boost::mutex>
#endif
lk = boost::make_unique_lock(m, boost::try_to_lock);
BOOST_TEST(lk.owns_lock() == false);
time_point t1 = Clock::now();
ns d = t1 - t0 - ms(250);
// This test is spurious as it depends on the time the thread system switches the threads
BOOST_TEST(d < ns(50000000)+ms(1000)); // within 50ms
}
{
time_point t0 = Clock::now();
#if ! defined(BOOST_NO_CXX11_AUTO_DECLARATIONS)
auto
#else
boost::unique_lock<boost::mutex>
#endif
lk = boost::make_unique_lock(m, boost::try_to_lock);
BOOST_TEST(lk.owns_lock() == false);
time_point t1 = Clock::now();
ns d = t1 - t0 - ms(250);
// This test is spurious as it depends on the time the thread system switches the threads
BOOST_TEST(d < ns(50000000)+ms(1000)); // within 50ms
}
{
time_point t0 = Clock::now();
while (true)
{
#if ! defined(BOOST_NO_CXX11_AUTO_DECLARATIONS)
auto
#else
boost::unique_lock<boost::mutex>
#endif
lk = boost::make_unique_lock(m, boost::try_to_lock);
if (lk.owns_lock()) break;
}
time_point t1 = Clock::now();
ns d = t1 - t0 - ms(250);
// This test is spurious as it depends on the time the thread system switches the threads
BOOST_TEST(d < ns(50000000)+ms(1000)); // within 50ms
}
#else
// time_point t0 = Clock::now();
// {
// boost::unique_lock<boost::mutex> lk(m, boost::try_to_lock);
// BOOST_TEST(lk.owns_lock() == false);
// }
// {
// boost::unique_lock<boost::mutex> lk(m, boost::try_to_lock);
// BOOST_TEST(lk.owns_lock() == false);
// }
// {
// boost::unique_lock<boost::mutex> lk(m, boost::try_to_lock);
// BOOST_TEST(lk.owns_lock() == false);
// }
while (true)
{
#if ! defined(BOOST_NO_CXX11_AUTO_DECLARATIONS)
auto
#else
boost::unique_lock<boost::mutex>
#endif
lk = boost::make_unique_lock(m, boost::try_to_lock);
if (lk.owns_lock()) break;
}
//time_point t1 = Clock::now();
//ns d = t1 - t0 - ms(250);
// This test is spurious as it depends on the time the thread system switches the threads
//BOOST_TEST(d < ns(50000000)+ms(1000)); // within 50ms
#endif
}