in reply to Threads sharing global variable

Regarding the motivation of this exercise. Although I am not the original poster, I see there's a need to provide some further background.

The readers-writers problem is an elementary problem in concurrency, and the typical solution involves the use of condition variables. The construct has its origins in 1974/1975 when Hoarse and Hansen formulated the idea of monitors.

The Art of Multiprocessor Programming introduces this problem in Chapter 1. Their example has Alice and Bob place sentences on a billboard one letter at a time.

"Both the mutual exclusion and producer-consumer protocols require waiting: if one participant is subjected to an unexpected delay, so is the other. ... Surprisingly, the readers-writers problem does have solutions that do not require waiting.

The scope of this exercise is no doubt to provide a working demonstration based on the method of condition variables. Again, the wikipedia article should prove informative.

Replies are listed 'Best First'.
Re^2: Threads sharing global variable
by BrowserUk (Patriarch) on Mar 10, 2016 at 23:42 UTC
    The readers-writers problem is an elementary problem in concurrency, and the typical solution involves the use of condition variables. The construct has its origins in 1974/1975 when Hoarse and Hansen formulated the idea of monitors.

    Quoting ancient history serves no one any good. The world has move on -- a lot -- in the four decades since 1974/5. Especially with respect to concurrency and shared data.

    "Both the mutual exclusion and producer-consumer protocols require waiting:

    Read. Learn. Enjoy. Wait free algorithms.

    Some choice quotes:

    Wait-freedom is the strongest non-blocking guarantee of progress, combining guaranteed system-wide throughput with starvation-freedom. An algorithm is wait-free if every operation has a bound on the number of steps the algorithm will take before the operation completes.12 This property is critical for real-time systems and is always nice to have as long as the performance cost is not too high.
    Wait-free algorithms were rare until 2011, both in research and in practice. However, in 2011 Kogan and Petrank15 presented a wait-free queue building on the CAS primitive, generally available on common hardware. Their construction expanded the lock-free queue of Michael and Scott,16 which is an efficient queue often used in practice. A follow-up paper by Kogan and Petrank17 provided a methodology for making wait-free algorithms fast and used this methodology to make the wait-free queue practically as fast as its lock-free counterpart. A subsequent paper by Timnat and Petrank18 provided an automatic mechanism for generating wait-free data structures from lock-free ones. Thus, wait-free implementations are now available for many data-structures.

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re^2: Threads sharing global variable
by Anonymous Monk on Mar 11, 2016 at 14:18 UTC

    I enjoyed the problem presented by the OP and also the history. Thank you for sharing.