A RetroSearch Logo

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

Search Query:

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

public member function

<mutex>

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

Try to lock for time span

Attempts to lock the timed_mutex, blocking for rel_time at most:

All lock and unlock operations on the timed_mutex follow a single total order, with all visible effects synchronized between the lock operations and previous unlock operations on the same object.

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 timed_mutex for the thread.
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
// timed_mutex::try_lock_for example
#include <iostream>       // std::cout
#include <chrono>         // std::chrono::milliseconds
#include <thread>         // std::thread
#include <mutex>          // std::timed_mutex

std::timed_mutex mtx;

void fireworks () {
  // waiting to get a lock: each thread prints "-" every 200ms:
  while (!mtx.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";
  mtx.unlock();
}

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 10 seconds, length of lines may vary slightly):
------------------------------------*
----------------------------------------*
-----------------------------------*
------------------------------*
-------------------------*
--------------------*
---------------*
----------*
-----*
*


Data races The timed_mutex object is accessed/modified as an atomic operation (causes no data races).

Exception safety If the timed_mutex is currently locked by the calling thread, it causes undefined behavior.
Otherwise, it offers the same level of guarantee as the operations on the duration object (for the type instantiations in <chrono>, this is a no-throw guarantee).

See also
timed_mutex::lock
Lock timed mutex (public member function)
timed_mutex::try_lock
Lock timed mutex if not locked (public member function)
timed_mutex::try_lock_until
Try to lock until time point (public member function)
timed_mutex::unlock
Unlock timed mutex (public member function)
sleep_for
Sleep for time span (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