[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: CriticalSection in VisaulC++ in C



Hi Oyvind,

>Q1. Does this tell me that _multi_threading and inheritance the
>    OO way are "incompatible"?

The way I did it once was to make a class CThread that would spawn when
constructed. The constructor of the CThread class would create a thread
and pass a statically declared C function (declared statically in the
'thread.cpp' file). This C function would get the object's this pointer 
as a parameter and simply would call this->run().

>From that moment the thread would be back into the object oriented world.

The object's destructor would (attempt) to kill the thread.

The following code is not tested, but illustrates the idea:

class CThread
{
    CThread( const void* parameters );
    ~CThread();
    void Run();
private:
    void* m_Parameters;
}

static LPTHREADPROC ThreadStarter( CThread *caller )
{
    caller->Run();
}

CThread::CThread( const void* parameters )
{
    m_Parameters = parameters;
    CreateThread(..., ..., ..., ThreadStarter, this, ... );
}

void CThread::Run()
{
    ... whatever the thread should do
}


>Q2. Why does it have to be a "C-style function". Is Win32 a non-OO
>    system?
Exactly. 

Dr. Ir. J.P.E. Sunter
Philips TASS
Building HCZ-1
PO. Box 218
5600 MD Eindhoven
The Netherlands

Tel. +31-40-2755288
Fax. +31-40-2755419
E-mail: johan.sunter@xxxxxxxxxxx