Mutex Lock

include: co/co.h.

#co::mutex

Starting from v3.0.1, co::mutex can be used in coroutines and non-coroutines.

#constructor

1. mutex();
2. mutex(mutex&& m);
3. mutex(const mutex& m);
  • 1, default constructor.
  • 2, move constructor.
  • 3, copy constructor, only increases the internal reference count by 1.

#lock

void lock() const;
  • Acquire the lock, will block until the lock is acquired.

#try_lock

bool try_lock() const;
  • Acquire the lock, will not block, return true when the lock is successfully acquired, otherwise return false.

#unlock

void unlock() const;
  • Release the lock, usually called by the coroutine or thread that previously obtained the lock.

#co::mutex_guard

#constructor

explicit mutex_guard(co::mutex& m);
explicit mutex_guard(co::mutex* m);
  • Call m.lock() to acquire the lock, the parameter m is a reference or pointer of the co::mutex class.

#destructor

~mutex_guard();
  • Release the lock acquired in the constructor.

#Code Example

#include "co/co.h"
#include "co/cout.h"

co::mutex g_m;
int g_v = 0;

void f() {
     co::mutex_guard g(g_m);
     ++g_v;
}

int main(int argc, char** argv) {
     flag::parse(argc, argv);
     go(f);
     go(f);
     f();
     f();
     co::sleep(100);
     co::print("g_v: ", g_v);
     return 0;
}