Re: pthread_create() and class member functions....

From: Victor Perevoztchikov (perev@bnl.gov)
Date: Mon Oct 04 1999 - 17:12:30 MEST


Hi Joern,

> However, the root TPosixThread implementation is unfortunately based on
> the pthread call
> of member function  TThread::Fun with class instance pointer as
> argument; since
> TThread::Fun is non virtual, this works in our environment as desribed
> (probably will not work with other compilers/platforms?...) .

why "unfortunately based". Pthread standard allows only simple C function.

TThread::Fun is static member of a class. A static member function == C function.
So it will work with all compilers/platforms.
I introduced static member function TThread::Fun and not simple C function
only do not pollute the name space. 

But you still can use nonstatic virtual member  function in Pthread.

For example you want to call virtual finction void A::F()
By C++ rules you mas provide 2 pointers:
 A* p  - pointer to class instance
 void (A::*f)()  - pointer to function

 Then you calculate pointer f = &A::F;

User function for Pthread

   user(void* adr)
{
  A inst;
  (A::*f)() = ((A::*)())adr;
  (inst.*f)();
}

In the example instance of A:: created inside of function.
But it is easy to pass this pointer via intermediate object
which keeps both poiners, to instance and to function.
Pointer to this object passed by adr uf user routine.

So, there is no any limitation in THread::

Victor




Joern Adamczewski wrote:
> 
> Hello Walter,
> 
> thanks for your detailed comments on the problem;
> sometimes things are not as simple as they seem, even if
> they might work!
> 
> In fact, testing again the use of member functions  within root TThread
> constructor
> (see previous mail's example), there _are_  problems with virtual
> functions;
> always the base class virtual function runs as thread, never the one of
> the derived class
> (observed for the egcs compiler of our Linux environment).
> 
> However, the root TPosixThread implementation is unfortunately based on
> the pthread call
> of member function  TThread::Fun with class instance pointer as
> argument; since
> TThread::Fun is non virtual, this works in our environment as desribed
> (probably will not work with other compilers/platforms?...) .
> 
> best regards,
> 
> Joern
> 
> (Joern Adamczewski, go4 team DV&EE GSI Darmstadt)

-- 
Victor M. Perevoztchikov   perev@bnl.gov  perev@vxcern.cern.ch       
Brookhaven National Laboratory MS 510A PO Box 5000 Upton NY 11973-5000
tel office : 516-344-7894; fax 516-344-4206; home 516-345-2690



This archive was generated by hypermail 2b29 : Tue Jan 04 2000 - 00:43:40 MET