in reply to Thread creation failed: pthread_create returned 11

I am not creating more than 4 threads at a time.

Actually, you could create unbounded many threads between the call to threads->create() and when that thread finally gets created and runs far enough to execute $tcount++.

You could certainly reduce that potential problem by moving the duplicate threads->create() calls out of the if/else blocks so you have just one copy of it below that and do the $tcount++ there instead of inside of processData(). You could still potentially accumulate unbounded many threads whose execution lies somewhere between the $tcount-- and the actual ending of the thread.

You also don't appear to ever join() nor detach() any of these threads. That surely builds up a growing stash of information about what each thread "returned", much like a memory leak.

These types of problems are part of why the usual pattern is to just create 4 threads and then farm out items to them to execute (usually using a thread queue).

- tye        

Replies are listed 'Best First'.
Re^2: Thread creation failed: pthread_create returned 11 (races, leaks)
by kamrul (Acolyte) on Aug 11, 2015 at 16:39 UTC
    Based on what you said, it seems those threads take a bit of time to get created. In that case, will it be more efficient if I keep 4 running threads and pass the messages to the threads using a queue; rather than creating new threads every time ?