473,409 Members | 2,063 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,409 software developers and data experts.

pthreads

Hello!

I am a c++ programmer under the os linux. I am using pthreads and i am
wondering why i can only use satic methods of a class to create a
thread of it. How can i change this, because it limits my programmes
very strongly. thanks in advance..Arnd Schröter
Jul 22 '05 #1
5 4505
On 12 Jan 2004 01:05:27 -0800 in comp.lang.c++, Ar***@uni.de (Arnd
Schroeter) was alleged to have written:
I am a c++ programmer under the os linux. I am using pthreads and i am
wondering why i can only use satic methods of a class to create a
thread of it. How can i change this, because it limits my programmes
very strongly. thanks in advance..Arnd Schröter


ptreads is not part of C++, so I can only assume that it is the usual
reason for such a restriction: Your function is going to be called from
code that is not C++ and does not have any provision to pass a 'this'
pointer to a member function. In many cases the designers of such
things provide an extra generic argument, e.g. a long, into which you
can cram the required 'this' pointer with lots of unsafe casting and
glue.

For comparison, see the topic "[30.2] How do I pass a pointer to member
function to a signal handler, X event callback, etc?" in Marshall
Cline's C++ FAQ. It is always good to check the FAQ before posting.
You can get the FAQ at:
http://www.parashift.com/c++-faq-lite/
Jul 22 '05 #2
Arnd Schroeter wrote:
Hello!

I am a c++ programmer under the os linux. I am using pthreads and i am
wondering why i can only use satic methods of a class to create a
thread of it. How can i change this, because it limits my programmes
very strongly. thanks in advance..Arnd Schröter


Threads are off-topic here, but I think your question is actually not
about threads, but about using callbacks in C with a C++ function.

Non-static member functions cannot be used. They need to be called for
an object, and how would the pthreads library know which object to call
the member function for? Further, the pthreads library is C, and so it
doesn't even know about objects and member functions.
Regarding static member functions: Actually, those probably work on many
platforms, but this is not guarranteed. You need to make your callback
function a non-member function that is declared extern "C" or be sure
that all platforms/compiler combinations you want to support have the
same calling conventions for C and C++.

There is still a way to call a member function, indirectly. pthreads,
like any other C library with a callback interface (at least those I
that know), provide an additional parameter of type void* that is
passed to the callback function. You can use that pointer to pass your
object. Something like this (untested and unfinished) should show you
the idea:
class Thread
{
public:
Thread();
//...
virtual void run() = 0;
//...
private:
pthread_t id_;
};

//pthread_create needs void* both for parameter and return type
extern "C"
void* thread_routine(void* obj)
{
static_cast<Thread*>(obj)->run();
return obj; //or whatever else you might need
}

Thread::Thread()
{
pthread_create(&_id, 0, thread_routine, this);
}
Jul 22 '05 #3
Arnd Schroeter wrote:
I am a c++ programmer under the os linux. I am using pthreads and i am
wondering why i can only use satic methods of a class to create a
thread of it. How can i change this, because it limits my programmes
very strongly. thanks in advance..Arnd Schröter


The non-static member functions have different signature, that's why.
And you can't change that. You have to work your way around it. One
possible workaround is like this:
struct thread
{
static void thread_func(void* p);
virtual void run() = 0;
};

void thread::thread_func(void* p) {
// iirc pthread_create needs a function that takes a void
// pointer.
thread* t = reinterpret_cast<thread*>(p);
t->run();
}

struct some_thread : thread {
void run() {
// do your stuff..
}
};

thread* thr = new some_thread();

Now pass thread::thread_func to the thread creation function as start
routine and thr as argument to start routine.
--
Ahti Legonkov

Jul 22 '05 #4

David Harmon wrote:
[...]
For comparison, see the topic "[30.2] How do I pass a pointer to member
function to a signal handler, X event callback, etc?" in Marshall
Cline's C++ FAQ. It is always good to check the FAQ before posting.
You can get the FAQ at:
http://www.parashift.com/c++-faq-lite/


http://groups.google.com/groups?selm...AA51E%40web.de
http://www.codesourcery.com/archives.../msg00164.html

regards,
alexander.
Jul 22 '05 #5

"Rolf Magnus" <ra******@t-online.de> wrote in message
news:bt*************@news.t-online.com...
Arnd Schroeter wrote:
Hello!

I am a c++ programmer under the os linux. I am using pthreads and i am
wondering why i can only use satic methods of a class to create a
thread of it. How can i change this, because it limits my programmes
very strongly. thanks in advance..Arnd Schröter
Threads are off-topic here, but I think your question is actually not
about threads, but about using callbacks in C with a C++ function.

Non-static member functions cannot be used. They need to be called for
an object, and how would the pthreads library know which object to call
the member function for? Further, the pthreads library is C, and so it
doesn't even know about objects and member functions.
Regarding static member functions: Actually, those probably work on many
platforms, but this is not guarranteed. You need to make your callback
function a non-member function that is declared extern "C" or be sure
that all platforms/compiler combinations you want to support have the
same calling conventions for C and C++.

There is still a way to call a member function, indirectly. pthreads,
like any other C library with a callback interface (at least those I
that know), provide an additional parameter of type void* that is
passed to the callback function. You can use that pointer to pass your
object. Something like this (untested and unfinished) should show you
the idea:
class Thread
{
public:
Thread();
//...
virtual void run() = 0;
//...
private:
pthread_t id_;
};

//pthread_create needs void* both for parameter and return type
extern "C"
void* thread_routine(void* obj)
{
static_cast<Thread*>(obj)->run();
return obj; //or whatever else you might need
}


You really want this to be:
extern "C"
void* thread_routine(void* obj) throw()
{
try {
static_cast<Thread*>(obj)->run();
return obj; //or whatever else you might need
}
catch(...)
{
// return error code or just exit
}
}

because it is undefined what will happen if you allow the main thread
function to throw an exception.

Thread::Thread()
{
pthread_create(&_id, 0, thread_routine, this);
}


P.S. The whole idea of a thread class is probably bad modelling since the
lifetime of the thread is not linked to the lifetime of the object without
quite a lot of fancy stuff.

Problems are:
1. A destructor absolutely must call pthread_join or the lifetime of the
thread is longer than that of the object - this in turn means that the
thread cannot be detached and also that a call to the destructor might never
return.
2. It is possible for the thread to start, do its job and exit before the
creating thread even returns from pthread_create i.e. you may well need a
condition to be signalled from the thread to the ctor to indicate that it is
realy going.

Mostly I model at a higher level of abstraction - for example a Worker
object that accepts Jobs with virtual run methods and runs them.
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: john | last post by:
Hi, I've heard that the lack of performance of JAVA on HP-UX 11.0 is due to bad implementation of pthreads. It should be solved in HP-UX 11.11. Can anyone tell me if this is true ? thanks,...
0
by: Kevin Dahlhausen | last post by:
I've been playing with pySonic on unix. PySonic is pyrex wrapper around the fmod sound library. Fmod is distributed as a shared library that uses pthreads. I am getting a segfault when a...
3
by: Savagesmc | last post by:
Problem Background: I am working on a thin wrapper for the pthreads interface on linux, and have encountered frustrating behavior. The idea is to have a "PosixThread" class that any class can...
15
by: John David Ratliff | last post by:
Maybe someone here can help me. I want to use C++, but pthreads seems to be nonfunctional in it. The following test code I've written in C and C++. The C version works, while the C++ one does...
2
by: Jack | last post by:
hi all I would find a parallel sorting algorithms written with pthread programs, as a benchmark to run. Any pointing will be of help thankyou in advance
2
by: um | last post by:
When the POSIX pthreads library for w32 release 2-2-0 (http://sources.redhat.com/pthreads-win32/) is compiled with VC++6 then it compiles and passes all the benchmark tests in the subdirectory...
5
by: Ed L. | last post by:
Is this pthreads warning of any concern? gcc -O2 -fno-strict-aliasing -Wall -Wmissing-prototypes -Wmissing-declarations -pthread -pthreads -D_REENTRANT -D_THREAD_SAFE -D_POSIX_PTHREAD_SEMANTICS...
8
by: Matt England | last post by:
My team currently using Boost Threads, but we are considering switching to ZThreads. (We seek cross-platform, C++ multithreading capabilities in an external library.) ZThread(s): ...
4
by: Neel | last post by:
Hi, I 'm facing some prob with creating pthreads. My requirement is to create threads in a loop. but I dont want to make an array of pthread. At anytime I want to have 5 threads (I used a...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.