A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://cplusplus.com/reference/mutex/unique_lock/try_lock_for/ below:

public member function

<mutex>

std::unique_lock::try_lock_for
template <class Rep, class Period>  bool try_lock_for (const chrono::duration<Rep,Period>& rel_time);

Try to lock mutex during time span

Calls member try_lock_for of the managed timed mutex object, and uses the returned value to set the owning state.

If the owning state is already true before the call, or if the object currently manages no mutex object, the function throws a system_error exception.



Parameters
rel_time
The maximum time span during which the thread will block, waiting to acquire a lock.
duration is an object that represents a specific relative time.

Return valuetrue if the function succeeds in locking the managed timed mutex object.
false otherwise.

Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// unique_lock::try_lock_for example
#include <iostream>       // std::cout
#include <chrono>         // std::chrono::milliseconds
#include <thread>         // std::thread
#include <mutex>          // std::timed_mutex, std::unique_lock, std::defer_lock

std::timed_mutex mtx;

void fireworks () {
  std::unique_lock<std::timed_mutex> lck(mtx,std::defer_lock);
  // waiting to get a lock: each thread prints "-" every 200ms:
  while (!lck.try_lock_for(std::chrono::milliseconds(200))) {
    std::cout << "-";
  }
  // got a lock! - wait for 1s, then this thread prints "*"
  std::this_thread::sleep_for(std::chrono::milliseconds(1000));
  std::cout << "*\n";
}

int main ()
{
  std::thread threads[10];
  // spawn 10 threads:
  for (int i=0; i<10; ++i)
    threads[i] = std::thread(fireworks);

  for (auto& th : threads) th.join();

  return 0;
}

Possible output (after around 10s, length of lines may vary slightly):
------------------------------------*
----------------------------------------*
-----------------------------------*
------------------------------*
-------------------------*
--------------------*
---------------*
----------*
-----*
*


Data races The unique_lock object is accessed/modified.
The managed timed mutex object is accessed/modified as an atomic operation (causing no data races).

Exception safetyBasic guarantee: if an exception is thrown by this member function, the unique_lock object is left in a valid state.

If the call fails, a system_error exception is thrown:


An exception is also thrown if the call to try_lock_for on the managed timed mutex object fails, and on any other condition reported with such mechanism by the library implementation.

See also
unique_lock::lock
Lock mutex (public member function)
unique_lock::try_lock
Lock mutex if not locked (public member function)
unique_lock::try_lock_until
Try to lock mutex until time point (public member function)
unique_lock::unlock
Unlock mutex (public member function)

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4