rakesh_nits,
..NET doesn't have out-of-the-box solution for this.
There couple types that you can use for synchornizing threads, but non of
the, AFAIKm guarantees that the threads will enter the guarded section in a
FCFS basis. Which means that that a thread may wait infinitelly if the app
is too busy.
However using this unordered Monitor you can build ordered sync object.
Here is my idea:
1. All call this new type FCFSMonitor
2. It has the follwong methods:
- Constructor that except an object to use for sync the guarded section
- Enter method
- Exit method
3. When you create instance of this class you pass some reference to a type
(reference type) and it saves this reference as an internal field. You also
create a queue of objects that will be used to keep the order of threads
trying to enter the critical section.
4. When a thread want so enter the critucal section it calls
fcfsMontior.Enter method. This method does the following thing:
- Calls Montior.TryEnter on the object passed in the constructor. If the
TryEnter fails you create a new Object, lock the object, adds it to the
queue and the call Monitor.Wait on this object.
- If a threads enters the critical section it do whatever it needs to do
and call fcfsMontior.Exit. The exit method gets the next object from the
queue lock it, call Monitor.Pusle on it (to wake up the waiting thread),
remove the object from the queue and unlock it. Then leaves the critical
section by unlocking the main sync object. The awaken thread should first
unlock its object and then try to enter the critical section in the same way
it did before.
Ofcourse this alogorithm is not perfect and needs to be polished, but I
think is a good start.
--
HTH
Stoitcho Goutsev (100)
"rakesh_nits" <co*********@gmail.com> wrote in message
news:11*********************@g49g2000cwa.googlegro ups.com...
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.What is the best way to
accomlish this?. 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