Jamoma API  0.6.0.a19
TTThread.h
1 /*
2  * TTBlue Class for working with threads
3  * Copyright © 2008, Timothy Place
4  *
5  * License: This code is licensed under the terms of the "New BSD License"
6  * http://creativecommons.org/licenses/BSD/
7  */
8 
9 #ifndef __TT_THREAD_H__
10 #define __TT_THREAD_H__
11 
12 #include "TTBase.h"
13 #include <thread>
14 
15 /****************************************************************************************************/
16 // Class Specification
17 // Note, a good resource is here: https://computing.llnl.gov/tutorials/pthreads/#CreatingThreads
18 
19 typedef void*(*TTThreadCallbackType)(void* arg);
20 
21 // C++11-compliant TTThread
22 class TTThread
23 {
24  std::thread thread;
25  //TTThreadCallbackType callback; ///< method called in the new thread when it starts
26  //void* argument; ///< argument passed to the callback
27 
28 public:
29  TTThread(TTThreadCallbackType aCallback, void* anArgument)
30  {
31  if(aCallback != nullptr)
32  thread = std::thread(aCallback, anArgument);
33  }
34 
35  /** Tells the calling thread to go dormant for an amount of time specified in milliseconds. */
36  static void sleep(TTUInt32 millisecondsToSleep)
37  {
38  std::this_thread::sleep_for(std::chrono::milliseconds(millisecondsToSleep));
39  }
40 
41  /** Tells the calling thread to wait for this thread to terminate before moving on.*/
42  void wait()
43  {
44  if (thread.joinable())
45  thread.join();
46  }
47 
48  /** This method is called in the new thread when the thread is created. */
49  //void* doCallbackMethod();
50 
51  // make sure this is a friend so that it can access the private members of the other atom
52  friend bool operator == (const TTThread& thread1, const TTThread& thread2)
53  {
54  return thread1.thread.get_id() == thread2.thread.get_id();
55  }
56 };
57 
58 typedef TTThread* TTThreadPtr;
59 
60 
61 #endif // __TT_THREAD_H__
Jamoma's lowest-level base class and related infrastructure.
bool TTFOUNDATION_EXPORT operator==(const TTObject &anObject, const TTObject &anotherObject)
Compare two objects for equality.
Definition: TTObject.cpp:167
std::uint32_t TTUInt32
32 bit unsigned integer
Definition: TTBase.h:178