469,134 Members | 1,290 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,134 developers. It's quick & easy.

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.

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);
}
Jul 22 '05 #1
6 2454

"Christian Buckl" <bu***@in.tum.de> 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
Jul 22 '05 #2
* 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?
Jul 22 '05 #3
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
Jul 22 '05 #4
* 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?
Jul 22 '05 #5
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
Jul 22 '05 #6
* 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?
Jul 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Peter Åstrand | last post: by
2 posts views Thread by raxitsheth | last post: by
11 posts views Thread by FiLH | last post: by
6 posts views Thread by darklupine | last post: by
5 posts views Thread by Bob Bins | last post: by
23 posts views Thread by Boltar | last post: by
8 posts views Thread by JackC | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.