X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fcore%2Fthread.h;fp=source%2Fcore%2Fthread.h;h=2b0766c68ba7fbd0d443265d30d58628ddda76f8;hp=0000000000000000000000000000000000000000;hb=e1ea831a640fba534e7e42e399f04cdf681ef8d3;hpb=0bcb8d4d6f33cbdad7b921cac787740bfe8e212e diff --git a/source/core/thread.h b/source/core/thread.h new file mode 100644 index 0000000..2b0766c --- /dev/null +++ b/source/core/thread.h @@ -0,0 +1,50 @@ +/* +This file is part of libmspframework +Copyright © 2006 Mikko Rasa, Mikkosoft Productions +Distributed under the LGPL +*/ +#ifndef MSP_FRAMEWORK_THREAD_H_ +#define MSP_FRAMEWORK_THREAD_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 cancel(); + void kill(); + virtual ~Thread(); +protected: + Thread(): launched_(false) { } + void launch(); + virtual void main()=0; + void check_cancel(); +private: + ThreadHandle thread_; + bool launched_; + + Thread(const Thread &); + Thread &operator=(const Thread &); + +#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 + +#endif