Mutex

Overview

mutex implements the mutual exclusion locks. Although libcsp provides the mutex synchronization primitives, try to avoid using it cause it may hurt the performance.

Golang: Do not communicate by sharing memory; instead, share memory by communicating.

Index

csp_mutex_t


csp_mutex_t defines the type of mutual exclusion locks.

Example:

csp_mutex_t mutex;

csp_mutex_init(mutex)


csp_mutex_init initializes the mutex.

Example:

csp_mutex_init(&mutex);

csp_mutex_try_lock(mutex)


csp_mutex_try_lock tries to lock the mutex. It returns true if success, otherwise false.

Example:

if (csp_mutex_try_lock(&mutex)) {
  // Do something...
  csp_mutex_unlock(&mutex);
}

csp_mutex_lock(mutex)


csp_mutex_lock locks the mutex. It will spin if the mutex has been locked.

Example:

csp_mutex_lock(&mutex);

csp_mutex_unlock(mutex)


csp_mutex_unlock unlocks the mutex.

Example:

csp_mutex_unlock(&mutex);