cppthread 1.1.16
C++ Thread Library
mutex.h
Go to the documentation of this file.
1// Copyright (c) 2013-2025 Made to Order Software Corp. All Rights Reserved
2//
3// https://snapwebsites.org/project/cppthread
4// contact@m2osw.com
5//
6// This program is free software; you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation; either version 2 of the License, or
9// (at your option) any later version.
10//
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15//
16// You should have received a copy of the GNU General Public License along
17// with this program; if not, write to the Free Software Foundation, Inc.,
18// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19#pragma once
20
33// C++
34//
35#include <cstdint>
36#include <memory>
37#include <vector>
38
39
40
41namespace cppthread
42{
43
44
45
46namespace detail
47{
48class mutex_impl;
49}
50
51
52// a mutex to ensure single threaded work
53//
54class mutex
55{
56public:
57 typedef std::shared_ptr<mutex> pointer_t;
58 typedef std::vector<pointer_t> vector_t;
59 typedef std::vector<mutex> direct_vector_t;
60
61 mutex();
62 mutex(mutex const & rhs) = delete;
63 ~mutex();
64
65 mutex & operator = (mutex const & rhs) = delete;
66
67 void lock();
68 bool try_lock();
69 void unlock();
70 void wait();
71 bool timed_wait(std::uint64_t const usec);
72 bool timed_wait(timespec const & nsec);
73 bool dated_wait(std::uint64_t const date);
74 bool dated_wait(timespec const & date);
75 void signal();
76 void safe_signal();
77 void broadcast();
78 void safe_broadcast();
79
80private:
81 std::shared_ptr<detail::mutex_impl>
83
84 std::uint32_t f_reference_count = 0;
85};
86
87
88
89extern mutex * g_system_mutex;
90
91
92
93} // namespace cppthread
94// vim: ts=4 sw=4 et
A mutex object to ensures atomicity.
Definition mutex.h:55
std::vector< pointer_t > vector_t
A vector of mutexes.
Definition mutex.h:58
void lock()
Lock a mutex.
Definition mutex.cpp:369
std::shared_ptr< mutex > pointer_t
Shared pointer to a mutex.
Definition mutex.h:57
std::vector< mutex > direct_vector_t
A vector of mutexes.
Definition mutex.h:59
bool dated_wait(std::uint64_t const date)
Wait on a mutex until the specified date.
Definition mutex.cpp:642
bool timed_wait(std::uint64_t const usec)
Wait on a mutex condition with a time limit.
Definition mutex.cpp:549
void wait()
Wait on a mutex condition.
Definition mutex.cpp:494
void safe_signal()
Signal a mutex.
Definition mutex.cpp:765
mutex()
An inter-thread mutex to ensure unicity of execution.
Definition mutex.cpp:229
void unlock()
Unlock a mutex.
Definition mutex.cpp:443
void signal()
Signal at least one mutex.
Definition mutex.cpp:737
std::shared_ptr< detail::mutex_impl > f_impl
The pthread mutex implementation.
Definition mutex.h:82
std::uint32_t f_reference_count
The lock reference count.
Definition mutex.h:84
void broadcast()
Broadcast a mutex signal.
Definition mutex.cpp:838
void safe_broadcast()
Broadcast a mutex signal.
Definition mutex.cpp:799
bool try_lock()
Try locking the mutex.
Definition mutex.cpp:400
~mutex()
Clean up a mutex object.
Definition mutex.cpp:319
mutex * g_system_mutex
The system mutex.
Definition mutex.cpp:888

This document is part of the Snap! Websites Project.

Copyright by Made to Order Software Corp.