2

I would like to create a thread pool. I have a class called ServerThread.cpp, whose constructor should do something like this:

ServerThread::ServerThread()
   {
         for( int i=0 ; i<init_thr_num ; i++ )
         {
              //create a pool of threads
              //suspend them, they will wake up when requests arrive for them to process
         }
   }

I was wondering if creating pthreads inside a constructor can cause any undefined behavior that one should avoid running into.

Thanks

cpp_noname
  • 2,031
  • 3
  • 17
  • 30

2 Answers2

5

You can certainly do that in a constructor but should be aware of a problem that is clearly explained by Scott Meyers ins his Effective/More Effective C++ books.

In short his point is that if any kind of exception is raised within a constructor, then your half-backed object will not be destroyed. This leads to memory leaks. So Meyers' suggestion is to have "light" constructors and then do the "heavy" work in an init method called after the object has been fully created.

This argument is not strictly related to creating a pool of pthreads within a constructor (whereby you might argue that no exception will be raised if you simply create them and then immediately suspend them), but is a general consideration about what to do in a constructor (read: good practices).

Another considerations to be done is that a constructor has no return value. While it is true that (if no exceptions are thrown) you can leave the object is a consistent state even if the thread creation fails, it would be possibly better to manage a return value from a kind of init or start method.

You could also read this thread on S.O. about the topic, and this one.

Community
  • 1
  • 1
sergio
  • 68,819
  • 11
  • 102
  • 123
  • 1
    That has nothing to do with threads, and Scott's suggestion has been largely superseded by more advanced techniques: things like having the resources managed by a base class or a member, which will have been fully constructed before entering the constructor body. (In this case, the pool of threads should be a member, so its destructor will be called in case of an exception.) – James Kanze Jun 29 '12 at 10:32
  • @James Kanze: in my answer, I had already pointed out the following: "This argument is not strictly related to creating a pool of pthreads within a constructor", but to "good practices". Furthermore, you confirm both in your comment and in your own answer that it is advisable to use some specific technique to correctly deal with this. An `init` method, as old or simple as it may be, is just one of those techniques. In any case, thanks for your comment. – sergio Jun 29 '12 at 10:43
2

From a strictly formal point of view, a constructor is really just a function like any other, and there shouldn't be any problem. Practically, there could be an issue: the threads may actually start running before the constructor has finished. If the threads need a fully constructed ServerThread to operate, then you're in trouble—this is often the case when ServerThread is a base class, and the threads need to interact with the derived class. (This is a very difficult problem to spot, because with the most frequently used thread scheduling algorithms, the new thread will usually not start executing immediately.)

James Kanze
  • 150,581
  • 18
  • 184
  • 329