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

Mutex

I'm developing a multi-threaded application which contains a global
collection.
The main thread adds objects to the global collection while the worker
threads (between 10-20) read (for each... next) from the global
collection. I'm getting an error when the worker threads try to read
from the collection while the main thread is either adding or removing
items on the global collection. On the main thread I've tried using a
synclock when adding or removing items from the global collection but
that does not seem to be helping. I remember back in college my prof
was talking about Semaphores, Mutex, and Monitors... is there any such
construct in vb.NET that i can use?

:\\derian
Nov 19 '05 #1
3 6488
"dercon" <de****@astutesolutions.com> wrote in message
news:90**************************@posting.google.c om...
I'm developing a multi-threaded application which contains a global
collection.
The main thread adds objects to the global collection while the worker
threads (between 10-20) read (for each... next) from the global
collection. I'm getting an error when the worker threads try to read
from the collection while the main thread is either adding or removing
items on the global collection. On the main thread I've tried using a
synclock when adding or removing items from the global collection but
that does not seem to be helping. I remember back in college my prof
was talking about Semaphores, Mutex, and Monitors... is there any such
construct in vb.NET that i can use?


Yes, if you check the .NET docs, you will find that there is a Mutex class,
but SyncLock should work. You're probably not using an object that is
available to all threads or is not a ref type. For example, using Me in a
Class is not guarenteed to work with SyncLock, but using the Class name in a
shared method is. One option is to make a lock object collection (or even a
single variable) and place it in a global module. Using the collection
itself as your locking mechanism should even work.

HTH,
Jeremy

Nov 19 '05 #2

"dercon" <de****@astutesolutions.com> wrote in message
news:90**************************@posting.google.c om...
I'm developing a multi-threaded application which contains a global
collection.
The main thread adds objects to the global collection while the worker
threads (between 10-20) read (for each... next) from the global
collection. I'm getting an error when the worker threads try to read
from the collection while the main thread is either adding or removing
items on the global collection. On the main thread I've tried using a
synclock when adding or removing items from the global collection but
that does not seem to be helping. I remember back in college my prof
was talking about Semaphores, Mutex, and Monitors... is there any such
construct in vb.NET that i can use?

:\\derian


First off, you must realize that iteration using For Each, even if
collection is syncronized is not thread safe... Exactly for the reasons you
specify. If a collection is modified by another thread during the
iteration, it invalidates the enumerator and throws an exception. There is
no way around this really except

A. Lock the collection throught the iteration
' code to add
SyncLock globalCollection
globalCollection.Add(new mytype)
end synclock

' code to iterate.
SyncLock globalCollection
For Each item as mytype in globalCollection
' do stuff with item
next
end synclock

With the above you are required to make sure that you use the synclock when
ever you want to add, remove, or iterate items. This is pretty inefficient,
since it is going to cause all threads to wait while one thread iterates the
collection.

B. Catch the exception.

You may want to go with option b. Again, you will need to catch the
exception thrown when the enumerator becomes invalid - in that case it may
be more logical to not use for each. You may want to use the IEnumerator
interface directly. This is air code so, you will definately want play
around with it :)

Dim enumerator as IEnumerator = globalCollection.GetEnumerator()
dim o as myobjecttype

do while true
try
if enumeator.movenext()
o = directcast(enumerator.current, myobjecttype)
' do stuff with object...
else
exit do
end if
catch
enmerator = globablCollection.GetEnumerator()
end try
loop

As for mutex, etc. There is a mutex class, but mutex's are really designed
for process syncronization rather then thread sync. They are global kernal
objects. What you really want is a critical section - which is what the
monitor class in System.Threading provides. Of course you don't often have
to use it directly because SyncLock is really just a shortcut for using
System.Threading.Monitor :) There is also the ManualResetEvent class and
the AutoResetEvent class, which can be used to wait for events to occur on
other threads. One interesting class you may want to look at is the
ReaderWriterLock class. It is sort of like the Monitor class, but is
optimized for use with Single writer, multiple reader scenarious... Anyway,
just some thoughts and ramblings...

HTH,
Tom Shelton
Nov 19 '05 #3
Thanks a lot guys... your post have given me some very helpful
insights. I'll let you know how the final project turns out.

:\\derian
Nov 19 '05 #4

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

Similar topics

0
by: Srijit Kumar Bhadra | last post by:
Hello, Here is some sample code with pywin32 build 203 and ctypes 0.9.6. Best regards, /Srijit File: SharedMemCreate_Mutex_win32all.py # This application should be used with...
5
by: Ken Varn | last post by:
I have a named mutex object that is accessed by both an asp.net application and a Windows executable .net application. The Windows executable runs under the administrator logon, while the asp.net...
193
by: Michael B. | last post by:
I was just thinking about this, specifically wondering if there's any features that the C specification currently lacks, and which may be included in some future standardization. Of course, I...
4
by: TGOS | last post by:
I was thinking about it for a while, a mutex written in C and without disabling any interrupts. Is it possible? typdef struct mutex { unsigned int owner1; unsigend int owner2; } *mutex; ...
2
by: Martin Maat | last post by:
Hi. I want to use the same mutex in different classes (web pages in an ASP.NET application). In global.asax.cs, the class that starts up first, I create a Mutex like this: static private...
16
by: Ed Sutton | last post by:
I use a mutex to disallow starting a second application instance. This did not work in a release build until I made it static member of my MainForm class. In a debug build, first instance got...
2
by: tony.newsgrps | last post by:
Hi there, I'm trying to understand the impact of killing a process that owns a system mutex (used to ensure there is only 1 instance of my program running) Here is my code pretty much: try...
3
by: NaeiKinDus | last post by:
Hello, i'm trying to program a thread that would be locked (by a mutex) and that would only be unlocked once that a function (generating data) is done. The purpose is to generate data, and unlock...
11
by: Lamont Sanford | last post by:
Given an object of type Mutex, what method or property should be called to determine if it is currently owned? I could call WaitOne(0,false) -- and if the return value is false, I could deduce...
2
by: tshad | last post by:
I am running a program as a Windows service which works fine. I am using a Mutex to prevent multiple threads from from accessing my log text file at the same time. It works fine in the Service:...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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: 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:
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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.