Hi Petr Hejl,
I hope I understoon you question correctly. I think you do need to synchronize.
first,You can safetly assume that clients are always single threaded.(unless you are developing a multithreaded client which is unlikely)
But in the case of the server things are different.
Server has to answer many clients at the same time and it is going to be multithreaded.
You locking code would work like this.
A) check if record is alrady locked.
B) If it is not locked then you lock it.
c) update the record
D) release the lock
It all seems fine if there is only one client at any given time. But there will be many clients. The server will user many threads to work with those clients.
Now think about this situation.
Client X does step A. Say the record is not locked. But just before it goes to step B and locks the record the
thread is made it wait and another thread (for another client Y) starts working. That does step A. record is still not locked. now that read locks the record. Now that thread is made to wait. The first thread for client X comes back to work and goes to do step B. It doesn't know that another thread has locked the record while it is waiting.
To deal with this problem you have to Synchronize. When you Snychronize the block of code for step A and B you are saying "only one thread can be inside this block of code at any given time. Another thread can come in only after the first one has finished. So if the Thread for X is blocked between A and B then no other thread can go into the syncrhonized code so another thread can't mess things up.
Synchronizing is something you need to do when working with multiple threads in situations like above. And the requirements say you need a multithreaded server. So if you don't syncrhonize the locking won't work correctly and you may lose points
[ April 12, 2006: Message edited by: sumuhan umamaheswarampillai ]
[ April 12, 2006: Message edited by: sumuhan umamaheswarampillai ]
[ April 12, 2006: Message edited by: sumuhan umamaheswarampillai ]