]> git.tdb.fi Git - libs/core.git/blobdiff - source/thread.h
Native threads for Win32
[libs/core.git] / source / thread.h
index 51ca0ef3395c2abe80d182a6cef056429d655e75..2b0766c68ba7fbd0d443265d30d58628ddda76f8 100644 (file)
@@ -6,33 +6,43 @@ Distributed under the LGPL
 #ifndef MSP_FRAMEWORK_THREAD_H_
 #define MSP_FRAMEWORK_THREAD_H_
 
-#ifdef WIN32
-#include "win32signum.h"
-#endif
-
-#include <pthread.h>
+#include "types.h"
 
 namespace Msp {
 
+/**
+Base class for threads.  To create a thread for some task, derive it from this
+class and implement the main() function.  Note that threads are not
+automatically started upon creation - you must manually call launch() instead.
+This is to allow initializing variables of the derived class before the thread
+is started.
+*/
 class Thread
 {
 public:
-       void *join();
-       void kill(int s)   { pthread_kill(thread_, s); }
+       void join();
+       void cancel();
+       void kill();
        virtual ~Thread();
 protected:
-       Thread(): valid_(false) { }
-       void launch()      { if(!valid_) pthread_create(&thread_, 0, &main_, this); }
-       virtual void *main()=0;
-       void exit(void *r) { pthread_exit(r); }
+       Thread(): launched_(false) { }
+       void launch();
+       virtual void main()=0;
+       void check_cancel();
 private:
-       pthread_t thread_;
-       bool      valid_;
+       ThreadHandle thread_;
+       bool         launched_;
 
        Thread(const Thread &);
        Thread &operator=(const Thread &);
 
-       static void *main_(void *t) { return ((Thread *)t)->main(); }
+#ifdef WIN32
+#      define THREAD_RETURN_ DWORD WINAPI
+#else
+#      define THREAD_RETURN_ void *
+#endif
+       static THREAD_RETURN_ main_(void *t) { ((Thread *)t)->main(); return 0; }
+#undef THREAD_RETURN_
 };
 
 } // namespace Msp