protection of common resource from multiple theads

jdcoburn
jdcoburn used Ask the Experts™
on
hi -- I'm using .Net 4.0 and VS2010 with C#. I have a primary thread (the UI thread) which has in it a driver-  handler to a USB based external device. I access the device so that only one post is outstanding at a time, which works fine by itself. I also have a background thread (a monitor thread) that needs to access the same external resource on a periodic basis. Where i run into trouble is with one thread posted and another overlaps the outstanding message.

I've played with various interlocking schemes, including using WaitOne, but i still have problems. So, I'm hoping you can suggest how this should work. I have a feeling i've been placing the WaitOne in the wrong place (I put it in the primary thread at the top of the driver call).  I think it should go in the background thread with access controlled by set and reset in the primary code. is this correct? if so, how do i stop the foreground thread from accessing the resource while the background thread is using it? Do i use two WaitOne's with two different interlocks?

thanks,
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
I don't know the exact answer to your C# question, but I might be able to offer a suggestion based on my multi-threading experience in C++.

The way I've programmed multi-threading is that you don't associate access control with a particular thread, but instead associate access control with the resource.  As an example, rather than sticking an access control into one thread or the other, you create a class to interface with your resource that both threads utilize.  You place the access control inside this interface class, and create one instance of the interface object that both threads use to gain access to the resource.

Now you don't necessarily have to have all access to the resource go through this interface class.  Logically, the interface class can simply act as a traffic cop.  When the traffic cop gives permission to access the resource, the calling thread then simply accesses the resource directly.  In such a setup, you simply have the calling threads obtain permission from the cop to access the resource, and once the calling thread is finished with the resource, it must tell the traffic cop the thread is done with the resource so that the traffic cop can grant access to the next request.

HTH
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009
Commented:
Create an Object that can be accessed from both Threads, then use the "lock" statement against that Object whenever you access the shared resource:
http://msdn.microsoft.com/en-us/library/ms173179.aspx
http://msdn.microsoft.com/en-us/library/c5kehkcz.aspx

Author

Commented:
I can't get to either link.
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
?...they're working fine for me.  Are you behind a corporate firewall with some kind of filtering?

Here's another one:
http://www.dotnetperls.com/lock

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial