Is there a better way to thread this class function? I have a class bar that keeps track of N instances of class foo in a std::map (so N = map.size()).
When I call bar::func I want to have N threads that call foo::foo_func.

foo::foo_func requires multiple arguments though, namely the instance of bar that it's related to.

I was thinking of doing something like:

void * _threaded_foo_func(void *);

struct box {
  bar * the_bar;
  foo * the_foo;
};

class bar {
  class sub_bar {
    // stuff
  };

  map&lt;foo*, sub_bar*&gt; foo_mappings;

  void func() {
    pthread_t threads[ foo_mappings.size() ];
    map&lt;foo*, sub_bar*&gt;::iterator it = foo_mappings.begin();
    int i=0;
    for(it; it != foo_mappings.end() &amp;&amp; i &lt; foo_mappings.size(); ++it, ++i) {
      box * args = new args();
      args-&gt;the_bar = this;
      args-&gt;the_foo = it-&gt;first;
      pthread_create( &amp;threads[i], NULL, _threaded_foo_func, (void*) args);
    }

    for(int i=0; i&lt;bar_mappings.size(); ++i) {
      pthread_join(threads[i], NULL);
    }

  }

};

void * _threaded_foo_func(void * args) {
  box * b = (box*) args;
  bar * the_bar = b-&gt;the_bar;
  foo * the_foo = b-&gt;the_foo;
  the_foo-&gt;foo_func(the_bar);
  return NULL;
}


My questions are:


Is there a better way to do this? Cleaner? Thoughts on using
fork()? 
Does this look like a poor design of the relationship
between foo &amp; bar?


To make this fun, you're only allowed to use pthread.h, no C++11 stuff :)
How many instances of foo are you going to have?  Spawning a large number of threads or forking a large number of processes isn't going to scale well in terms of both CPU and memory resources.

Another approach that generally has better scalability is a thread pool.  The basic idea is that you have a fixed number of threads that pull and execute tasks from a queue, resulting in better resource utilization.