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 5 4531
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/
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<Thr ead*>(obj)->run();
return obj; //or whatever else you might need
}
Thread::Thread( )
{
pthread_create( &_id, 0, thread_routine, this);
}
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(voi d* 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_cas t<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
"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<Thr ead*>(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<Thr ead*>(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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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,
John.
|
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 stream
stops playing, at this point I've been able to determine only that the
segfault is from a semaphore message in the pthread library.
Native C++ code solution works fine, so it is some interaction between
python->pySonic shared lib...
|
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 inherit from. A child then only needs to define a
virtual "executive" method that has the thread code for the child object,
and the threading setup and management are automatic then. The thread
creation takes place in the PosixThread base class...
|
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 not. Can anyone tell me why?
--- main.c -> compile with gcc main.c -lpthread ---
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
|
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
| |
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 "tests".
Also, VC++ 2005 beta 1 compiles the tests fine, but here the following
tests fail in execution:
# semaphore1.pass \
# condvar2.pass \
# condvar2_1.pass \
# mutex8.pass \
|
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 -fpic -shared -Wl,-soname,libecpg.so.4 execute.o
typename.o descriptor.o data.o error.o prepare.o memory.o connect.o misc.o
path.o exec.o -L../../../../src/port -L../pgtypeslib -lpgtypes
-L../../../../src/interfaces/libpq -lpq -lcrypt -lm...
|
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):
http://zthread.sourceforge.net/
http://www.inf.uni-konstanz.de/dbis/members/vinnik/zsim/doc/
Can anyone share their ZThreads experience, either good, bad, or
|
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 global variable as a
counter).
Whenever the counter is less than 5, I want to create new thread.
whenever any thread is about to terminate, I will decrement the
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |