Threads
A thread represents a piece of code that can be run, hence the reason that the wvl::thread provides the operable interface.
The wvl::thread can start a thread for a member function, a global function and a functor which are without parameter. For example:
void
function()
{
wvl::message_box.show("Hello, world");
}
class
functor
{
public:
void operator()()
const
{
wvl::message_box.show("Hello,
world");
}
};
To actually run, you must call the thread::start(). All threads are inactive when first created. When a thread stops running, it can automatically delete itself and release the res.
int WINAPI WinMain
(HINSTANCE hThisInstance,HINSTANCE, char*,
int)
{
wvl::thread
thread_;
thread_.start(&function); //don't forget operator
&!
thread_.wait();
thread_.start(functor());
thread_.wait();
return 0;
}
An alternate way to work with a thread is to create a class that holds a wvl::thread object as a data member. For example
class
foo
{
public:
foo(){}
~foo(){ thread_.wait(); }
void
thread_demo(){
thread_.start(this, &foo::sample);
}
private:
void
sample() {
wvl::message_box.show("Hello,
world");
}
private:
wvl::thread thread_;
};
int WINAPI WinMain
(HINSTANCE hThisInstance,HINSTANCE, char*,
int)
{
foo x;
x.thread_demo();
return 0;
}
During a thread is surviving,
- thread::paused() returns a flag denotes the current thread
is paused.
- thread::pause() can pause a thread,
and
- thread::resume() can resume a thread.
-
thread::wait() can block until the thread is
finish.
- thread::empty() returns a
flag denotes the current thread is existing
- thread::handle() returns the handle of a thread, and the
handle can be operated by specific system API.
wvl::thread can be copy-constructable and copyable, but it is useless, nothing would happen.