I'm developing a C++ program which has a "scan" method which will trigger a relatively long running scanning procedure. When the procedure is completed, the scan method will notify observers of results using the observer pattern.
I would like to create a separate thread for each scan. This way I can run multiple scans simultaneously. When each scanning process completes, I would like the scan method to notify the listeners.
According the boost thread library, it looks like I can maybe do something like this:
#include <boost/thread/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/bind.hpp>
#include <iostream>
boost::mutex io_mutex;
void scan(int scan_target, vector<Listener> listeners)
{
//...run scan
{
boost::mutex::scoped_lock
lock(io_mutex);
std::cout << "finished scan" << endl;
// notify listeners by iterating through the vector
// and calling "notify()
}
}
int main(int argc, char* argv[])
{
vector<Listener> listeners
// create
boost::thread thrd1(
boost::bind(&scan, 1, listeners));
boost::thread thrd2(
boost::bind(&scan, 2, listeners));
//thrd1.join();
//thrd2.join();
return 0;
}
Does this look roughly correct? Do I need to mutex the call to listenrs? Is it ok to get rid of the joins?