Generic Multithread Mover

The generic class template< class Interface> Mover in the namespace Multithread is used to apply a function to a collection of objects. It can use several threads to do this. At the very beginning, the threads are created upon the first call to the member function do_moves. The objects are placed on a queue and each thread keeps pulling objects off the queue and executing the function until the queue is empty. The threads then wait until the next time do_moves is called. This might be useful for a wide variety of situations where one must repeatedly run simulations on several copies for a while, stop to do something else like data gathering, and resume the simulations. Upon destruction of the Mover object, the threads are destroyed.

One big issue is what happens in case of an error. In serial code, one can print a message and exit, or throw an exception. It's more complicated in multithreaded code, because you want to shut down the other threads in a graceful manner; otherwise, debugging can be a nightmare. This code assumes that if an error occurs in the user-provided do_move function, it will throw a C++ exception of the type provided by the Interface class. When this happens, the thread in which the error occurs shuts down the other threads, and rethrows the exception to be caught once more by the user's code.

The code is used by including the header file multithread_mover.hh, which in turn pulls in other headers files in the same directory. The code must also be linked to the compiled result of barrier.cc. Both of these files are in the src directory.

The Interface class, provided by the user, must have the following members:

The Mover has the following member functions: