Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to access local storage using pthreads

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.

  1. Implement a hash on the thread id: pthread_self().
  2. Pass the parameter from the thread creation to every function it calls (very invasive).

I'd love some clever way to make it look like I have a global variable in each thread, something with very little overhead.

like image 920
Dov Avatar asked Dec 29 '22 04:12

Dov


1 Answers

If each thread gets its own log, use pthread_key_create and related functions to maintain a per-thread logger variable.

like image 76
bmargulies Avatar answered Jan 08 '23 15:01

bmargulies