Lock

class RWLock

class paddle::RWLock

A simple read-write lock. The RWlock allows a number of readers or at most one writer at any point in time. The RWlock disable copy.

Lock:

Use lock() to lock on write mode, no other thread can get it until unlock.

Use lock_shared() to lock on read mode, other thread can get it by using the same method lock_shared().

Unlock:

Use unlock() to unlock the lock.

Public Functions

RWLock()
~RWLock()
RWLock(const RWLock&)
RWLock &operator=(const RWLock&)
void lock()

lock on write mode.

Note
the method will block the thread, if failed to get the lock.

void lock_shared()

lock on read mode.

Note
if another thread is writing, it can’t get the lock, and will block the thread.

void unlock()

Protected Attributes

pthread_rwlock_t rwlock_

class ReadLockGuard

class paddle::ReadLockGuard

The ReadLockGuard is a read mode RWLock using RAII management mechanism.

Public Functions

ReadLockGuard(RWLock &rwlock)

Construct Function. Lock on rwlock in read mode.

~ReadLockGuard()

Destruct Function.

Note
This method just unlock the read mode rwlock, won’t destroy the lock.

Protected Attributes

RWLock *rwlock_

class SpinLock

class paddle::SpinLock

A simple wrapper for spin lock. The lock() method of SpinLock is busy-waiting which means it will keep trying to lock until lock on successfully. The SpinLock disable copy.

Public Functions

SpinLock()
~SpinLock()
SpinLock(const SpinLock&)
SpinLock &operator=(const SpinLock&)
void lock()
void unlock()

Protected Attributes

pthread_spinlock_t lock_
char padding_[64-sizeof(pthread_spinlock_t)]

class Semaphore

class paddle::Semaphore

A simple wapper of semaphore which can only be shared in the same process.

Public Functions

Semaphore(int initValue = 0)

Construct Function.

Parameters
  • initValue -

    the initial value of the semaphore, default 0.

~Semaphore()
bool timeWait(struct timespec *ts)

The same as wait(), except if the decrement can not be performed until ts return false install of blocking.

Return
ture if the decrement proceeds before ts, else return false.
Parameters
  • ts -

    an absolute timeout in seconds and nanoseconds since the Epoch 1970-01-01 00:00:00 +0000(UTC).

void wait()

decrement the semaphore. If the semaphore’s value is 0, then call blocks.

void post()

increment the semaphore. If the semaphore’s value greater than 0, wake up a thread blocked in wait().

Protected Attributes

sem_t sem_

class ThreadBarrier

class paddle::ThreadBarrier

A simple wrapper of thread barrier. The ThreadBarrier disable copy.

Public Functions

ThreadBarrier(int count)

Construct Function. Initialize the barrier should wait for count threads in wait().

~ThreadBarrier()
ThreadBarrier(const ThreadBarrier&)
ThreadBarrier &operator=(const ThreadBarrier&)
void wait()

If there were count - 1 threads waiting before, then wake up all the count - 1 threads and continue run together. Else block the thread until waked by other thread .

Protected Attributes

pthread_barrier_t barrier_

class LockedCondition

class paddle::LockedCondition

A wrapper for condition variable with mutex.

Inherits from condition_variable

Public Functions

template <class Op>
void notify_one(Op op)

execute op and notify one thread which was blocked.

Parameters
  • op -

    a thread can do something in op before notify.

template <class Op>
void notify_all(Op op)

execute op and notify all the threads which were blocked.

Parameters
  • op -

    a thread can do something in op before notify.

template <class Predicate>
void wait(Predicate pred)

wait until pred return ture.

Note
pred shall not apply any non-constant function through the dereferenced iterator.
Template Parameters
  • Predicate -

    c++ concepts, describes a function object that takes a single iterator argument that is dereferenced and used to return a value testable as a bool.

std::mutex *mutex()

get mutex.

Protected Attributes

std::mutex mutex_