title: Locking in OMP regions
body: <p>I'm trying to instrument some functions in my application to see how long they take. I'm recording all of the times in-memory using a linked list. </p>
  
  <p>In the process, I've introduced a global variable that keeps track of the end of the list. When I enter a new timing region, I insert a new record at the end of the list. Fairly simple stuff.</p>
  
  <p>Some of the functions I want to track are called in OpenMP regions, however. Which means they'll likely be called multiple times in parallel. And this is where I'm stumped.</p>
  
  <p>If this was using normal Pthreads, I'd simply wrap access to the global variable in a mutex and call it a day. However, I'm unsure: will this strategy still work with functions called in an OpenMP region? As in, will they respect the lock?</p>
  
  <p>For example (won't compile, but I think gets the point across):</p>
  
  <pre><code>Record *head;
  Record *tail;
  
  void start_timing(char *name) {
      Record *r = create_record(name);
      tail-&gt;next_record = r;
      tail = r;
      return r;
  }
  
  int foo(void) {
     Record r = start_timing("foo");
     //Do something...
     stop_timing(r);
  }
  
  int main(void) {
     Record r = start_timing("main");
     //Do something...
     #pragma omp parallel for...
     for (int i = 0; i &lt; 42; i++) {
         foo();
     }
     //Do some more...
     stop_timing(r);
  }
  </code></pre>
  
  <p>Which I would then update to:</p>
  
  <pre><code>void start_timing(char *name) {
      Record *r = create_record(name);
  
      acquire_mutex_on_tail();
      tail-&gt;next_record = r;
      tail = r;
      release_mutex_on_tail();
  
      return r;
  }
  </code></pre>
  
  <p>(Apologies if this has an obvious answer - I'm relatively inexperience with the OpenMP framework and multithreading in general.)</p>