multithreading - Threaded implementation of observer pattern - C++ -


I am developing a C ++ program that has a "scan" method that has long been running the scanning process Will speed up. When the process is completed, the scan method will inform the supervisors of the results using the supervisor pattern.

I want to make a separate thread for each scan. In this way, I can run many scans together when each scanning process is completed, then I would like the scan method to inform the listeners.

According to the Boost Thread Library, it seems that I can do something like this:

  #include & lt; Boost / thread / thread.hpp & gt; # Include & lt; Boost / thread / mutex.hpp & gt; # Include & lt; Boost / bind.hpp & gt; # Include & lt; Iostream & gt; Boost: Mute x io_mutex; Zero scan (int scan _ target, vector & lt; lister; listeners) {//..run scan (boost :: mutex :: scoped_lock lock (io_mutex); std :: cout & lt; "end scan" & Lt; endl; // Notify the listeners by going through the vector again and "notify ()}} the main points (int argc, char * argv []) and {vector & lt; audience / listeners // boost :: thread thrd1 (boost :: bind) (and scan, 1, listeners)); Promotion :: thread thrd2 (boost :: bind (and scan, 2, listeners)); //thrd1.join ( ); //thrd2.join (); Returns 0;}  

Does this look almost right? Do I need to call listeners? Is it OK to get rid of joining?

You Whether the lock is required or not, it depends on you what you do with the notification. I think that if you lock the function to only those listeners where you need a single threaded access.

< / Div>

Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -