About Thread Synchronization
-
Hi all, I'm having an application for Remote control, but there are some problems in its part of network connections.I have tried for weeks, still can't find out the reason for that. In our program, we don't use thread synchronization in function "TCPSock->Recv"(winsock api recv() is used in it), because this function is only used in single thread. Today, I tried to add thread synchronization to this function.As a result, these network connections problems are fixed(at least it seems to be). My question is, "Is thread synchronization needed when only using a function in single thread?" Regards, HQ
-
Hi all, I'm having an application for Remote control, but there are some problems in its part of network connections.I have tried for weeks, still can't find out the reason for that. In our program, we don't use thread synchronization in function "TCPSock->Recv"(winsock api recv() is used in it), because this function is only used in single thread. Today, I tried to add thread synchronization to this function.As a result, these network connections problems are fixed(at least it seems to be). My question is, "Is thread synchronization needed when only using a function in single thread?" Regards, HQ
hanq_38910130 wrote:
"Is thread synchronization needed when only using a function in single thread?"
It is actually the other way round. If you are using multiple threads, then you try to synchronize access to the code that must be protected from multiple simultaneous accesses. Or is that you are referring to an additional worker thread (other than the application's main thread) while saying a "Single thread"?
It is a crappy thing, but it's life -^ Carlo Pallini
-
hanq_38910130 wrote:
"Is thread synchronization needed when only using a function in single thread?"
It is actually the other way round. If you are using multiple threads, then you try to synchronize access to the code that must be protected from multiple simultaneous accesses. Or is that you are referring to an additional worker thread (other than the application's main thread) while saying a "Single thread"?
It is a crappy thing, but it's life -^ Carlo Pallini
Thanks for your replay:) Yes, I mean "an additional worker thread" when I say "a single thread". Our program mainly has two parts, server and client. server accepts connections from client, when a connection is accepted, it creates a "worker thread" to communicate with the client. Only in this thread, "TCPSock->Recv()" will be called, so originally we don't use a "Thread Synchronization". But today, I Added "Thread Synchronization", and the bugs seem to be fixed, so I want to know "Is Thread Synchronization needed in single thread in some conditions?" Thanks very much :)
-
Thanks for your replay:) Yes, I mean "an additional worker thread" when I say "a single thread". Our program mainly has two parts, server and client. server accepts connections from client, when a connection is accepted, it creates a "worker thread" to communicate with the client. Only in this thread, "TCPSock->Recv()" will be called, so originally we don't use a "Thread Synchronization". But today, I Added "Thread Synchronization", and the bugs seem to be fixed, so I want to know "Is Thread Synchronization needed in single thread in some conditions?" Thanks very much :)
hanq_38910130 wrote:
But today, I Added "Thread Synchronization", and the bugs seem to be fixed
Glad about that.
hanq_38910130 wrote:
"Is Thread Synchronization needed in single thread in some conditions?"
But, I still am not able to understand this. If it is just one thread accessing the code that must be 'protected', then it does not matter how many other threads are running simultaneously. But if more than one thread can possibly access the code that must be protected, then you need synchronization. I hope I am clear. There is a whole lot of information on this topic here[^] if you would like to read up.
It is a crappy thing, but it's life -^ Carlo Pallini
-
hanq_38910130 wrote:
But today, I Added "Thread Synchronization", and the bugs seem to be fixed
Glad about that.
hanq_38910130 wrote:
"Is Thread Synchronization needed in single thread in some conditions?"
But, I still am not able to understand this. If it is just one thread accessing the code that must be 'protected', then it does not matter how many other threads are running simultaneously. But if more than one thread can possibly access the code that must be protected, then you need synchronization. I hope I am clear. There is a whole lot of information on this topic here[^] if you would like to read up.
It is a crappy thing, but it's life -^ Carlo Pallini
;) Thanks for your help. I will look into the linking which you provided to see if there can be any useful information. Add maybe the reason that causes all these problems exist in some other code, anyway I will check this. ;P