473,385 Members | 1,536 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,385 software developers and data experts.

threads in .net and protecting shared variables

I was ok understanding threads in Delphi pre .net, but reading through a
reference book and comparing it to what help file says has confused me

A System.Threading.Timer thread in one class calls an event in a 2nd class
as follows (fast timer)

public void OnDriverSendResult(rackdetail RackDetail)
{
// Add new RackDetail to linked list and leave quickly
RackDetail.Previous = this.LastDetail;
if (this.LastDetail == null)
this.FirstDetail = RackDetail;
else
this.LastDetail.Next = RackDetail;
this.LastDetail = RackDetail;
}

In the 2nd class, I will have another system.threading.timer thread (slow
timer) that takes the linked list created by the first timer, processes the
data in it and posts it to a database.

private void ProcessList()
{
if (FirstDetail == null) return;

RackDetail Unprocessed = FirstDetail;
FirstDetail = null;
Processed = Process(Unprocessed);
Post(Processed);
}

How do I protect shared variables/objects in .net threads please. Some code
samples would be more useful to me in the first instance.

thanks
Claire
Nov 17 '05 #1
3 1909
Take a look at Monitor/Enter/Exit and lock(which uses Monitor
internally) to do synchronization. This is similar to Critical Section
in Win32. Another option to synchronize data is to use WaitHandle
derived classes, Mutex, ManualResetEvent and AutoResetEvents.

------
Ajay Kalra
aj*******@yahoo.com

Nov 17 '05 #2
Ajay Kalra wrote:
Take a look at Monitor/Enter/Exit and lock(which uses Monitor
internally) to do synchronization. This is similar to Critical Section
in Win32. Another option to synchronize data is to use WaitHandle
derived classes, Mutex, ManualResetEvent and AutoResetEvents.


Or, use "lock", which is inherently exception-safe and (in this case)
pretty easy to read and understand.

You do realize that your OnDriverSendResult is not re-entrant -- even on
it's own?

You seem to be using a self-implemented linked list, why not use one of
the supplied data-structures in .NET?

BTW: this is the classic producer/consumer problem.

An easy solution, which should perform very well too, is sketched below.

The AutoResetEvent below is entirely optional code, but usefull if you
have a dedicated thread for running ProcessList.

readonly IList Queue; // Use ArrayList or LinkedList or whatever
readonly AutoResetEvent QueueNotEmpty = new AutoResetEvent();
public void OnDriverSendResult(RackDetail detail) {
lock ( Queue ) {
Queue.Add(detail);
QueueNotEmpty.Set();
}
}

private static IList emptyList; // choose any empty list to init with
public void ProcessList() {
IList process;
QueueNotEmpty.WaitOne();
lock ( Queue ) {
if ( Queue.GetEnumerator().MoveNext() ) { // non-empty?
process = new ArrayList(l);
Queue.Clear();
} else {
process = emptyList;
}
foreach ( RackDetail detail in process )
Process(detail);
}

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Nov 17 '05 #3
> You do realize that your OnDriverSendResult is not re-entrant -- even on
it's own?


Thanks for your help guys.
I don't know if you'll look back at this thread Helge but I've no idea what
you meant by the above (even though I looked it up on
http://dictionary.reference.com/search?q=re-entrant ). Can you explain for
this case please. thanks
Nov 17 '05 #4

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

Similar topics

1
by: Dennis Gavrilov | last post by:
Hi, All! I have two questions: strategic and technical. Technical one first: I need to share an array of objects (implemented as hashes, having references to other objects and hashes, sharing...
0
by: Al Tobey | last post by:
I was building perl 5.8.2 on RedHat Enterprise Linux 3.0 (AS) today and noticed that it included in it's ccflags "-DTHREADS_HAVE_PIDS." I am building with -Dusethreads. With newer Linux...
28
by: Dennis Owens | last post by:
I am trying to run a thread off of a form, and every once in a while the thread will raise an event for the form to read. When the form gets the event, the form will place the event into a dataset...
6
by: ouech | last post by:
hi, I'd like to know if i need mutexs to lock the use of member methods of a class instance shared between several threads via a pointer? if the method modify member variables, i know that...
3
by: Andreas Müller | last post by:
i need two loops that run forever. one of it receives data and stores it to a vector. the other one writes the elements of the vector to the disk. this means the vector is a receiver buffer. How...
2
by: Oenone | last post by:
I'm upgrading a DLL from VB6 to VB.NET. The DLL gets called from an ASP.NET web application. In the VB6 code there is a module-level object which stores the context about what the user is doing...
6
by: James Radke | last post by:
Hello, I have a multithreaded windows NT service application (vb.net 2003) that I am working on (my first one), which reads a message queue and creates multiple threads to perform the processing...
9
by: Bob Day | last post by:
VS 2003, vb.net , sql msde... I have an application with multiple threads running. Its a telephony application where each thread represents a telephone line. For code that would be the same...
167
by: darren | last post by:
Hi I have to write a multi-threaded program. I decided to take an OO approach to it. I had the idea to wrap up all of the thread functions in a mix-in class called Threadable. Then when an...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?

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.