TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: PETER SCHULLER
from: JERRY COFFIN
date: 1997-04-01 09:44:00
subject: Multithreading member-functions

On (28 Mar 97) Peter Schuller wrote to All...
 > Is there any way to multithread a C++ Class member-function? I'm
 > using VisualAge C++ 3.0, and I am trying to execute a member-function
 > in a separate thread. But the compiler says "error EDC3167: The "
 > _Optlink" qualifier cannot be applied to "void()".". If I however move
 > the function out of the class that works, but instead I can't pass
 > *this on to it.
You normally have to make the thread function static in the class.  To
be useful, you normally pass a pointer to the class as a parameter to
that function, then invoke a virtual function from it.  Here's a version
for Win32 - about all that should be needed to convert it to OS/2 would
be changing the names of the functions and data structures.
#define WIN32_LEAN_AND_MEAN
#include 
class ThreadBase {
    HANDLE handle;
    int state_;
    // This is the function whose address we pass to the OS as the
    // controlling function of the thread.
    //
    DWORD static __stdcall RealThreadProc(void *theClass) {
        // Here we invoke the virtual function in the class whose
        // address we passed into here...
        //
        return ((ThreadBase *)theClass)->ThreadProc();
    }
public:
    virtual DWORD ThreadProc() = 0;
    ThreadBase() {
        // I'm not saving the thread ID, because it's pretty close to
        // useless.  I'm not even sure why they have thread IDs at all.
        //
        DWORD ID;
        CreateThread(NULL, 0, &RealThreadProc, this, CREATE_SUSPENDED, &ID);
    }
    int priority() { return GetThreadPriority(handle); }
    BOOL priority(int new_priority) {
        return SetThreadPriority(handle, new_priority);
    }
    DWORD run() { return ResumeThread(handle); }
    ~ThreadBase() { CloseHandle(handle); }
    void wait(DWORD how_long) { WaitForSingleObject(handle, how_long); }
    // give the caller the thread handle to put in an array
    // in a call to [Msg]WaitForMultipleObjects.
    HANDLE operator()() { return handle; }
};
I s'pose there'll be a _little_ more to conversion than adding "Dos" to
the beginning of each function name, but it shouldn't be a _whole_ lot
more than that either.
    Later,
    Jerry.
... The Universe is a figment of its own imagination.
--- PPoint 1.90
---------------
* Origin: Point Pointedly Pointless (1:128/166.5)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.