condition variable on read/write locks

This is a discussion on condition variable on read/write locks within the C Programming forums, part of the General Programming Boards category; Hello. I have the following problem: I have one writter and many readers. The writter writes in a buffer and ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    11

    condition variable on read/write locks

    Hello. I have the following problem: I have one writter and many readers. The writter writes in a buffer and the readers read from this buffer. It's not classicle producer/consumers, because all the readers need to read all the data that the writter has written. I implemented it using read/write lock: Write lock for writting and read lock for reading. The problem is that when there's no data to be read I must use busywait or sleepwait. If condition variables worked with read/write locks it would be easy: Every write sygnals, if the reader has read all the data it blocks on condition variable... but no, there isn't condition variable for read/write locks. So my question is: Is there any way to avoid sleepwait with some mechanism given the situation? I must not use mutexes, because there are many many readers. Thanks

  2. #2
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Why not use Events? Check out CEvent, and the win32 equivalent. Your threads can block on an event without using a lot of CPU and be awakend by some master when it chooses.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    11
    I use the posix standart. What is this library with the events? I've never heard of it.

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,643
    >> The problem is that when there's no data to be read I must use busywait or sleepwait
    When writers have nothing to write, keep the write-lock acquired. This will keep the readers efficiently blocked while there's nothing to read.

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SDL Condition variables.
    By antex in forum Game Programming
    Replies: 3
    Last Post: 11-11-2005, 06:11 AM
  2. static class variable vs. global variable
    By nadamson6 in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2005, 03:31 PM
  3. write Variable and open Variable and get Information
    By cyberbjorn in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2004, 01:30 AM
  4. Need help
    By awkeller in forum C Programming
    Replies: 2
    Last Post: 12-09-2001, 02:02 PM
  5. Variable Allocation in a simple operating system
    By awkeller in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 01:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21