java - Signal between threads -


this interview question in pseudo code. told there problems in following approach. not find else other holding main thread while event waiting. can guys see real issues?

here question:

there main thread , child thread. child thread monitoring on 30k message, sent in 1k chunks external source. once child thread sees 1k chunk ready, signals main thread function retrieve data. pseudo code is:

// method called in main thread  void mainthreadfunction(out message) {        var buffer;      loop     {         event.wait;             read data buffer;         event.release;          if (all data of message complete)         {             exit loop;         }     }      copy buffer message; }  // method in child thread  void childthreadfunction() {      // once 1k chunk of data ready     event.set; } 

the child thread not wait main thread read data, can start overwriting new data before old data read.

typically in kind of situation, maintain 2 or more buffers child thread can write into. while main thread reading one, child thread writing next. when buffers full, child thread has wait main thread finish one.

see: https://en.wikipedia.org/wiki/multiple_buffering


Comments

Popular posts from this blog

android - Why am I getting the message 'Youractivity.java is not an activity subclass or alias' -

python - How do I create a list index that loops through integers in another list -

c# - “System.Security.Cryptography.CryptographicException: Keyset does not exist” when reading private key from remote machine -