• Welcome to Valhalla Legends Archive.
 

Thread Synchronization Question

Started by shadypalm88, March 07, 2005, 08:14 PM

Previous topic - Next topic

shadypalm88

Note:  I'm currently developing under Windows, but any general suggestions would be appreciated.

I'm wondering if there's some type of built-in synchronization facility that meets these requirements.  I have a bunch of threads, and each thread has a data structure assigned to it.  These structures, as they sometimes need to be accessed by other threads, are stored in a global, dynamically-allocated array.  Currently I'm using critical sections to control access.

But that's too restrictive, IMO.  Once the structure is created and its values initialized, it no longer needs to be written to, only read from (except one field that doesn't need to be accessed from outside the owning thread).

So is there any (relatively straightforward) way to:
- Allow read operations to go through "simultaneously", while still signaling some sort of controller that the read operation(s) are in progress, but
- When the array needs to be resized, the resizing function can signal that, blocking out all new read operations and waiting for all pending operations to complete, and then reallocating the memory for the array, unlocking it, and allowing reads to continue.

Basically a tri-state mutex: unlocked, read-only, and fully locked.

Arta

Windows does support this natively, but the API are undocumented. I'm not sure where to look for more info, Skywing could point you the right way though. Multiple Reader Single Writer synchronisation is definitely what you're after, though, and that's what these API are designed for.

Kp

As an extension to Arta's comments, you can build your own SWMR primitive if you can't or don't want to dig through the API documentation.  It's pretty simple, and comes up in most OS classes.
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!