473,399 Members | 3,919 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 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 2743
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: WebRod | last post by:
Hi, i am tyring to find a solution to synchronize 2 environnement for months!! How do you synchonize one (or several!!) production environnement from DEV???? I am looking for a software...
5
by: Cyrus | last post by:
I have a question regarding synchronization across multiple threads for a Hashtable. Currently I have a Threadpool that is creating worker threads based on requests to read/write to a hashtable....
4
by: scott | last post by:
hi all, Thx to any one that can offer me help, it will be much appreciated. iv got a multithreaded program and need to use thread synchronization. The synchronization does not have to...
0
by: Rod | last post by:
I orginally posted this to microsoft.public.sqlserver.ce but had not received any responses. I have a CF.NET application (C#) with a SqlCE database. We had originally planned to use SQL...
5
by: fei.liu | last post by:
Hello, in the application I am developing, I am having trouble to synchronize event triggered actions using 'lock(ob){...};' technique. Here is a outline of my code: class C{ int x = 0; public...
12
by: emma_middlebrook | last post by:
Hi Say you had N threads doing some jobs (not from a shared queue or anything like that, they each know how to do their own set of jobs in a self-contained way). How can you coordinate them so...
0
by: lbrtchx | last post by:
Say you need to serve the same Web content from a number of IP address, which you need to keep out there with high availability requirements ~ I think Web Services would be a good candidate for...
0
by: sundman.anders | last post by:
Hi all! I have a question about thread synchronization and c++ streams (iostreams, stringstreams, etc). When optimizing a program for a multicore processor I found that stringstream was causing...
3
by: CKKwan | last post by:
Dear All, Can synchronize a class, any function is called and the entire class is locked. Can synchronize a method What if I need to Lock a class only when specific method is call?
15
by: ingejg | last post by:
I am starting to study internet synchronization, and my head is still spinning since internet is not my forte, however my boss is breathing down my neck at the moment. Our company has only one...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.