473,500 Members | 1,712 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

TryEnter and Threadpools

Hi,

I'm trying to add to a buffer, that might be in use by other threads. To
synchronise around the buffer, I'm using a monitor. However, if the add
takes too long, I'd like to delegate it to a threadpool thread and then
carry on doing my own thing:

int mSecondsToWait = 100; // try for 0.1 sec

if (Monitor.TryEnter(syncRoot, mSecondsToWait))
{
//only add if not flushing or if no other item is being added
try
{
buffer.Add(item);
}
finally
{
Monitor.Exit(syncRoot);
}
}
else
{
ThreadPool.QueueUserWorkItem(delegate(object state)
{
lock(syncRoot){buffer.Add(item);}
});
}

My questions are:

1) Is this the best way to do this? This request to add might be called many
times per second, and so calls to buffer.Add might pile up. That's when I'd
like to call a thread. Can you ask the monitor how many threads are waiting
on it already?
2) Whats the best amount of time to wait for an exclusive lock? My initial
thought is that I should only wait as long as it takes to create a thread,
but then there's no way of knowing when a lock might be relinquished so
that's kind of arbitrary. I guess I should wait for the lenth of time it
takes to add an item to the buffer. Should I wait at all?
3) This strategy doesn't retain ordering of items added (since the next item
in queue might get the lock if the front gives up and spawns a thread). Is
there a strategy that does?

Thanks!

Shak
Oct 16 '06 #1
0 1028

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2053
by: Peter Kirk | last post by:
Hi there, I have a question regarding ThreadPools. Say I have the following program, which has a timer which on every "tick" queues a method for execution. This works ok (and up to 25 methods can...
1
1561
by: Martin Brindle | last post by:
Hello there. I'm after using a series of threads, from a threadpool, via async delegates, to download files. I've noticed a few other people on here doing the same thing, but my question is...
0
944
by: phillin77 | last post by:
My library need to be used both on .NET Framework and .NET Compact Framework. In .NET Framework, I use Monitor.TryEnter(Object, millisecondsTimeout) to get into a lock or timeout. But in .NET...
5
3807
by: Rainer Queck | last post by:
Hello NG, is there a possibillity to determine how long a thread had to wait on Monitor.TryEnter(lockObj,timeout) ? Thanks for hints. Rainer Queck
0
7136
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
7018
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...
0
7232
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...
0
5490
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,...
1
4923
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...
0
3110
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...
0
3106
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1430
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 ...
0
316
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...

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.