Closed symisc closed 8 years ago
The usual rules apply. You can't touch an object instance from multiple threads at the same time. But different instances of the same class can be used in different threads without synchronization. This rule applies to all objects in dlib and the standard C++ library.
Well, I should clarify. You can't mutate an object from multiple threads at once without synchronization. If you knew that accesses to an object were non-modifying then you wouldn't need synchronization. However, the only time you should assume that touching an object in dlib is non-modifying is when the documentation specifically indicates that is the case.
I use the detector() obtained from dlib.get_frontal_face_detector() for a face detection muti-threaded server so I should no worry about synchronization since it is a read-only operation. right?
On 21-Nov-16 01:45, Davis E. King wrote:
Well, I should clarify. You can't mutate an object from multiple threads at once without synchronization. If you knew that accesses to an object were non-modifying then you wouldn't need synchronization. However, the only time you should assume that touching an object in dlib is non-modifying is when the documentation specifically indicates that is the case.
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/davisking/dlib/issues/340#issuecomment-261819336, or mute the thread https://github.com/notifications/unsubscribe-auth/AEZu8GUPZJonweWB1YNds5-AzozoMdrtks5rAOmogaJpZM4K3s3B.
Mrad - Symisc Systems, Suarl, http://symisc.net
Multiple threads can't touch the same object instance. Other than that you can do whatever you want without synchronization.
Just meet the same issue today, face detector is not thread safe, probably due to the image scanner inside. Avoid this by using thread_local.
thread_local dlib::frontal_face_detector face_detector_;
if (face_detector_.num_detectors() == 0) {
face_detector_ = dlib::get_frontal_face_detector();
}
If you are going to do that you might as well write it like this:
thread_local dlib::frontal_face_detector face_detector = dlib::get_frontal_face_detector();
Yes, both work!
get_frontal_face_detector()
invocation is very slow, that's the reason it was asked if it was thread safe or not, I think. Question: is there a way to perform the slow deserialization once and then quickly instantiate how many detectors needed quickly, by sharing as much read-only data as possible?
You can copy objects in c++ and make as many instances you like.
Thank you, I noticed there was copy constructor. Is there some data sharing between the instances or it's deep copy?
Unless noted otherwise there is never hidden data sharing in dlib.
Thank you. Immutable data sharing is welcome, if any.
Hi, Is the detector obtained from dlib.get_frontal_face_detector() can be shared between multiple threads operating on it simultaneously or we should use thread locking to synchronize access?