Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

making thread body as member function

Posted on 2001-07-25
8
Medium Priority
?
249 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 7

Accepted Solution

by:
KangaRoo earned 200 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
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 the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

718 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