I have code that is completely parallel, no dependencies, so using pthreads was a natural choice. Unfortunately, there is one shared resource, the log file.
We don't want the log to be interleaved line-by-line in any case, so rather than use mutexes on every log call I want to open a separate log file for each thread. But currently, all through the code, there is a global variable logger.
I currently have two solutions, neither of which makes me happy.
I'd love some clever way to make it look like I have a global variable in each thread, something with very little overhead.
If each thread gets its own log, use pthread_key_create and related functions to maintain a per-thread logger variable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With