473,694 Members | 2,895 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.wheth er 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 2757
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.TryEnte r(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.co m

"rakesh_nit s" <co*********@gm ail.com> wrote in message
news:11******** *************@g 49g2000cwa.goog legroups.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.wheth er 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.Ent er method. This method does the following thing:
- Calls Montior.TryEnte r 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.Exi t. 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_nit s" <co*********@gm ail.com> wrote in message
news:11******** *************@g 49g2000cwa.goog legroups.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.wheth er 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.wheth er 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
1662
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 with these functionnalities: - FTP synchronization - synchronize ONLY updated files!!
5
15575
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. One function of the Hashtable is to iterate through its keys, which apparently is inherently not thread-safe. Other functions of the Hashtable include adding/modifying/deleting. To solve the synchronization issues I am doing two things: 1. Lock...
4
3195
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 work across multiple processes just the one. I was wondering if any one new which one used the least overhead. Im at current using mutexes but was wondering if there was something a bit
0
3484
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 Authentication to replicate with the publication and indeed had SQL Authentication working flawlessly. Then our client asked us to use Windows Authentication instead. For incremental changes, Windows authentication works fine. When trying to do the...
5
2676
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 void incre(){ if(x < 20) x++; } } class Form1: WinForm{
12
2005
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 that they all wait until they've all done one job before starting off on each of their next jobs. I have been thinking about this for a day and can't seem to find a solution.
0
1695
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 the synchronization of Web pages/content. ~ What some frameworks do is database development and then let the databases themselves do the synchronization (Should I update my knowledge of this subject ;-))
0
1974
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 a LOT of synchronization overhead. After a bit of digging I concluded that this synchronization has to do with the access to a global locale inside the stream. The problem can be seen by running the small distilled benchmark code
3
6929
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
2711
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 server with fixed IP address provided by our ISP, while the other sites (which I wish in the future hold the replicas databases) have only standard internet connections with Dynamic IP (which means that they change IP addresses, as given by the ISP...
0
8616
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8552
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8971
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7650
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6481
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4332
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2992
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2249
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1970
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.