Threading C++ lock
<p>I need some synchronous mechanism for thread. I am wondering the implementation below which one is a better way?</p>

<pre><code>classA{

public:
   int sharedResourceA;
   pthread_mutex_t mutex1;
   functionA();
   int nonSharedResources;

}

classA::functionA(){
  pthread_mutex_lock( &amp;mutex1 );
  use sharedResourceA;
  pthread_mutex_unlock( &amp;mutex1 );
}

classA objA;
pthread_mutex_lock(&amp;objA.mutex1) //use lock because another thread can call obj.functionA
  use objA.sharedResources;
pthread_mutex_unlock(&amp;objA.mutex1)

use objA.nonSharedResources = blah //without lock because is non shared
</code></pre>

<p>OR I shouldn't create a lock at classA, instead I create a lock at the application. Eg:</p>

<pre><code>classA objA;
pthread_mutex_t mutex2;
pthread_mutex_lock(mutex2) //use lock because another thread can call obj.functionA
  use objA.sharedResources;
pthread_mutex_unlock(mutex2)

pthread_mutex_lock(mutex2) //use lock because another thread can call obj.functionA
  functionA();
pthread_mutex_unlock(mutex2)

use objA.nonSharedResources = blah //without lock because is non shared
</code></pre>
