Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Problem:
(For Windows OS only)

One thread is writing a variable 'count' and 20 threads have to read this variable within a time frame of 30 milli seconds.
The writer thread should write the new value of 'count' in the next cycle, only after the 20 reader threads have read the current value of 'count' in the current cycle of 30 milliseconds.
The same for reader threads too. Reader threads have to read the new value of 'count' in the current cycle only after the writer thread has updated the previous value of 'count' in the current cycle.

Current situation:

The writer thread is updating the variable 'count' in current cycle. Before all 20 reader threads read the value of 'count' in this cycle, writer thread updates the value of 'count' and hence the remaining reader threads are reading the wrong value missing out the correct value.

(1) How to synchronize these threads in the following serial way -

1 writer thread writes the variable 'count', then 20 reader threads read it, then again 1 writer thread writes, then 20 reader threads read ?

The reader threads count is not fixed to 20. Sometimes there may be only 5 reader threads, sometimes there may be 15.

(2) Which synchronization object to use and how ? All threads are in the same application.
Posted

This can be handle using Events.

All the reader threads will wait for a single event created by Writer thread , which it signals after updating the value of count.
Multiple threads can wait using (WaitforSingleObject).

After writing the writer thread can wait for multiple events using WaitforMultipleObjects, where every read thread signals its own event after reading the count, & after all the events are signaled only then the Writer thread writes again.

Also inorder to ensure sync you can use interlockedexchange for the values to be written & read to & from the variable.
 
Share this answer
 
Sounds like a semaphore to control when the master thread updates the count (after each write it's raised by the number of threads and each thread lowers it when it's processed the count [1]).

You might want a mutex to synchronise access to the count as well.

Cheers,

Ash

[1] Or is it the other way around? Can't remember the exact semantics at the mo.
 
Share this answer
 
Please observe it :) :
class CDataChest
{ 
  // One lock for the all operations
  CCriticalSection m_cCriticalSection;

  // The data
  int m_iCount;

  // The writer was here, the data may be read but not written
  bool m_bWritten;

  // Map of the readers' read-traces,
  // which are possible when the m_bWritten is true;
  // the last trace reset all traces and set m_bWritten to false
  CMap<HANDLE, HANDLE, bool, bool> m_RedersDoneFlags;

public:
  CDataChest();

  // Readers' function, can return false as well
  bool GetCount(HANDLE hReaderThread, int& iCount);
  // Writer's function, can return false as well
  bool SetCount(int iCount);

  // CheckOut of a reader thread (CheckIn was at the first GetCount())
  void RemoveReader(HANDLE hExitingReaderThread);
};

When you will need more explanations - just request it :)
 
Share this answer
 
In your case, I think it may be a better way to follow those steps:
1st: Write thread change the variable "count" , for exampe: count = 1;
2nd: Write thread broadcast a user defined message to all Read threads, whether they need the variable or not.(Sometimes there may be 5, or 15)
3rd: when received the broadcast message, each read thread must send a reply message to the write thread, if the read thread do not use the variable, it must send a "not use" reply, else it must send a "received and finish" reply after it received and finished use the variable.
4th: when the write thread received all the message, it can change the variable "count" , for exampe: count = 2; and repeat to the 2nd step.

else method:
you can use Semaphore.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900