I want to translate some Java code into C++. One of the Java classes extends the Thread class and contains the following method:
public static synchronized String createUniqueID() {
//some code here
}
How can I synchronize (in the Java sense of the word) class methods in C++ using Boost? I have read about using boost::mutex for synchronizing access to shared data, but I am not sure how to apply this to C++ class methods.
The following is equivalent to a Java
synchronizedmethod in C++. In fact, it is exactly equivalent, with the obvious exceptions that it is written in a different language and a different threading library.Note that the mutex is protected, not private, allowing you to use the same mutex (as you should) in subclasses.