Original Post
Hi, I have what is hopefully a quick question about thread synchonization. I have a wrapper class for a TCP socket with the receive running in a seperate thread and a seperate module handling the send. To avoid lock issues, I have defined a mutex within the TCP wrapper itself and ensure to lock it immediately upon attempting to access the socket, so for example in pseudocode: I believe I am perfectly fine in this case but for some reason I had a nagging voice in the back of my head telling me the handler that calls the send should call mutex.Lock() before calling object->send(). This mutex would also be provided to the wrapper class executing the receive as a copy upon construction so if it is in the process of a receive that I can't even step into the object's memory space. Basically looking for some input here since I believe my solution will suffice (I'm not accessing any variables before locking the object) but just wanted to be certain since I'm second guessing myself. Thanks!
// called by a class managing the socket
void send( CMessage& : msg )
{
m_mutex.Lock();
sendto( blah );
m_mutex.Unlock();
}
// running in seperate thread
int receive( void )
{
while( bRun )
{
m_mutex.Lock();
receivefrom( blah );
// if data do something with it
m_mutex.Unlock();
}
}