I've been working on a project lately, and i need to manage a pair of thread pools.
What the worker threads in the pools do is basically execute some kind of pop operation to each respective queue, eventually wait on a condition variable (`pthread_cond_t`) if there is no available value in the queue, and once they get an item, parse it and execute operations accordingly.
What i'm concerned about is the fact that i want to have no memory leaks, and to achieve that i noticed that calling a `pthread_cancel` on each thread when the main process is exiting is definitely a bad idea, as it leaves a lot of garbage around.
The point is, my first thought was to use a exit flag which i can set when the threads need to exit, so that they can easily free memory and call a `pthread_exit`...
I guess i should set this flag, then send a broadcast signal to the threads waiting on the condition variable and check the flag right after the pop operation...

Is this really the correct way to implement a good thread pool termination? I don't feel that much confident about this... 
I'm writing some pseudo-code here to explain what i'm talking about

Each pool thread will run some code structured like this:

```
/* Worker thread (which will run on each pool thread) */
{ /* Thread initialization */ ... }
loop {
   value = pop();
   { /* Mutex lock because of the shared flag */ ... }
   if (flag) {{ /* Free memory and unlock mutex */ ... } pthread_exit(); }
   { /* Unlock the mutex */ ... }
   { /* Elaborate value */ ... }
}
return NULL;

```

And there will be some kind of `pool_stopRunning()` function which will look like:

```
/* pool_stopRunning() function code */
{ /* Acquire flag mutex */ ... }
setFlag(flag);
{ /* Unlock flag mutex */ ... }
{ /* Acquire queue mutex */ ... }
pthread_cond_broadcast(...);
{ /* Unlock queue mutex */ ... }

```

Thanks in advance, i just need to be sure that there isn't a fancy-er way to stop a thread pool... (or get to know a better way, by any chance)
As always, i'm sorry if there is any typo, i'm not and english speaker and it's kind of late right now >: