How to make global data thread safe ? Please Help...

Hi friends,

I seem to be stuck in a peculiar problem.

I have a public static class with public static members (different array lists). The members are thus global in nature and accessible across the namespace.

I have a worker function which is called asynchronously (so that there are several instances running per second) and which reads the different global members at different points of execution (not the entire set at any one point).

I have a writer function which is used to set the global members and this is the only one in the application writing to these variables. It will write the entire set of globals when given control through a GUI event.

I need a mechanism where if one instance of worker function is accessing any of the global members and the writer function is called, all instances of worker function (which I assume would be different threads) should complete their read operation and wait before next read (of another global member) until the writer function is finished updating all the globals. This must also mean that the writer function wait till all instances of read operation are complete and then go ahead to update the variables.

I have gone through Lock, Mutex, Semaphore but feel quite confused about the type of model that I should implement. Please help me.

Thanks n Regards,

Kunal




Answer this question

How to make global data thread safe ? Please Help...

  • orent

    Sorry, that was a typo. Thanks for pointing it out, I have corrected it.

  • rayou

    The C# lock keyword is the same as using Monitor.Enter and Monitor.Exit. A semaphore is like a throttle, you don't use it for exclusive access.

    It sounds like what you want is a ReaderWriterLock.



  • Porko

    > The C# lock keyword is the same as using Mutex.Enter and Mutex.Exit.

    The lock keyword utilizes the Monitor class, not the Mutex class to lock a critical area.

    Mark.



  • How to make global data thread safe ? Please Help...