Synchronization
-
Hi All, I am having a function which can be called from different independent threads simultaneously.Inside this i am having a hashtable.what each thread will do that it will check whether the hashtable contains a particular key if not then it will make an entry in the hashtable with that key or if it exists it will simply use the value corresponding to that particular key.what i want that the access to the function should be on first come first serve basis.so if a thread is already waiting on the function the current thread after completing processing should leave it (should enter in succession only if there is not any waiting thread), and no thread should be kept waiting for infinite.so entry into the function should be related to time of waiting in short i want the impementation to be a queue like.is using lock(object) will be suffice for the task or i have to use something else. and also if you can tell me how this lock statement functions.whether it will cause the other statement to wait or simply not allow the other threads to access it and throw some error.Thanks in Advance. Rakesh
-
Hi All, I am having a function which can be called from different independent threads simultaneously.Inside this i am having a hashtable.what each thread will do that it will check whether the hashtable contains a particular key if not then it will make an entry in the hashtable with that key or if it exists it will simply use the value corresponding to that particular key.what i want that the access to the function should be on first come first serve basis.so if a thread is already waiting on the function the current thread after completing processing should leave it (should enter in succession only if there is not any waiting thread), and no thread should be kept waiting for infinite.so entry into the function should be related to time of waiting in short i want the impementation to be a queue like.is using lock(object) will be suffice for the task or i have to use something else. and also if you can tell me how this lock statement functions.whether it will cause the other statement to wait or simply not allow the other threads to access it and throw some error.Thanks in Advance. Rakesh
From the documentation.
[
lock
] marks a statement block as a critical section by obtaining the mutual-exclusion lock for a given object, executing a statement, and then releasing the lockHowever, you do not have the guarantee that it acts like a queue, only mutual exclusion on a thread-level basis. If you have an infinite condition in your
lock
block, then all other threads will wait forever. "we must lose precision to make significant statements about complex systems." -deKorvin on uncertainty