471,627 Members | 2,034 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,627 software developers and data experts.

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

Jan 11 '06 #1
3 2689
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"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

Jan 11 '06 #2
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

Jan 11 '06 #3
rakesh_nits wrote:
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/m...e/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

Jan 11 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by WebRod | last post: by
4 posts views Thread by scott | last post: by
12 posts views Thread by emma_middlebrook | last post: by
3 posts views Thread by CKKwan | last post: by
1 post views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.