Eventually, I used gcc implementation of thread local storage see this for more details.
I added a tid variable of pid_t type in the global scope but with __thread keyword in front. Inside the function that the thread executes, I assigned to tid its value by calling syscall(SYS_gettid). From that moment tid had the right value and from each thread you can access it directly as a global variable but it has its own copy for each thread. The last thing I had to do is to add tid to the LOG macros. This solves the issue completely and no complicated API of posix threads is required.