samedi 25 avril 2015

Thread Safe Buffers / POD

My problem is quiet common I suppose, but it drives me crazy:

I have a multi threaded application with 5 threads. 4 of these threads do their job, like network communication and local file system access. They all write their output in a data structure like this:

struct Buffer {
  std::vector<std::string> lines;
  bool has_been_modified;
}

And there is one thread that prints these buffers to screen:

Buffer buf1, buf2, buf3, buf4;

...

if ( buf1.has_been_modified || 
     buf2.has_been_modified || 
     buf3.has_been_modified || 
     buf4.has_been_modified )
{
  redraw_screen_from_buffers();
}

So now my question is, how do I protect/encapsulate the buffers to be only modified, when redraw_screen_from_buffers() isn't reading their data, so on screen the can't be half modified output.

I can't find a proper solution, although I think this has to be a quiet common problem.

Greetings

Aucun commentaire:

Enregistrer un commentaire