correct unsafe memory free

store the mutex in the local_thread_handler routine in the stack to
allow deallocating the thread structure before unlocking the mutex. if
not, the main caller thread could allocate memory in a not freed memory
zone
implementation of a soft stop routine

implementation of a stop routine which waits until each thread finished
its work to join it on the caller thread.