| TIP: Click on subject to list as thread! | ANSI |
| echo: | |
|---|---|
| to: | |
| from: | |
| date: | |
| subject: | Multithreading member-fu |
PS> I'm not sure weather I should post this in a C++ echo or
PS> OS/2 programming echo,
PS> but here it goes:
This really goes in the OS/2 programming echo.
PS> Is there any way to multithread a C++ Class member-function? I'm using
PS> VisualAge C++ 3.0, and I am trying to execute a member-function in a separate
PS> thread. But the compiler says "error EDC3167: The
"_Optlink" qualifier cannot
There are two ways around this. One, which you probably already
considered is to instantiate the class in a utility (non-member)
function and call the member function from that utility. E.g.
void utilityFoo ( void * )
{
MyClass myclass ;
myclass.foo () ;
}
And then use utility method utilityFoo as the parameter for
_beginthread. This is also kind of a pain in the ass because you have
to demangle the name the compiler gives 'void utilityFoo ( void * )'
The other way to do this is to use the OCL IThread class. (The
following is taken from the VAC++ 3.0 Frequently Asked Questions info
file (CPPFAQ.INF, found in IBMCPP\HELP, the topic heading is "How do I
start a Thread?")
// MyClass has member function void foo()
IThread thread;
thread.start( new IThreadMemberFn( *this, foo ) )
2. If the code you want to run does not match the above descriptions,
you must write a "wrapper" using one of the following two techniques:
a. Derive from IThreadFn and implement run to call the desired member
function of your object. This may be difficult because you typically
need to know what additional arguments to pass, and this technique
provides no way to address that requirement. However, it can be more
syntactically elegant: aThread.start(this) instead of aThread.start(
new IThreadMemberFn(*this, T::foobar) ). Also, the lifetime of
this must transcend the lifetime of the thread.
b. Derive a new class from IThreadFn. Add data members for arguments
to
the function you want to run, including the object that a member
function might be invoked on. Add a constructor that accepts
references to the arguments as its arguments. Finally, implement run
to dispatch the function you want with the provided arguments. The
MLE sample program shipped with VisualAge C++ provides an example of
this technique. You could alternatively implement the function you
want to run within MyThreadFn::run.
The IThreadFn object is reference-counted so that it can be
automatically deleted when the thread terminates. As a result, if
you use IThreadFn or a derived class, you must allocate the IThreadFn
using the new operator and you must not delete it.
I know this is a little complex, spend some time looking at the
example code, the CPPFAQ.INF and the IThread and IThreadFn classes in
the OCL reference. If you are still stuck let us know!
Thanks
Peter
___
X KWQ/2 1.2i X LSD: virtual reality without the expensive hardware.
--- Maximus/2 2.02
* Origin: OS/2 Shareware BBS, telnet://bbs.os2bbs.com (1:109/347)SEEN-BY: 50/99 54/99 270/101 620/243 625/155 711/401 413 430 934 712/311 407 SEEN-BY: 712/505 506 517 623 624 704 713/317 800/1 @PATH: 109/347 632 7 396/1 270/101 712/624 711/934 |
|
| SOURCE: echomail via fidonet.ozzmosis.com | |
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™.