Creating POSIX threads with a class function

  • Thread starter Christian Buckl
  • Start date
C

Christian Buckl

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);
}
 
S

Sharad Kala

Christian Buckl said:
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
 
A

Alf P. Steinbach

* 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.
 
P

Peter van Merkerk

Christian said:
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).
 
A

Alf P. Steinbach

* 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.
 
P

Peter van Merkerk

Alf said:
* Peter van Merkerk:


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

Alf P. Steinbach

* Peter van Merkerk:
I never mentioned extern "C" in my post.

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

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top