class Poco::Mutex

Overview

A Mutex (mutual exclusion) is a synchronization mechanism used to control access to a shared resource in a concurrent (multithreaded) scenario. Moreā€¦

#include <Mutex.h>

class Mutex: private Poco::MutexImpl
{
public:
    // typedefs

    typedef Poco::ScopedLock<Mutex> ScopedLock;

    // methods

    void
    lock();

    void
    lock(long milliseconds);

    bool
    tryLock();

    bool
    tryLock(long milliseconds);

    void
    unlock();
};

Inherited Members

protected:
    // methods

    void
    lockImpl();

    bool
    tryLockImpl();

    bool
    tryLockImpl(long milliseconds);

    void
    unlockImpl();

Detailed Documentation

A Mutex (mutual exclusion) is a synchronization mechanism used to control access to a shared resource in a concurrent (multithreaded) scenario.

Mutexes are recursive, that is, the same mutex can be locked multiple times by the same thread (but, of course, not by other threads). Using the ScopedLock class is the preferred way to automatically lock and unlock a mutex.

Construction

~Mutex()

destroys the Mutex.

Methods

void
lock()

Locks the mutex.

Blocks if the mutex is held by another thread.

void
lock(long milliseconds)

Locks the mutex.

Blocks up to the given number of milliseconds if the mutex is held by another thread. Throws a TimeoutException if the mutex can not be locked within the given timeout.

Performance Note: On most platforms (including Windows), this member function is implemented using a loop calling (the equivalent of) tryLock() and Thread::sleep(). On POSIX platforms that support pthread_mutex_timedlock(), this is used.

bool
tryLock()

Tries to lock the mutex.

Returns false immediately if the mutex is already held by another thread. Returns true if the mutex was successfully locked.

bool
tryLock(long milliseconds)

Locks the mutex.

Blocks up to the given number of milliseconds if the mutex is held by another thread. Returns true if the mutex was successfully locked.

Performance Note: On most platforms (including Windows), this member function is implemented using a loop calling (the equivalent of) tryLock() and Thread::sleep(). On POSIX platforms that support pthread_mutex_timedlock(), this is used.

void
unlock()

Unlocks the mutex so that it can be acquired by other threads.