Topic: thr_create with member function


Author: "Ronald F. Guilmette" <rfg@rahul.net>
Date: 22 Dec 1994 20:19:48 GMT
Raw View
In article <3d9e4s$mn9@news.iesd.auc.dk>,
Bjarne Rasmussen <bras92@control.auc.dk> wrote:
>
>We have a problem with installing a member-function as a thread under UNIX.

The draft C++ standard does not specifically address threads, or multi-
threaded programs.  Thus, your question would be better directed to the
comp.lang.c++ newsgroup.

--

-- Ron Guilmette, Sunnyvale, CA ---------- RG Consulting -------------------
---- E-mail: rfg@segfault.us.com ----------- Purveyors of Compiler Test ----
-------------------------------------------- Suites and Bullet-Proof Shoes -




Author: bras92@control.auc.dk (Bjarne Rasmussen)
Date: 21 Dec 1994 14:29:48 GMT
Raw View
We have a problem with installing a member-function as a thread under UNIX.
The compiler complains that the member-function is not void, and when the
program is run it breaks with a Segmentation Fault (se code below)

When using a regular function everything runs perfect. Also when using a
regular function which then calls the member-function everything works.

The member function can not be static, since one function per thread is needed !

Any help on this is appreciated (please reply to me) !

Thanks in advance,

Bjarne Rasmussen.

The code below causes a Segmentation Fault:

class foo {
 int i;
public:
 foo() { thr_create(..., f, ...); };
 void f() { while(1) { i=1; ... } };
};

The code below works:

class foo {
 int i;
public:
 foo() { thr_create(..., g, ...); };
 void f() { while(1) { i=1; ... } };
};

void g()
{
 f();
}






Author: pollardj@jba.co.uk (Jonathan de Boyne Pollard)
Date: 21 Dec 1994 14:59:06 -0000
Raw View
Bjarne Rasmussen (bras92@control.auc.dk) wrote:
: We have a problem with installing a member-function as a thread under UNIX.

The problem with member functions is that they need a 'this' pointer.  How
the 'this' pointer is passed is internal compiler magic, and in 99% of
cases conflicts with the parameter passing requirements of the create thread
function.

With OS/2, I get around this as follows

    //
    //    Abstraction of a thread
    //
    class Thread {
    public:
        virtual void main ( void ) = 0 ;
    protected:
        void begin(unsigned long) ;
        Thread(void) {}
    } ;

    static
    void
    run ( void * v )
    {
        ((Thread *)v)->main() ;
    }

    void
    Thread::begin (unsigned long stack)
    {
        _beginthread(run, stack, this) ;
    }

    //
    //    A concrete thread
    //
    class MyThread : public Thread {
        int PerThreadData ;
        char something ;
    public:
        inline
        MyThread ( int d, char s ) :
            PerThreadData(d), something(s)
            {
                begin(4096 * 64) ;
            }
    protected:
        virtual void main ( void )
            {
                // meat of the thread code here.
  // All instance variables of this class are "per-thread"
  // variables.
            }
    } ;

    //
    //    Example use
    //
    int
    main ( int, char ** )
    {
        MyThread mine(99) ;
        //  The main thread goes on to do something else
    }