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

SVG stroke-linecap doesn't work for circles in Firefox? -

routes - Laravel 4 Wildcard Routing to Different Controllers -

cross browser - XSLT namespace-alias Not Working in Firefox or Chrome -