Use pthread_sigmask instead of sigprogmask for signal handling
According to http://man7.org/linux/man-pages/man2/sigprocmask.2.html sigprogmask behavior is undefined in multithreaded environment. It should be substituted by phtread_sigmask.
Furthermore, instead of blocking the handling of SIGUSR2 after the starting of each function, we can just block handling before starting the various threads and re-enabling it afterward. In this way, only the main thread will be able to react to sig USR2