本文整理汇总了C++中std::mutex::try_lock方法的典型用法代码示例。如果您正苦于以下问题:C++ mutex::try_lock方法的具体用法?C++ mutex::try_lock怎么用?C++ mutex::try_lock使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类std::mutex
的用法示例。
在下文中一共展示了mutex::try_lock方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: customer
void customer()
{
std::unique_lock<std::mutex> graud(mut);
if(mut.try_lock())
{
std::cout << "mutex unlocked after unique_lock" <<std::endl;
}else{
std::cout <<"mutex locked after unique_lock" <<std::endl;
}
while(flag == 0)
{
std::cout << "wait........." <<std::endl;
cond.wait(graud);
}
if(mut.try_lock())
{
std::cout << "mutex unlocked after wait" <<std::endl;
}else{
std::cout <<"mutex locked after wait" <<std::endl;
}
std::cout << "flag = " << flag << std::endl;
}
示例2: remove
/**
* To remove from the list, we need to keep a pointer to the previous
* node, too. Note that this is much easier on account of us having a
* sentinel
*/
void remove(int val)
{
while (true) {
if (linkMtx.try_lock()) {
// find the node whose val matches the request
Node* prev = sentinel;
Node* curr = prev->next;
while (curr != NULL) {
// if we find the node, disconnect it and end the search
if (curr->val == val) {
prev->next = curr->next;
// delete curr...
free(curr);
break;
}
else if (curr->val > val) {
// this means the search failed
break;
}
// advance one node
prev = curr;
curr = prev->next;
}
linkMtx.unlock();
return;
}
}
}
示例3: deliver_new_event
void deliver_new_event(std::size_t, const void* inbnd) {
#ifndef NDEBUG
bool success = assert_single_threaded.try_lock();
assert(success);
AtScopeEnd ase{[&]{assert_single_threaded.unlock();}};
#endif
//std::cout << "received message" << std::endl;
if (on_first_message){
//std::cout << "expected this message is size_t" << std::endl;
recv.first = *((int*)(inbnd));
on_first_message = false;
event_fd.notify();
//async_tick(c);
}
else{
//std::cout << "expected this message is vector" << std::endl;
recv.second = from_bytes<std::vector<unsigned char> >(nullptr,((char*) inbnd));
{
stringstream ss;
ss << "received " << recv.second->size() << " element vector" << std::endl;
//std::cout << ss.str();
}
on_first_message = true;
event_fd.notify();
//async_tick(c);
}
}
示例4: insert
/**
* insert method; find the right place in the list, add val so that it is
* in sorted order; if val is already in the list, exit without inserting
*/
void insert(int val)
{
while (true) {
if (linkMtx.try_lock()) {
// traverse the list to find the insertion point
Node* prev =sentinel;
Node* curr = prev->next;
while (curr != NULL) {
if (curr->val >= val)
break;
prev = curr;
curr = prev->next;
}
// now insert new_node between prev and curr
//
// NB: if the test fails, it means we quit the above loop on account
// of /finding/ val, in which case we just exit
if (!curr || ((curr->val) > val)) {
// create the new node
Node* i = (Node*)malloc(sizeof(Node));
i->val = val;
i->next = curr;
// insert it
prev->next = i;
}
linkMtx.unlock();
return;
}
}
}
示例5: attempt_10k_incresses
void attempt_10k_incresses() {
for (auto i = 0; i < 10000; ++i)
if (mtx.try_lock()) { // only increase if currently not locked
++counter;
mtx.unlock();
}
}
示例6: SimulateClient
void SimulateClient(InternetCafe* internetCafe, std::vector<Client*>& clients, unsigned int clientId, std::mutex& mtx)
{
Client* curClient = nullptr;
curClient = clients[clientId];
bool clientIsRunning = true;
if (curClient)
{
while (clientIsRunning)
{
if (mtx.try_lock())
{
if (internetCafe->RequestComputer(curClient->GetId(), curClient->GetMoney(), clientIsRunning))
{
curClient->SetMoney(curClient->GetMoney() - internetCafe->GetHireCost());
}
mtx.unlock();
}
std::this_thread::sleep_for(std::chrono::duration_cast<std::chrono::milliseconds>
(std::chrono::duration<double, std::ratio<1, 1000>>(TIMESTEP)));
}
std::cout << "User " << curClient->GetId() << " has exited simulation" << std::endl;
}
}
示例7: run
// the main device method
virtual void run() {
if(!opened) {
scene->addItem(item);
// lock the image viewer
img_view_mutex.lock();
// update the pixmap and unlock the image viewer
show_image();
// open a window to show the QGraphicsScene
view->show();
// set the flag to true
opened = true;
} else {
if (img_view_mutex.try_lock()) {
// a new thread!
std::thread show_image_thread(&ImageViewerDevice::show_image, this);
// leave it alone
show_image_thread.detach();
}
}
}
示例8: wait_for_all_entering_ep
/**
* I hope everyone come inside, and everyone know
* how many had been here
*/
int wait_for_all_entering_ep()
{
static std::atomic<bool> wait_finish;
static std::atomic<int> thread_inside_count;
static std::atomic<int> saved_thread_inside_count;
static std::mutex mtx;
wait_finish = false;
thread_inside_count++;
if (mtx.try_lock())
{
while (thread_inside_count != get_expected_thread_count())
{
// Well, furiosly infinit looping until all the threads runnin in the
// kernel are trapped here
}
saved_thread_inside_count = thread_inside_count.load();
wait_finish = true;
mtx.unlock();
}
else
{
while (wait_finish == false);
}
thread_inside_count--;
return saved_thread_inside_count.load();
}
示例9: try_lock
bool try_lock()
{
check_for_hierarchy_violation();
if(!internal_mutex.try_lock())
return false;
update_hierarchy_value();
return true;
}
示例10: attempt_10k_inc
void attempt_10k_inc() {
for (int i=0; i<10000; ++i) {
if (mtx.try_lock()) {
// only increase if lock can be got successfully
++counter;
mtx.unlock();
}
}
}
示例11: ConnectOnInput
void ConnectOnInput(int wiimote_number)
{
if (!g_wiimotes_mutex.try_lock())
return;
if (g_wiimotes[wiimote_number])
g_wiimotes[wiimote_number]->ConnectOnInput();
g_wiimotes_mutex.unlock();
}
示例12: try_lock
bool try_lock() {
if (_mutex.try_lock())
{
_isLocked = true;
return true;
}
else {
_isLocked = false;
return false;
}
};
示例13: iDroneFSM
void iDroneFSM() {
while (run) {
if (navLock.try_lock()) {
fsm.act(model);
navLock.unlock();
}
//std::this_thread::sleep_for(std::chrono::seconds(2));
}
}
示例14: threadfunction_mutex_try_lock
void threadfunction_mutex_try_lock(int arg)
{
long long count = 0;
while(true) {
if(coutLock.try_lock()) {
cout << "mutex try_lock: " << arg << ", this_thread::get_id=" << this_thread::get_id() << ", tried " << count << " times before we attained the lock" << "\n";
coutLock.unlock();
break;
} else { // lock is busy, do something else. Let's count the number of times we tried.
count++;
}
}
}
示例15: thread_function_increase
void thread_function_increase()
{
for (int i=0; i<3; i++)
{
if(g_counter_mutex.try_lock())
//g_counter_mutex.lock();
{
++g_counter;
cout << this_thread::get_id() << ": " << i << endl;
g_counter_mutex.unlock();
this_thread::sleep_for(std::chrono::seconds(2));
}
}
}