Synchronization

R

rakesh_nits

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
 
N

Nicholas Paldino [.NET/C# MVP]

Rakesh,

The lock statement would give you what you want if it wasn't for the
requirement to have a timeout.

However, you can do the following:

// Assume o is the object you want to lock on. Wait for 5 seconds.
Monitor.TryEnter(o, 5000);

// Try/catch.
try
{
// Perform code here.
}
finally
{
// Release lock.
Monitor.Exit(o);
}

This is basically what the compiler does with a lock statement, sans the
call to TryEnter. Rather, it replaces it with Enter.

Hope this helps.
 
S

Stoitcho Goutsev \(100\)

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.
 
J

Jon Skeet [C# MVP]

rakesh_nits said:
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.

Have a look at
http://www.pobox.com/~skeet/csharp/miscutil/usage/locking.html

Currently it doesn't have a "TryLock" instead of Lock, but it wouldn't
be hard to add (and you don't need an explicity try/finally block that
way).

Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top