
My idea was to create a mutex for this (lock before the call, unlock after it)... I think I'd probably create a class to manage access to the shared function. The class would contain the mutex, and the method that calls the C function would instantiate the lock object. Nobody else would directly call the C function any more.
I ended up reaching the same conclusion here, as I wanted to store other persistent data too (in this case a counter for the number of times the function was called) and wrapping it all up in a class does make it cleaner, as you suggest.
Since you already have a way to pass mutex references to your handful of objects, though, you could pass a reference to your wrapper object instead.
Yes, I think I prefer this method, as I would rather choose to only have one instance of the class floating around, as opposed to using a Singleton pattern with its additional complexity to enforce this rule. (I'd never heard of the Singleton pattern before - interesting!)
I'd probably allocate it on the heap, pointing to it with a boost::shared_ptr. Store a copy of that boost::shared_ptr in each of your handful of objects. Ownership is shared among them, and the last one to leave turns out the lights.
I agree - however this time it will be around for the life of the (fairly simplistic) program, so it'll be just as easy to create it as a local variable in main(), wait for the threads to terminate and then have it destroyed automatically. Thanks for your reply! Cheers, Adam.