# 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`

  Public Functions

  `DISABLE_COPY`(SpinLock)
  `SpinLock`()
  `~SpinLock`()
  void `lock`()
  void `unlock`()

## class Semaphore

class `paddle::``Semaphore`

  Public Functions

  `Semaphore`(const Semaphore &other)

    Disable copy & assign.

  Semaphore &`operator=`(const Semaphore &&other)
  `Semaphore`(Semaphore &&other)

    Enable move.

  `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().

## class ThreadBarrier

class `paddle::``ThreadBarrier`

  Public Functions

  `DISABLE_COPY`(ThreadBarrier)
  `ThreadBarrier`(int count)

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

  `~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 .

## 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_`