C++ std::mutex std::lock_guard

mutex:http://www.cplusplus.com/reference/mutex/mutex/?kw=mutex

lock_guard:http://www.cplusplus.com/reference/mutex/lock_guard/?kw=lock_guard

class

std::mutex

class mutex;

Mutex class

mutex is a lockable object that is designed to signal when critical sections of code need exclusive access, preventing other threads with the same protection from executing concurrently and access the same memory locations.

mutex objects provide exclusive ownership and do not support recursivity (i.e., a thread shall not lock a mutex it already owns) -- see recursive_mutex for an alternative class that does.

It is guaranteed to be a standard-layout class.

 

Member types

member type description
native_handle_type Type returned by native_handle (only defined if library implementation supports it)

 

Member functions

(constructor)

Construct mutex (public member function )

lock

Lock mutex (public member function )

try_lock

Lock mutex if not locked (public member function )

unlock

Unlock mutex (public member function )

native_handle

Get native handle (public member function )

 

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
// mutex example
#include        // std::cout
#include          // std::thread
#include           // std::mutex

std::mutex mtx;           // mutex for critical section

void print_block (int n, char c) {
  // critical section (exclusive access to std::cout signaled by locking mtx):
  mtx.lock();
  for (int i=0; i

Edit & Run



Possible output (order of lines may vary, but characters are never mixed):

**************************************************
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

 

class template

std::lock_guard

template  class lock_guard;

Lock guard

lock guard is an object that manages a mutex object by keeping it always locked.

On construction, the mutex object is locked by the calling thread, and on destruction, the mutex is unlocked. It is the simplest lock, and is specially useful as an object with automatic duration that lasts until the end of its context. In this way, it guarantees the mutex object is properly unlocked in case an exception is thrown.

Note though that the lock_guard object does not manage the lifetime of the mutex object in any way: the duration of the mutex object shall extend at least until the destruction of the lock_guard that locks it.

 

Template parameters

Mutex

mutex-like type.
It shall be a basic lockable type, such as mutex (see BasicLockable for requirements).

 

Member types

member type definition description
mutex_type The template parameter (Mutex) The managed mutex object type

 

Member functions

(constructor)

Construct lock_guard (public member function )

(destructor)

Destroy lock_guard (unlocking mutex) (public member function )


 

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
31
32
33
34
35
// lock_guard example
#include        // std::cout
#include          // std::thread
#include           // std::mutex, std::lock_guard
#include       // std::logic_error

std::mutex mtx;

void print_even (int x) {
  if (x%2==0) std::cout << x << " is even\n";
  else throw (std::logic_error("not even"));
}

void print_thread_id (int id) {
  try {
    // using a local lock_guard to lock mtx guarantees unlocking on destruction / exception:
    std::lock_guard lck (mtx);
    print_even(id);
  }
  catch (std::logic_error&) {
    std::cout << "[exception caught]\n";
  }
}

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

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

  return 0;
}

Edit & Run



Possible output:

[exception caught]
2 is even
[exception caught]
4 is even
[exception caught]
6 is even
[exception caught]
8 is even
[exception caught]
10 is even


The order of the lines printed may be different.

你可能感兴趣的:(C++,VC)