I create multiple threads. I know using static mutex locks we can synchronize threads accessing the data. My requirement is that if 2 threads simultaneously accessing a particular object should be locked otherwise no locks should apply.
In the example below, the main() creates a thread passing a pointer (handler1) to Class Handler. In this case accessing Send() method should be locked. if main() method is referring another class Handler object (handler2), both should be allowed to access Send() - if 2 threads share the common object pointer (handler1), then put a lock before Send() otherwise no locks.
virtual void Send()
// sending messages to socket stream
void* thread_function(void* argObject)
Handler* handler1 = new Handler();
// Test Case 1: In this scenario both main() and thread function are referring to same
// handler1; therefore, should lock before accessing Send()
int val = pthread_create(&tid, NULL, thread_function, (void*)handler1);
// Test Case 2: In this scenario main() is accessing handler1 but thread function is
// accessing handler2; therefore, should NOT apply lock before Send()
Handler* handler2 = new Handler();
int val2= pthread_create(&tid2, NULL, thread_function, (void*)handler2);
Thanks for the help