title: Multithreaded program using OpenSSL and locks randomly crashes
body: <p>I have logic for signing soap documents which use OpenSSL methods like:</p>
  
  <pre><code>OpenSSL_add_all_digests, 
  SSL_load_error_strings();
  ERR_load_BIO_strings();
  OpenSSL_add_all_algorithms();
  OpenSSL_add_all_digests();
  EVP_DigestUpdate
  </code></pre>
  
  <p>and so on. Of course every logic for signing document is running in different thread.</p>
  
  <p>According to many topics about that subject I created static class for handling OpenSSL threads:</p>
  
  <pre><code>unsigned long SomeStaticClass::pthreads_thread_id(){
          unsigned long ret;
  
          ret = ( unsigned long )pthread_self();
          return ret;
  }
  
  void SomeStaticClass::pthreads_locking_callback( int mode, int type, const char* /*file*/, int /*line*/ ){
  
          if( mode &amp; CRYPTO_LOCK ){
                  printf("CRYPTO_LOCK_MODE type: %d\n", type);
                  pthread_mutex_lock( &amp;( lock_cs[type] ) );
          }
          else{
                  printf("UNLOCK type: %d\n", type);
                  pthread_mutex_unlock( &amp;( lock_cs[type] ) );
          }
  }
  
  void SomeStaticClass::thread_setup(){
  
          printf("THREAD SETUP\n");
          lock_cs = ( pthread_mutex_t* )OPENSSL_malloc( CRYPTO_num_locks() * sizeof( pthread_mutex_t ) );
          for( int i = 0; i &lt; CRYPTO_num_locks(); i++ ){
                  pthread_mutex_init( &amp;( lock_cs[i] ), NULL );
          }
  
          CRYPTO_set_id_callback( SomeStaticClass::pthreads_thread_id );
          CRYPTO_set_locking_callback( SomeStaticClass::pthreads_locking_callback );
  }
  
  void SomeStaticClass::thread_cleanup(){
  
          printf("THREAD CLEANUP\n");
          CRYPTO_set_id_callback( NULL );
          CRYPTO_set_locking_callback( NULL );
          for( int i = 0; i &lt; CRYPTO_num_locks(); i++ ) {
                  pthread_mutex_destroy( &amp;( lock_cs[i] ) );
          }
  
          OPENSSL_free( lock_cs );
  }
  </code></pre>
  
  <p>I left printf for debugging purpose. I know some of these methods are deprecated, but I can only use openssl 0.9.8.</p>
  
  <p>I run thread_setup before runnig threads, and thread_cleanup after join them. Unfortunately my app still randomly crashes when I am using many threads. I get SIGSEGV just after pthreads_locking_callback was calling, cause I get on console <code>CRYPTO_LOCK_MODE type: 2</code>. According to backtrace it crashes after calling OpenSSL_add_all_digests or EVP_DigestUpdate.</p>
  
  <p>So the question is why OpenSSL crashes since I am using proper methods for handling multi-threaded program. What I am missing?</p>
  
  <p><strong>EDIT:</strong>
  It is not duplicate of <a href="https://stackoverflow.com/questions/3919420/tutorial-on-using-openssl-with-pthreads">Tutorial on Using OpenSsl with pthreads</a> because as I mentioned I am already using this classic functions for multithread application.</p>
  
  <p><strong>EDIT2:</strong>
  It works!
  Looks like @Matt Caswell was right. Thank you for the correct answer.</p>