Using a shared resource

Hi,

The problem I have is not specifically related to C# or its implementation. Its more of a theoritical nature, but since I have been frequenting this forum as well as building my project in C#, I thought it d be a better place to ask it.

The problem is this:

There are three classes: A, B and C.

They are a sort of component classes and the class that encompasses these classes (the bigger system) is, lets say, class BigSystem.

Means the BigSystem class has objects of each of A, B and C.

There is a shared resource (for e.g. a file or a serial port) that could be accessed by only one class at a time. To counter this, we make a class AReader that is a sort of wrapper class. This class could only be accessed by A, B or C.

Now how do we access the resource, when wanted to by the separate classes Do we make BigSystem static Is there any other way




Answer this question

Using a shared resource

  • gregaug

    Salam Yousaf

    you said that u have a shared resource (i.e. a File or a Serial Port) that could only be accessed by one class at a time. You created a wrapper class around that recource, now to do this in a serial order, maintain a queue for using the resource.

    Each of the class (i.e. A, B, C) when want to use the resource they are added to the Resource Queue.

    You can make the Wrapper class as a Static class and make it methods as static also, then can utilize the queue like for e.g ResourceQueue.Add(/*class*/), and get the result of the operation like ResouceQueue.GetResult().

    Hope this might help.



  • Using a shared resource