Mutex Lock

include: co/co.h.


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


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.


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


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


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



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.


  • 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);

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