Solved

making thread body as member function

Posted on 2001-07-25
8
245 Views
Last Modified: 2013-12-14
In C++ how to create a thread, whose thread body is member function (not static ) of the
class, so that when class object is created, that particular thread should
access only its member variables, which may be the case with as many class
objects created.
0
Comment
Question by:havman56
8 Comments
 
LVL 6

Expert Comment

by:MichaelS
ID: 6320931
I am afraid you can't do it. What is the problem with static functions?
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 6320942
struct A
{
   void Realwork();
   static void Run(void*)
}

void A::Run(void* parm)
{
   reinterpret_cast<A*>(parm)->Realwork();
}

A object;
makethread(&A::Run, reinterpret_cast<void*>(&A))
0
 
LVL 6

Expert Comment

by:MichaelS
ID: 6320961
>KangaRoo
Actually the question is how to do it without "static"
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Accepted Solution

by:
KangaRoo earned 50 total points
ID: 6320971
make-thread functions generally tke parameters of the form

make_thread(FuncPtrType, void*)
and possible a few others. The function pointer type is usually a C funtion, like void (*FuncPtrType)(void*) or int (*FuncPtrType)(void*). There is no way to fit a non-static funtion pointer into this.
But we can use a static meber and the obvious thing to do is pass the object as the void*. The static can then cast the void* parameter back into an object.

For safety you might consider making the static thread function private and provide a public interface:

class A
{
     void Realwork();
     static void Run(void*)
  public:
     void RunThreaded();
}
void A::Run(void* parm)
{
  reinterpret_cast<A*>(parm)->Realwork();
}

void A::RunThreaded()
{
   makethread(&A::Run, reinterpret_cast<void*>(this));
}

A object;
object.RunThreaded();
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 6320975
And that is without statics, well, not in the interface. The use of a static member is now a hidden implementation detail :)
0
 
LVL 10

Expert Comment

by:makerp
ID: 6321109
void __cdecl run_thread(void *p)
{
  Class *c = (Class*)p;
  p->run();
  delete r;
}

class Class
{
  void run()
  {
     printf("RUN");
  }
};


the latter on

Class *c = new Class
_beginthread(run_thread,0,(void*)c);

this would/should work on windows compilers
0
 
LVL 4

Expert Comment

by:AssafLavie
ID: 6321142
Consider the following more object oriented approach:

1. Define an interface for performing thread actions:

struct Action
{
     virtual void operator() = 0;
     virtual void ~Action() { };
};

2. Define a general thread creating function and ThreadProc which invoke a functor:

HANDLE CreateThreadObject(Action* pAction)
{
     return CreateThread(0, 0, GeneralThreadProc, pAction, 0, 0);
};
static void GeneralThreadProc(void * prm)
{
     reinterpret_cast<Action*>(prm)->();
};

3. Each time you want to do some task in another thread simply derive from Action:
struct SomeWorkerAction : public Action
{
     void operator()
     {
          MessageBox(0, "This is done on another thread", 0, 0);
          delete this;
     };
};

-- or --

struct SomeWorkerActionWithParams : public Action
{
     string data_;
     SomeWorkerActionWithParams(const string& data) : data_(data) { };
     void operator()
     {
          MessageBox(0, "This is done on another thread", data_.c_str(), 0);
          delete this;
     };
};

And spawn a thread to run it:
     CreateThreadObject(new SomeWorkerAction);
     CreateThreadObject(new SomeWorkerActionWithParams("use this param"));

This approach is a lot nicer. It's easier to pass parameters to the thread and it's also easier to use when implementing more complicated MT applications. (For instance, you can store a queue of Action* and invoke them from a pool of threads.)

btw, the code is just a thought. I didn't compile it or anything so don't be suprised if it has errors. I *do* happen to have a private Thread Pool library which uses a similar idiom.
0
 
LVL 1

Expert Comment

by:xutao
ID: 6329121
I think this is what exactly you want:
http://www.codeproject.com/useritems/callback_adapter.asp

Almost a perfect solution.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Find Visual Studio Tools 2 121
Dynamically allocate memory 9 70
Embarcadero WebBroker REST server endpoint parameters 2 96
Need some help with design 13 13
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question