Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Creating POSIX threads with a class function

Reply
Thread Tools

Creating POSIX threads with a class function

 
 
Christian Buckl
Guest
Posts: n/a
 
      10-08-2004
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);
}


 
Reply With Quote
 
 
 
 
Sharad Kala
Guest
Posts: n/a
 
      10-08-2004

"Christian Buckl" <(E-Mail Removed)> 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


 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      10-08-2004
* 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?
 
Reply With Quote
 
Peter van Merkerk
Guest
Posts: n/a
 
      10-08-2004
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
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      10-08-2004
* 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?
 
Reply With Quote
 
Peter van Merkerk
Guest
Posts: n/a
 
      10-08-2004
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
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      10-08-2004
* 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?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Posix Threads and select() Richard Cranium C Programming 2 01-02-2008 09:23 PM
Basic Question on POSIX Threads JackC C++ 14 10-16-2007 08:24 PM
STL map and Posix Threads Chaman Singh C++ 3 04-09-2004 07:02 PM
POSIX Threads causing memory leak Heiko Neuhaus C++ 3 12-21-2003 07:16 AM
Problem using IRIX 6.5 CC (MIPSPro C++) STL I/O w/ POSIX threads Evan David Light C++ 6 12-01-2003 06:29 PM



Advertisments