Back to home page

EIC code displayed by LXR

 
 

    


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_THREAD_HPP
0026 #define SFML_THREAD_HPP
0027 
0028 ////////////////////////////////////////////////////////////
0029 // Headers
0030 ////////////////////////////////////////////////////////////
0031 #include <SFML/System/Export.hpp>
0032 #include <SFML/System/NonCopyable.hpp>
0033 #include <cstdlib>
0034 
0035 
0036 namespace sf
0037 {
0038 namespace priv
0039 {
0040     class ThreadImpl;
0041     struct ThreadFunc;
0042 }
0043 
0044 ////////////////////////////////////////////////////////////
0045 /// \brief Utility class to manipulate threads
0046 ///
0047 ////////////////////////////////////////////////////////////
0048 class SFML_SYSTEM_API Thread : NonCopyable
0049 {
0050 public:
0051 
0052     ////////////////////////////////////////////////////////////
0053     /// \brief Construct the thread from a functor with no argument
0054     ///
0055     /// This constructor works for function objects, as well
0056     /// as free functions.
0057     ///
0058     /// Use this constructor for this kind of function:
0059     /// \code
0060     /// void function();
0061     ///
0062     /// // --- or ----
0063     ///
0064     /// struct Functor
0065     /// {
0066     ///     void operator()();
0067     /// };
0068     /// \endcode
0069     /// Note: this does *not* run the thread, use launch().
0070     ///
0071     /// \param function Functor or free function to use as the entry point of the thread
0072     ///
0073     ////////////////////////////////////////////////////////////
0074     template <typename F>
0075     Thread(F function);
0076 
0077     ////////////////////////////////////////////////////////////
0078     /// \brief Construct the thread from a functor with an argument
0079     ///
0080     /// This constructor works for function objects, as well
0081     /// as free functions.
0082     /// It is a template, which means that the argument can
0083     /// have any type (int, std::string, void*, Toto, ...).
0084     ///
0085     /// Use this constructor for this kind of function:
0086     /// \code
0087     /// void function(int arg);
0088     ///
0089     /// // --- or ----
0090     ///
0091     /// struct Functor
0092     /// {
0093     ///     void operator()(std::string arg);
0094     /// };
0095     /// \endcode
0096     /// Note: this does *not* run the thread, use launch().
0097     ///
0098     /// \param function Functor or free function to use as the entry point of the thread
0099     /// \param argument argument to forward to the function
0100     ///
0101     ////////////////////////////////////////////////////////////
0102     template <typename F, typename A>
0103     Thread(F function, A argument);
0104 
0105     ////////////////////////////////////////////////////////////
0106     /// \brief Construct the thread from a member function and an object
0107     ///
0108     /// This constructor is a template, which means that you can
0109     /// use it with any class.
0110     /// Use this constructor for this kind of function:
0111     /// \code
0112     /// class MyClass
0113     /// {
0114     /// public:
0115     ///
0116     ///     void function();
0117     /// };
0118     /// \endcode
0119     /// Note: this does *not* run the thread, use launch().
0120     ///
0121     /// \param function Entry point of the thread
0122     /// \param object Pointer to the object to use
0123     ///
0124     ////////////////////////////////////////////////////////////
0125     template <typename C>
0126     Thread(void(C::*function)(), C* object);
0127 
0128     ////////////////////////////////////////////////////////////
0129     /// \brief Destructor
0130     ///
0131     /// This destructor calls wait(), so that the internal thread
0132     /// cannot survive after its sf::Thread instance is destroyed.
0133     ///
0134     ////////////////////////////////////////////////////////////
0135     ~Thread();
0136 
0137     ////////////////////////////////////////////////////////////
0138     /// \brief Run the thread
0139     ///
0140     /// This function starts the entry point passed to the
0141     /// thread's constructor, and returns immediately.
0142     /// After this function returns, the thread's function is
0143     /// running in parallel to the calling code.
0144     ///
0145     ////////////////////////////////////////////////////////////
0146     void launch();
0147 
0148     ////////////////////////////////////////////////////////////
0149     /// \brief Wait until the thread finishes
0150     ///
0151     /// This function will block the execution until the
0152     /// thread's function ends.
0153     /// Warning: if the thread function never ends, the calling
0154     /// thread will block forever.
0155     /// If this function is called from its owner thread, it
0156     /// returns without doing anything.
0157     ///
0158     ////////////////////////////////////////////////////////////
0159     void wait();
0160 
0161     ////////////////////////////////////////////////////////////
0162     /// \brief Terminate the thread
0163     ///
0164     /// This function immediately stops the thread, without waiting
0165     /// for its function to finish.
0166     /// Terminating a thread with this function is not safe,
0167     /// and can lead to local variables not being destroyed
0168     /// on some operating systems. You should rather try to make
0169     /// the thread function terminate by itself.
0170     ///
0171     ////////////////////////////////////////////////////////////
0172     void terminate();
0173 
0174 private:
0175 
0176     friend class priv::ThreadImpl;
0177 
0178     ////////////////////////////////////////////////////////////
0179     /// \brief Internal entry point of the thread
0180     ///
0181     /// This function is called by the thread implementation.
0182     ///
0183     ////////////////////////////////////////////////////////////
0184     void run();
0185 
0186     ////////////////////////////////////////////////////////////
0187     // Member data
0188     ////////////////////////////////////////////////////////////
0189     priv::ThreadImpl* m_impl;       //!< OS-specific implementation of the thread
0190     priv::ThreadFunc* m_entryPoint; //!< Abstraction of the function to run
0191 };
0192 
0193 #include <SFML/System/Thread.inl>
0194 
0195 } // namespace sf
0196 
0197 #endif // SFML_THREAD_HPP
0198 
0199 
0200 ////////////////////////////////////////////////////////////
0201 /// \class sf::Thread
0202 /// \ingroup system
0203 ///
0204 /// Threads provide a way to run multiple parts of the code
0205 /// in parallel. When you launch a new thread, the execution
0206 /// is split and both the new thread and the caller run
0207 /// in parallel.
0208 ///
0209 /// To use a sf::Thread, you construct it directly with the
0210 /// function to execute as the entry point of the thread.
0211 /// sf::Thread has multiple template constructors, which means
0212 /// that you can use several types of entry points:
0213 /// \li non-member functions with no argument
0214 /// \li non-member functions with one argument of any type
0215 /// \li functors with no argument (this one is particularly useful for compatibility with boost/std::%bind)
0216 /// \li functors with one argument of any type
0217 /// \li member functions from any class with no argument
0218 ///
0219 /// The function argument, if any, is copied in the sf::Thread
0220 /// instance, as well as the functor (if the corresponding
0221 /// constructor is used). Class instances, however, are passed
0222 /// by pointer so you must make sure that the object won't be
0223 /// destroyed while the thread is still using it.
0224 ///
0225 /// The thread ends when its function is terminated. If the
0226 /// owner sf::Thread instance is destroyed before the
0227 /// thread is finished, the destructor will wait (see wait())
0228 ///
0229 /// Usage examples:
0230 /// \code
0231 /// // example 1: non member function with one argument
0232 ///
0233 /// void threadFunc(int argument)
0234 /// {
0235 ///     ...
0236 /// }
0237 ///
0238 /// sf::Thread thread(&threadFunc, 5);
0239 /// thread.launch(); // start the thread (internally calls threadFunc(5))
0240 /// \endcode
0241 ///
0242 /// \code
0243 /// // example 2: member function
0244 ///
0245 /// class Task
0246 /// {
0247 /// public:
0248 ///     void run()
0249 ///     {
0250 ///         ...
0251 ///     }
0252 /// };
0253 ///
0254 /// Task task;
0255 /// sf::Thread thread(&Task::run, &task);
0256 /// thread.launch(); // start the thread (internally calls task.run())
0257 /// \endcode
0258 ///
0259 /// \code
0260 /// // example 3: functor
0261 ///
0262 /// struct Task
0263 /// {
0264 ///     void operator()()
0265 ///     {
0266 ///         ...
0267 ///     }
0268 /// };
0269 ///
0270 /// sf::Thread thread(Task());
0271 /// thread.launch(); // start the thread (internally calls operator() on the Task instance)
0272 /// \endcode
0273 ///
0274 /// Creating parallel threads of execution can be dangerous:
0275 /// all threads inside the same process share the same memory space,
0276 /// which means that you may end up accessing the same variable
0277 /// from multiple threads at the same time. To prevent this
0278 /// kind of situations, you can use mutexes (see sf::Mutex).
0279 ///
0280 /// \see sf::Mutex
0281 ///
0282 ////////////////////////////////////////////////////////////