473,379 Members | 1,263 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

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 2622

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Peter Åstrand | last post by:
There's a new PEP available: PEP 324: popen5 - New POSIX process module A copy is included below. Comments are appreciated. ---- PEP: 324 Title: popen5 - New POSIX process module
6
by: Evan David Light | last post by:
After agonizing over this problem for a few days, I've decided to seek help. No, not the variety that involes a jacket that zips up the back but this august body of intrepid individuals. I've...
2
by: raxitsheth | last post by:
Hello All... I am using Posix Thread. class Parent { public: virtual void* func(void *)=0;
11
by: FiLH | last post by:
Hello, I would like to know if posix semaphores are inter processes or just semaphores for threads inside the same process. I have not seen it defined in the posix specification I have found,...
6
by: darklupine | last post by:
I'm working on a project for a class at school. In order to complete the project, I have to create a new thread, but I'm not overly certain on how to do so. I have it coded, but it still throws an...
5
by: Bob Bins | last post by:
Is there a way to create a shared lock using the Monitor class or any other synchronization class in .NET? So I have one resource that may have multiple threads using it at once but a second...
1
by: StuartJ | last post by:
Hi, I'm trying to create a generic threading application where you can pass in any object and a number of threads, most of it is working but I'm having trouble creating multiple instances of the...
23
by: Boltar | last post by:
Hi I'm writing a threading class using posix threads on unix with each thread being run by an object instance. One thing I'm not sure about is , if I do the following: myclass::~myclass() {...
8
by: JackC | last post by:
Hi, I am trying to get posix threads working from within an object, heres some code: int NConnection::TheadControl() { int thread_id; pthread_t new_connection;...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.