|
cppthread 1.1.16
C++ Thread Library
|
A mutex object to ensures atomicity. More...

Public Types | |
| typedef std::vector< mutex > | direct_vector_t |
| A vector of mutexes. | |
| typedef std::shared_ptr< mutex > | pointer_t |
| Shared pointer to a mutex. | |
| typedef std::vector< pointer_t > | vector_t |
| A vector of mutexes. | |
Public Member Functions | |
| mutex () | |
| An inter-thread mutex to ensure unicity of execution. | |
| mutex (mutex const &rhs)=delete | |
| ~mutex () | |
| Clean up a mutex object. | |
| void | broadcast () |
| Broadcast a mutex signal. | |
| bool | dated_wait (std::uint64_t const date) |
| Wait on a mutex until the specified date. | |
| bool | dated_wait (timespec const &date) |
| Wait on a mutex until the specified date. | |
| void | lock () |
| Lock a mutex. | |
| mutex & | operator= (mutex const &rhs)=delete |
| void | safe_broadcast () |
| Broadcast a mutex signal. | |
| void | safe_signal () |
| Signal a mutex. | |
| void | signal () |
| Signal at least one mutex. | |
| bool | timed_wait (std::uint64_t const usec) |
| Wait on a mutex condition with a time limit. | |
| bool | timed_wait (timespec const &nsec) |
| Wait on a mutex condition with a time limit. | |
| bool | try_lock () |
| Try locking the mutex. | |
| void | unlock () |
| Unlock a mutex. | |
| void | wait () |
| Wait on a mutex condition. | |
Private Attributes | |
| std::shared_ptr< detail::mutex_impl > | f_impl |
| The pthread mutex implementation. | |
| std::uint32_t | f_reference_count = 0 |
| The lock reference count. | |
This class is used by threads when some data accessed by more than one thread is about to be accessed. In most cases it is used with the guard class so it is safe even in the event an exception is raised.
The mutex also includes a condition variable which can be signaled using the signal() function. This wakes threads that are currently waiting on the condition with one of the wait() functions.
f_impl, would allow for copies, it's still not a good idea.) | cppthread::mutex::mutex | ( | ) |
The mutex object is used to lock part of the code that needs to be run by only one thread at a time. This is also called a critical section and a memory barrier.
In most cases one uses the guard object to temporarily lock the mutex using the FIFO to help ensure the mutex gets unlocked as required in the event an exception occurs.
The lock can be tried to see whether another thread already has the lock and fail if so. See the try_lock() function.
The class also includes a condition in order to send signals and wait on signals. There are two ways to send signals and three ways to wait. Note that to call any one of the wait functions you must first have the mutex locked, what otherwise happens is undefined.
If you need a FIFO of messages between your threads, look at the snap_fifo template.
| cppthread_exception_invalid_error | If any one of the initialization functions fails, this exception is raised. The function also logs the error. |
Definition at line 229 of file mutex.cpp.
References cppthread::end(), f_impl, and cppthread::log.

| cppthread::mutex::~mutex | ( | ) |
This function ensures that the mutex object is cleaned up, which means the mutex and conditions get destroyed.
This destructor verifies that the mutex is not currently locked. A locked mutex can't be destroyed. If still locked, then an error is sent to the logger and the function calls exit(1).
Definition at line 319 of file mutex.cpp.
References cppthread::end(), f_impl, f_reference_count, and cppthread::log.

| void cppthread::mutex::broadcast | ( | ) |
Our mutexes include a condition that get signaled by calling this function. This function actually signals all the threads that are currently listening to the mutex signal. The order in which the threads get awaken is unspecified.
The function ensures that the mutex is locked before broadcasting the signal so you do not have to lock the mutex yourself.
| cppthread_exception_invalid_error | If one of the pthread system functions return an error, the function raises this exception. |
Definition at line 838 of file mutex.cpp.
References cppthread::end(), f_impl, lock(), and cppthread::log.
Referenced by cppthread::fifo< T >::done(), and cppthread::fifo< T >::pop_front().


| bool cppthread::mutex::dated_wait | ( | std::uint64_t const | usec | ) |
This function waits on the mutex condition to be signaled up until the specified date is passed.
| cppthread_exception_mutex_failed_error | This exception is raised whenever the thread wait function fails. |
| [in] | usec | The date when the mutex times out in microseconds. |
Definition at line 642 of file mutex.cpp.
References dated_wait().
Referenced by dated_wait().


| bool cppthread::mutex::dated_wait | ( | timespec const & | date | ) |
This function waits on the mutex condition to be signaled up until the specified date is passed.
| cppthread_exception_mutex_failed_error | This exception is raised whenever the thread wait function fails. |
| [in] | date | The date when the mutex times out in nanoseconds. |
Definition at line 668 of file mutex.cpp.
References cppthread::end(), f_impl, and cppthread::log.

| void cppthread::mutex::lock | ( | ) |
This function locks the mutex. The function waits until the mutex is available if it is not currently available. To avoid waiting one may want to use the try_lock() function instead.
Although the function cannot fail, the call can lock up a process if two or more mutexes are used and another thread is already waiting on this process.
| cppthread_exception_invalid_error | If the lock fails, this exception is raised. |
Definition at line 369 of file mutex.cpp.
References cppthread::end(), f_impl, f_reference_count, and cppthread::log.
Referenced by cppthread::guard::guard(), broadcast(), cppthread::fifo< T >::byte_size(), cppthread::fifo< T >::clear(), cppthread::fifo< T >::done(), cppthread::fifo< T >::empty(), cppthread::fifo< T >::is_done(), cppthread::guard::is_locked(), cppthread::guard::lock(), cppthread::fifo< T >::pop_front(), cppthread::fifo< T >::push_back(), safe_broadcast(), safe_signal(), and cppthread::fifo< T >::size().


| void cppthread::mutex::safe_broadcast | ( | ) |
Our mutexes include a condition that get signaled by calling this function. This function actually signals all the threads that are currently listening to the mutex signal. The order in which the threads get awaken is unspecified.
The function ensures that the mutex is locked before broadcasting the signal so you do not have to lock the mutex yourself. Note that is not required to lock a mutex before broadcasting a signal. The effects are similar.
| cppthread_exception_invalid_error | If one of the pthread system functions return an error, the function raises this exception. |
Definition at line 799 of file mutex.cpp.
References cppthread::end(), f_impl, lock(), and cppthread::log.

| void cppthread::mutex::safe_signal | ( | ) |
Our mutexes include a condition that get signaled by calling this function. This function wakes up one listening thread.
The function ensures that the mutex is locked before sending the signal so you do not have to lock the mutex yourself.
| cppthread_exception_invalid_error | If one of the pthread system functions return an error, the function raises this exception. |
Definition at line 765 of file mutex.cpp.
References cppthread::end(), f_impl, lock(), and cppthread::log.

| void cppthread::mutex::signal | ( | ) |
Our mutexes include a condition that get signaled by calling this function. This function wakes up one or more listening threads.
The function does not lock the mutext before sending the signal. This is useful if you already are in a guarded block or you do not mind waking up more than one thread as a result of the call.
| cppthread_exception_invalid_error | If one of the pthread system functions return an error, the function raises this exception. |
Definition at line 737 of file mutex.cpp.
References cppthread::end(), f_impl, and cppthread::log.
Referenced by cppthread::thread::internal_thread(), and cppthread::fifo< T >::push_back().


| bool cppthread::mutex::timed_wait | ( | std::uint64_t const | usecs | ) |
At times it is useful to wait on a mutex to become available without polling the mutex, but only for some time. This function waits for the number of specified micro seconds. The function returns early if the condition was triggered. Otherwise it waits until the specified number of micro seconds elapsed and then returns.
| cppthread_exception_system_error | This exception is raised if a function returns an unexpected error. |
| cppthread_exception_mutex_failed_error | This exception is raised when the mutex wait function fails. |
| [in] | usecs | The maximum number of micro seconds to wait until you receive the signal. |
Definition at line 549 of file mutex.cpp.
References timed_wait().
Referenced by cppthread::fifo< T >::pop_front(), and timed_wait().


| bool cppthread::mutex::timed_wait | ( | timespec const & | nsecs | ) |
At times it is useful to wait on a mutex to become available without polling the mutex, but only for some time. This function waits for the number of specified nano seconds. The function returns early if the condition was triggered. Otherwise it waits until the specified number of nano seconds elapsed and then returns.
| cppthread_exception_system_error | This exception is raised if a function returns an unexpected error. |
| cppthread_exception_mutex_failed_error | This exception is raised when the mutex wait function fails. |
| [in] | nsecs | The maximum number of nano seconds to wait until you receive the signal. |
Definition at line 583 of file mutex.cpp.
References cppthread::end(), f_impl, and cppthread::log.

| bool cppthread::mutex::try_lock | ( | ) |
This function tries locking the mutex. If the mutex cannot be locked because another process already locked it, then the function returns immediately with false.
| cppthread_exception_invalid_error | If the lock fails, this exception is raised. |
Definition at line 400 of file mutex.cpp.
References cppthread::end(), f_impl, f_reference_count, and cppthread::log.

| void cppthread::mutex::unlock | ( | ) |
This function unlock the specified mutex. The function must be called exactly once per call to the lock() function, or successful call to the try_lock() function.
The unlock never waits.
| cppthread_exception_invalid_error | If the unlock fails, this exception is raised. |
| cppthread_exception_not_locked_error | If the function is called too many times, then the lock count is going to be zero and this exception will be raised. |
Definition at line 443 of file mutex.cpp.
References cppthread::end(), f_impl, f_reference_count, and cppthread::log.
Referenced by cppthread::guard::is_locked(), cppthread::guard::lock(), and cppthread::guard::unlock().


| void cppthread::mutex::wait | ( | ) |
At times it is useful to wait on a mutex to become available without polling the mutex (which uselessly wastes precious processing time.) This function can be used to wait on a mutex condition.
This version of the wait() blocks until a signal is received.
| cppthread_exception_not_locked_once_error | This exception is raised if the reference count is not exactly 1. In other words, the mutex must be locked by the caller but only one time. |
| cppthread_exception_mutex_failed_error | This exception is raised in the event the conditional wait fails. |
Definition at line 494 of file mutex.cpp.
References cppthread::end(), f_impl, and cppthread::log.
Referenced by cppthread::fifo< T >::pop_front(), and cppthread::thread::start().


|
private |
This variable member holds the actual pthread mutex. The mutex implementation manages this field as required.
Definition at line 82 of file mutex.h.
Referenced by mutex(), ~mutex(), broadcast(), dated_wait(), lock(), safe_broadcast(), safe_signal(), signal(), timed_wait(), try_lock(), unlock(), and wait().
|
private |
This document is part of the Snap! Websites Project.
Copyright by Made to Order Software Corp.