Re: Threading classes concept ?
Gianni Mariani wrote:
red floyd wrote:
class Thread
{
private:
virtual void thread_func() = 0;
static void start_thread(void* param)
{
static_cast<Thread*>(param)->thread_func();
}
void start()
{
os_specific_thread_starter(&start_thread, this);
}
protected:
Thread() { }
virtual ~Thread() { }
private:
Thread(const Thread&);
Thread& operator=(const Thread&);
};
Then you have the following:
class MyThread : public Thread
{
private:
int n;
public:
MyThread(int n_) : Thread(), n(n_) { }
~MyThread();
private:
void thread_func()
{
std::cout << "Hello from thread " << n << std::endl;
}
};
All you need to do is define thread_func.
a) Need synchronization in destructor
Left as exercise for the reader. Note that this is really skeleton
code, no error checking etc... It's just "here's the concept" pseudo-code.
b) Must not be copy constructible or assignable
Agreed. I did it in the base, but not the child. Shown as an example.
However, since the base has private unimplemented copy/assignment, the
default copy/assignment for the child should fail, correct?