Thread.hpp
1 //
3 // SFML - Simple and Fast Multimedia Library
4 // Copyright (C) 2007-2014 Laurent Gomila (laurent.gom@gmail.com)
5 //
6 // This software is provided 'as-is', without any express or implied warranty.
7 // In no event will the authors be held liable for any damages arising from the use of this software.
8 //
9 // Permission is granted to anyone to use this software for any purpose,
10 // including commercial applications, and to alter it and redistribute it freely,
11 // subject to the following restrictions:
12 //
13 // 1. The origin of this software must not be misrepresented;
14 // you must not claim that you wrote the original software.
15 // If you use this software in a product, an acknowledgment
16 // in the product documentation would be appreciated but is not required.
17 //
18 // 2. Altered source versions must be plainly marked as such,
19 // and must not be misrepresented as being the original software.
20 //
21 // 3. This notice may not be removed or altered from any source distribution.
22 //
24 
25 #ifndef CPP3DS_THREAD_HPP
26 #define CPP3DS_THREAD_HPP
27 
29 // Headers
31 #include <cpp3ds/System/NonCopyable.hpp>
32 #include <cstdlib>
33 #ifdef EMULATION
34 #include <SFML/System/Thread.hpp>
35 #else
36 #include <3ds.h>
37 #endif
38 
39 // Stack size (in byte)
40 #define THREAD_STACK_SIZE 16384
41 
42 namespace cpp3ds
43 {
44 namespace priv
45 {
46  struct ThreadFunc;
47 }
48 
54 {
55 public :
56 
79  template <typename F>
80  Thread(F function);
81 
107  template <typename F, typename A>
108  Thread(F function, A argument);
109 
130  template <typename C>
131  Thread(void(C::*function)(), C* object);
132 
133  void initialize();
134 
142  ~Thread();
143 
153  void launch();
154 
166  void wait();
167 
179  void terminate();
180 
181 private :
182 
191  static void entryPoint(void* userData);
192 
199  void run();
200 
202  // Member data
204  priv::ThreadFunc* m_entryPoint;
205  bool m_isActive;
206  #ifdef EMULATION
207  sf::Thread* m_thread;
208  #else
209  Handle m_thread;
210  u32* threadStack;
211  #endif
212 };
213 
214 #include <cpp3ds/System/Thread.inl>
215 
216 } // namespace cpp3ds
217 
218 #endif
219 
220 
Utility class to manipulate threads.
Definition: Thread.hpp:53
void launch()
Run the thread.
void wait()
Wait until the thread finishes.
~Thread()
Destructor.
void terminate()
Terminate the thread.
Utility class that makes any derived class non-copyable.
Definition: NonCopyable.hpp:35
Thread(F function)
Construct the thread from a functor with no argument.