Let’s say I have a class with a static function. The class’s constructor does a pthread_create using the static function as its entry point.
My question is:
If I had multiple instances of this class, would they all run their own thread using that function? Are there any issues with doing this? And… if the function itself had static variables in it, would I have a problem with it not being re-entrant?
If your constructor does a
pthread_create()every time, then you’ll have as many threads as you do objects. If those threads accessstaticvariables in your class, you will need to ensure that access to those variables is protected by a mutex. (Also, if those threads access non-staticvariables, you’ll want to protect those too, from other callers to your object’s methods).One thread per object is probably too many, so you may want to reconsider your design.