473,563 Members | 2,722 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.TryEnt er(syncRoot, mSecondsToWait) )
{
//only add if not flushing or if no other item is being added
try
{
buffer.Add(item );
}
finally
{
Monitor.Exit(sy ncRoot);
}
}
else
{
ThreadPool.Queu eUserWorkItem(d elegate(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 1029

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

Similar topics

1
2056
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 be queued at a time - as per documentation). But, when the main application exits, then all the executing methods are also terminated. How do I...
1
1562
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 this. Having tried something similiar previously, using the threadpool class to queue my requests. Does the WebRequest class require a spare...
0
955
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 Compact Framework, it doesn't support this method, only support Monitor.TryEnter(Object). Can anyone help to solve this problem: get a...
5
3816
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
7659
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...
0
7580
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...
1
7634
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7945
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6244
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...
1
5481
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...
0
3634
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...
1
2079
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
1
1194
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.