Creating POSIX threads with a class function

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.

Advertisements

"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.

Advertisements

> 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?

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:
>
> 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 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:
> 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?

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!