Jump to content
  • Advertisement
Sign in to follow this  
metiscus

Threads, objects, and functionoids

This topic is 4232 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

As part of a project that I am working on, I've got a member function in an object that I would like the create a thread for. The eventual plan is to use a mutex (or semaphore) to limit the number of these threads, but for now assume that that is handled. I am iterating a std::list of objects with functions that need to be threaded. Take the following pseudo code as an example. class node { protected: void foo ( void ); private: void * ptr_a; void * ptr_b; }; std::list<node*> list; some_func ( ... ) { (for each item in list) (create a thread which points to node->foo()) } I get all sorts of compile error messages about referencing a member function in this way. I've tried passing the this pointer from the function to a global function and then allowing the global function to try and start the thread (setting the global function as a friend of the class) and this still yields compile errors. There must be some fundamental understanding that I am missing in this matter. Stuff I am looking at: boost::thread and boost::bind (both of these fail to solve the problem, although the bind is only to pass args. anyway) functioniods - from the c++ faq lite, still trying to wrap my head around this. So I guess what I am looking for is a suggestion of how to best pursue this. If you need more implementation details, I will give them as I am able in responses to questions. As an aside, if anyone has any suggested reading about multithread design for applications, I would be your best friend.

Share this post


Link to post
Share on other sites
Advertisement
A member function is not the same as a void foo(void) function. The member function actually has a hidden first parameter, which is the class instance itself (the this pointer). Most thraeding libraries expect the thread entry to be of a certain form, just like main() always has to be. You have to make some kind of proxy function to run your threads from.

Share this post


Link to post
Share on other sites
Is it possible that I should not be using objects.. I am not trained in software design, I am a computer engineer, so I am always looking out for possible design errors that I would make.

Share this post


Link to post
Share on other sites
What kind of compile errors are you getting? If you post some free standing code that produces the same errors that you're getting in your real code then somebody might be able to help you. Even posting the exact compile errors you're getting without the accompanying code might be enough to let people suggest a solution to your problem. Your question is far too vague to expect anyone to solve your problem.

Share this post


Link to post
Share on other sites
Boost.Thread and Boost.Bind are exactly what you want.


class node
{
// Since foo() is protected, some_func()
// needs access to it!
friend void some_func();
protected:
void foo ();
private:
void * ptr_a;
void * ptr_b;
};

std::list<node*> list;

void some_func()
{
using namespace boost;

// As the name implies, a group of threads
thread_group threads;

// For each node in the list, create a new
// thread in the threadgroup
std::for_each(list.begin(), list.end(),
bind(thread_group::create_thread, &threads, _1);

// NOTE: Destroying the thread_group instance (or a thread
// for that matter) won't terminate the actual thread, so it's
// OK to let 'threads' go out of scope and be destroyed.
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!