title: Initializing a static array of pthread mutexes in C++
body: <p>In the following code I am recieving an error that acc_locks is an undefined reference in the add_to_balance function. acc_locks is an array of pthread_mutex_t.</p>
  
  <p>I think the error is due to the mutexes not being initialized until the constructor is called. </p>
  
  <p>I would like to initialize them with PTHREAD_MUTEX_INITIALIZER, but I don't know how to accomplish this without writing it out 100 times. (Which I won't do out of principle)</p>
  
  <pre><code>acc_locks = {PTHREAD_MUTEX_INITIALIZER, ... } //100 times
  </code></pre>
  
  <p>This article, <a href="https://stackoverflow.com/questions/5138778/static-pthreads-mutex-initialization">Static pthreads mutex initialization</a>, describes how this can be done in C using P99_DUPL. I can't use C99 w/ C++. Is there a similar duplication macro for C++? Am I trying to solve the wrong problem?</p>
  
  <pre><code>//AccountMonitor.h
  #include &lt;pthread.h&gt;
  
  static const unsigned char num_accounts = 100;
  
  class AccountMonitor{
    private:
      static float balance[ num_accounts];
      static pthread_mutex_t acc_locks[ num_accounts];
    public:
      AccountMonitor();
      void add_to_balance(int acc,float v);
  
  };
  
  
  //AccountMonitor.cpp
  #include "AccountMonitor.h"
  
  float AccountMonitor::balance[ num_accounts] = {0.0};
  
  AccountMonitor::AccountMonitor(){
      for (int i=0; i&lt;num_accounts; i++){ 
          pthread_mutex_init( &amp;acc_locks[i], NULL );
      }
  }
  
  void AccountMonitor::add_to_balance(int acc, float v){
      int index = acc - 1;
  
      pthread_mutex_lock( &amp;acc_locks[ index] );
      balance[ index] += v;
      pthread_mutex_unlock ( &amp;acc_locks[index] );
  
  }
  </code></pre>