| 60 | |
| 61 | |
| 62 | class SimpleThread |
| 63 | { |
| 64 | private: |
| 65 | struct ThreadRef; |
| 66 | |
| 67 | template<typename TCallback, typename TArgs> |
| 68 | struct CallbackWrapper |
| 69 | { |
| 70 | template<typename U> |
| 71 | CallbackWrapper(TCallback&& callback, U&& args) |
| 72 | : callback(std::forward<TCallback>(callback)), args(std::forward<U>(args)) |
| 73 | { |
| 74 | } |
| 75 | |
| 76 | static void callAndDelete(void* wrapper) |
| 77 | { |
| 78 | auto typedWrapper = static_cast<CallbackWrapper*>(wrapper); |
| 79 | typedWrapper->args.callCallback(std::move(typedWrapper->callback)); |
| 80 | delete typedWrapper; |
| 81 | } |
| 82 | |
| 83 | typename std::decay<TCallback>::type callback; |
| 84 | TArgs args; |
| 85 | }; |
| 86 | |
| 87 | typedef void (*CallbackFunc)(void*); |
| 88 | |
| 89 | void startThread(void* callbackObj, CallbackFunc callbackFunc); |
| 90 | |
| 91 | |
| 92 | public: |
| 93 | static const int StackSize = 4 * 1024; // bytes |
| 94 | |
| 95 | SimpleThread() : thread(nullptr) { } |
| 96 | |
| 97 | SimpleThread(SimpleThread&& other) |
| 98 | : thread(other.thread) |
| 99 | { |
| 100 | other.thread = nullptr; |
| 101 | } |
| 102 | |
| 103 | SimpleThread& operator=(SimpleThread&& other) |
| 104 | { |
| 105 | thread = other.thread; |
| 106 | other.thread = nullptr; |
| 107 | return *this; |
| 108 | } |
| 109 | |
| 110 | // Disable copying and copy-assignment |
| 111 | private: |
| 112 | SimpleThread(SimpleThread const&); |
| 113 | SimpleThread& operator=(SimpleThread const&); |
| 114 | public: |
| 115 | |
| 116 | template<typename TCallback> |
| 117 | explicit SimpleThread(TCallback&& callback) |
| 118 | { |
| 119 | auto wrapper = new CallbackWrapper<TCallback, ::details::ArgWrapper<>>( |
no outgoing calls