I have code that is completely parallel, no dependencies, so using pthreads was a natural choice. Unfortunately, there is one common resource, the log file.
We donβt want the log to be alternated in turn anyway, so instead of using mutexes for each log call, I want to open a separate log file for each thread. But at present, through all the code, there is a global variable logger.
I currently have two solutions, none of which make me happy.
- Implement a hash on the thread id: pthread_self ().
- Pass a parameter from creating the thread for each function that it calls (very invasive).
I would like some smart way to make it look like I have a global variable in each thread, something with very little overhead.
source share