Mike Davies
Mike Davies

Reputation: 21

Howto Execute a C++ member function as a thread without Boost?

I am using a small embedded RTOS which supports threads. I am programming in C++ and want to create a class that will allow me to run an arbitrary member function of any class as a thread. The RTOS does not directly support creating threads from member functions but they work fine if called from withing a thread. Boost::thread is not available on my platform.

I am currently starting threads in an ad-hoc fashion through a friend thread_starter() function but it seems that I must have a seperate one of these for each class I want to run threads from. My current solution of a thread base class uses a virtual run() function but this has the disadvantage that I can only start 1 thread for a class and that is restricted to the run() function + whatever that calls in turn (ie I cannot run an arbitrary function from within run() elegantly)

I would ideally like a class "thread" that was templated so I could perform the following from within a class "X" member function :

class X 
{
    run_as_thread(void* p)';
};

X x;
void* p = NULL;

template<X>
thread t(x, X::run_as_thread, p);

//somehow causing the following to be run as a thread :
x->run_as_thread(p);

Sorry if this has been done to death here before but I can only seem to find references to using Boost::thread to accomplish this and that is not available to me. I also do not have access to a heap so all globals have to be static.

Many thanks,

Mike

Upvotes: 2

Views: 1067

Answers (4)

Omaha
Omaha

Reputation: 2292

It sounds like what you are asking is a way to run an arbitrary member function on a class asynchronously. I take it from your comment about the virtual run() function:

"this has the disadvantage that I can only start 1 thread for a class"

...to mean that you do not like that option because it causes all function calls to execute in that thread, when what you want is the ability to have individual function calls threaded off, NOT just create an object-oriented thread abstraction.

You should look into a thread pooling library for your target platform. I can't offer any concrete suggestions given no knowledge of your actual platform or requirements, but that should give you a term to search on and hopefully get some fruitful results.

Upvotes: 0

fduff
fduff

Reputation: 3811

Have a look at my post on passing C++ callbacks between unrelated classes in non-boost project here.

Upvotes: 0

Tom Tanner
Tom Tanner

Reputation: 9354

Assuming your RTOS works a bit like pthreads, and you don't have C++11 (which probably makes assumptions about your threading support) you can use this sort of mechanism, but you need a static method in the class which takes a pointer to an instance of the class. Thus (roughly)

class Wibble
{
  public:
    static void *run_pthread(void *me)
    {
         Wibble *x(static_cast<Wibble *>(me));
         return x->run_thread_code();
    }
  private:
    void *run_thread();
};

Wibble w;
pthread_create(&thread, &attr, Wibble::run_pthread, &w);

Passing arguments is left as an exercise to the reader...

This can be templatised with a bit of effort, but it's how the guts is going to need to work.

Upvotes: 1

Some programmer dude
Some programmer dude

Reputation: 409136

If your compiler is modern enough to support the C++11 threading functionality then you can use that.

Maybe something like this:

class X
{
public:
    void run(void *p);
};

X myX;
void *p = nullptr;

std::thread myThread(std::bind(&X::run, myX, p));

Now X::run will be run as a thread. Call std::thread::join when the thread is done to clean up after it.

Upvotes: 2

Related Questions