|
|
|||
File indexing completed on 2026-06-02 08:58:14
0001 //////////////////////////////////////////////////////////// 0002 // 0003 // SFML - Simple and Fast Multimedia Library 0004 // Copyright (C) 2007-2023 Laurent Gomila (laurent@sfml-dev.org) 0005 // 0006 // This software is provided 'as-is', without any express or implied warranty. 0007 // In no event will the authors be held liable for any damages arising from the use of this software. 0008 // 0009 // Permission is granted to anyone to use this software for any purpose, 0010 // including commercial applications, and to alter it and redistribute it freely, 0011 // subject to the following restrictions: 0012 // 0013 // 1. The origin of this software must not be misrepresented; 0014 // you must not claim that you wrote the original software. 0015 // If you use this software in a product, an acknowledgment 0016 // in the product documentation would be appreciated but is not required. 0017 // 0018 // 2. Altered source versions must be plainly marked as such, 0019 // and must not be misrepresented as being the original software. 0020 // 0021 // 3. This notice may not be removed or altered from any source distribution. 0022 // 0023 //////////////////////////////////////////////////////////// 0024 0025 #ifndef SFML_MUTEX_HPP 0026 #define SFML_MUTEX_HPP 0027 0028 //////////////////////////////////////////////////////////// 0029 // Headers 0030 //////////////////////////////////////////////////////////// 0031 #include <SFML/System/Export.hpp> 0032 #include <SFML/System/NonCopyable.hpp> 0033 0034 0035 namespace sf 0036 { 0037 namespace priv 0038 { 0039 class MutexImpl; 0040 } 0041 0042 //////////////////////////////////////////////////////////// 0043 /// \brief Blocks concurrent access to shared resources 0044 /// from multiple threads 0045 /// 0046 //////////////////////////////////////////////////////////// 0047 class SFML_SYSTEM_API Mutex : NonCopyable 0048 { 0049 public: 0050 0051 //////////////////////////////////////////////////////////// 0052 /// \brief Default constructor 0053 /// 0054 //////////////////////////////////////////////////////////// 0055 Mutex(); 0056 0057 //////////////////////////////////////////////////////////// 0058 /// \brief Destructor 0059 /// 0060 //////////////////////////////////////////////////////////// 0061 ~Mutex(); 0062 0063 //////////////////////////////////////////////////////////// 0064 /// \brief Lock the mutex 0065 /// 0066 /// If the mutex is already locked in another thread, 0067 /// this call will block the execution until the mutex 0068 /// is released. 0069 /// 0070 /// \see unlock 0071 /// 0072 //////////////////////////////////////////////////////////// 0073 void lock(); 0074 0075 //////////////////////////////////////////////////////////// 0076 /// \brief Unlock the mutex 0077 /// 0078 /// \see lock 0079 /// 0080 //////////////////////////////////////////////////////////// 0081 void unlock(); 0082 0083 private: 0084 0085 //////////////////////////////////////////////////////////// 0086 // Member data 0087 //////////////////////////////////////////////////////////// 0088 priv::MutexImpl* m_mutexImpl; //!< OS-specific implementation 0089 }; 0090 0091 } // namespace sf 0092 0093 0094 #endif // SFML_MUTEX_HPP 0095 0096 0097 //////////////////////////////////////////////////////////// 0098 /// \class sf::Mutex 0099 /// \ingroup system 0100 /// 0101 /// Mutex stands for "MUTual EXclusion". A mutex is a 0102 /// synchronization object, used when multiple threads are involved. 0103 /// 0104 /// When you want to protect a part of the code from being accessed 0105 /// simultaneously by multiple threads, you typically use a 0106 /// mutex. When a thread is locked by a mutex, any other thread 0107 /// trying to lock it will be blocked until the mutex is released 0108 /// by the thread that locked it. This way, you can allow only 0109 /// one thread at a time to access a critical region of your code. 0110 /// 0111 /// Usage example: 0112 /// \code 0113 /// Database database; // this is a critical resource that needs some protection 0114 /// sf::Mutex mutex; 0115 /// 0116 /// void thread1() 0117 /// { 0118 /// mutex.lock(); // this call will block the thread if the mutex is already locked by thread2 0119 /// database.write(...); 0120 /// mutex.unlock(); // if thread2 was waiting, it will now be unblocked 0121 /// } 0122 /// 0123 /// void thread2() 0124 /// { 0125 /// mutex.lock(); // this call will block the thread if the mutex is already locked by thread1 0126 /// database.write(...); 0127 /// mutex.unlock(); // if thread1 was waiting, it will now be unblocked 0128 /// } 0129 /// \endcode 0130 /// 0131 /// Be very careful with mutexes. A bad usage can lead to bad problems, 0132 /// like deadlocks (two threads are waiting for each other and the 0133 /// application is globally stuck). 0134 /// 0135 /// To make the usage of mutexes more robust, particularly in 0136 /// environments where exceptions can be thrown, you should 0137 /// use the helper class sf::Lock to lock/unlock mutexes. 0138 /// 0139 /// SFML mutexes are recursive, which means that you can lock 0140 /// a mutex multiple times in the same thread without creating 0141 /// a deadlock. In this case, the first call to lock() behaves 0142 /// as usual, and the following ones have no effect. 0143 /// However, you must call unlock() exactly as many times as you 0144 /// called lock(). If you don't, the mutex won't be released. 0145 /// 0146 /// \see sf::Lock 0147 /// 0148 ////////////////////////////////////////////////////////////
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|