Creating POSIX threads with a class function

Discussion in 'C++' started by Christian Buckl, Oct 8, 2004.

  1. Hi,
    I try to implement my own thread class based on POSIX threads. I want my
    class to manage everything (creation of threads, exception handling...).
    This includes also some functions that need to be called within the threads
    context (like setting the cancelability state and type). That´s why I am
    using a function that initializes the thread and then calls the original
    thread function.
    The problem is, that I can't manage to implement the initializing function
    within my class.
    The code is added at the end of this message. I currently use an extra
    initialising function outside my class. This function initializes the thread
    and then calls the origin thread function. This works very well. But if I
    try to implement the init function as member of the class I have problems
    to convert the function properly to have the right parameter for the
    pthread_create call.

    I look forward for your answers

    Chris


    //myThread.h
    class MyThread
    {
    public:
    MyThread(int threadPriority, void* (*threadFunction)(void*));

    ...

    void* (*mThreadFunction)(void*); //the original thread function
    ...
    };

    void* initThread(void* arg); //the thread initialising function


    --------------------------------------------------------------------

    //myThread.cc
    MyThread::MyThread(int threadPriority, void* (*threadFunction)(void*));
    {
    ...
    mThreadFunction=threadFunction; //I save the original thread function
    ...
    pthread_create(&mThreadID,&attr,initThread,(void*) this); //create a
    thread that uses the initializing function
    }

    void* initThread(void* arg)
    {
    ZerberusThread* tmp;
    //initialisation of thread
    ...

    //execute now the original thread function
    tmp=(ZerberusThread*) arg;
    tmp->mThreadFunction(NULL);
    }
    Christian Buckl, Oct 8, 2004
    #1
    1. Advertising

  2. Christian Buckl

    Sharad Kala Guest

    "Christian Buckl" <> wrote in message
    > I try to implement my own thread class based on POSIX threads. I want my
    > class to manage everything (creation of threads, exception handling...).
    > This includes also some functions that need to be called within the

    threads
    > context (like setting the cancelability state and type). That´s why I am
    > using a function that initializes the thread and then calls the original
    > thread function.


    Off-topic. Please read this - http://www.slack.net/~shiva/welcome.txt
    You could get an answer on comp.programming.threads.

    Sharad
    Sharad Kala, Oct 8, 2004
    #2
    1. Advertising

  3. * Christian Buckl:
    >
    > The problem is, that I can't manage to implement the initializing function
    > within my class.


    Use a constructor.


    > The code is added at the end of this message. I currently use an extra
    > initialising function outside my class. This function initializes the thread
    > and then calls the origin thread function. This works very well. But if I
    > try to implement the init function as member of the class I have problems
    > to convert the function properly to have the right parameter for the
    > pthread_create call.


    You cannot use a member function as a C callback.

    Use a namespace level function.

    Encapsulate its usage in your class.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Oct 8, 2004
    #3
  4. Christian Buckl wrote:
    > Hi,
    > I try to implement my own thread class based on POSIX threads. I want my
    > class to manage everything (creation of threads, exception handling...).
    > This includes also some functions that need to be called within the threads
    > context (like setting the cancelability state and type). That´s why I am
    > using a function that initializes the thread and then calls the original
    > thread function.
    > The problem is, that I can't manage to implement the initializing function
    > within my class.
    > The code is added at the end of this message. I currently use an extra
    > initialising function outside my class. This function initializes the thread
    > and then calls the origin thread function. This works very well. But if I
    > try to implement the init function as member of the class I have problems
    > to convert the function properly to have the right parameter for the
    > pthread_create call.


    The problem is that C callbacks cannot call non-static member functions
    of a class. The reason is that the callback does not know on which
    instance of the MyThread class the member function is to be called.
    You could make initThread() a static member function. Since static
    member functions cannot access non-static member data you will still
    need to pass this pointer (just like you are doing now).

    --
    Peter van Merkerk
    peter.van.merkerk(at)dse.nl
    Peter van Merkerk, Oct 8, 2004
    #4
  5. * Peter van Merkerk:
    >
    > The problem is that C callbacks cannot call non-static member functions
    > of a class. The reason is that the callback does not know on which
    > instance of the MyThread class the member function is to be called.
    > You could make initThread() a static member function. Since static
    > member functions cannot access non-static member data you will still
    > need to pass this pointer (just like you are doing now).


    Formally a static member function cannot be declared 'external "C"', so
    this is UnGood (TM) advice -- although it will probably work in practice.

    The OP should use an 'external "C"' function at namespace scope as C
    callback.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Oct 8, 2004
    #5
  6. Alf P. Steinbach wrote:

    > * Peter van Merkerk:
    >
    >>The problem is that C callbacks cannot call non-static member functions
    >>of a class. The reason is that the callback does not know on which
    >>instance of the MyThread class the member function is to be called.
    >>You could make initThread() a static member function. Since static
    >>member functions cannot access non-static member data you will still
    >>need to pass this pointer (just like you are doing now).

    >
    > Formally a static member function cannot be declared 'external "C"', so
    > this is UnGood (TM) advice -- although it will probably work in practice.


    I never mentioned extern "C" in my post.

    > The OP should use an 'external "C"' function at namespace scope as C
    > callback.


    extern "C" specifies external linkage, which is not related to the
    problem the OP is having.

    --
    Peter van Merkerk
    peter.van.merkerk(at)dse.nl
    Peter van Merkerk, Oct 8, 2004
    #6
  7. * Peter van Merkerk:
    > Alf P. Steinbach wrote:
    >
    > > * Peter van Merkerk:
    > >
    > >>The problem is that C callbacks cannot call non-static member functions
    > >>of a class. The reason is that the callback does not know on which
    > >>instance of the MyThread class the member function is to be called.
    > >>You could make initThread() a static member function. Since static
    > >>member functions cannot access non-static member data you will still
    > >>need to pass this pointer (just like you are doing now).

    > >
    > > Formally a static member function cannot be declared 'external "C"', so
    > > this is UnGood (TM) advice -- although it will probably work in practice.

    >
    > I never mentioned extern "C" in my post.


    Happily that's now been corrected... ;-)


    > > The OP should use an 'external "C"' function at namespace scope as C
    > > callback.

    >
    > extern "C" specifies external linkage, which is not related to the
    > problem the OP is having.


    That is incorrect: 'extern "C"' is what is required.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Oct 8, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Evan David Light
    Replies:
    6
    Views:
    454
    David Anderson
    Dec 1, 2003
  2. Heiko Neuhaus

    POSIX Threads causing memory leak

    Heiko Neuhaus, Dec 21, 2003, in forum: C++
    Replies:
    3
    Views:
    2,566
    Paul Pluzhnikov
    Dec 21, 2003
  3. Chaman Singh

    STL map and Posix Threads

    Chaman Singh, Apr 8, 2004, in forum: C++
    Replies:
    3
    Views:
    530
    Nick Hounsome
    Apr 9, 2004
  4. JackC
    Replies:
    14
    Views:
    627
    James Kanze
    Oct 16, 2007
  5. Richard Cranium

    Posix Threads and select()

    Richard Cranium, Jan 2, 2008, in forum: C Programming
    Replies:
    2
    Views:
    753
    suresh shenoy
    Jan 2, 2008
Loading...

Share This Page