davisking / dlib

A toolkit for making real world machine learning and data analysis applications in C++
http://dlib.net
Boost Software License 1.0
13.59k stars 3.38k forks source link

Detector Thread Safety #340

Closed symisc closed 8 years ago

symisc commented 8 years ago

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?

davisking commented 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.

davisking commented 8 years ago

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.

symisc commented 8 years ago

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

davisking commented 8 years ago

Multiple threads can't touch the same object instance. Other than that you can do whatever you want without synchronization.

dongwang218 commented 7 years ago

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();
    }
davisking commented 7 years ago

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();
dongwang218 commented 7 years ago

Yes, both work!

ceztko commented 5 years ago

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?

davisking commented 5 years ago

You can copy objects in c++ and make as many instances you like.

ceztko commented 5 years ago

Thank you, I noticed there was copy constructor. Is there some data sharing between the instances or it's deep copy?

davisking commented 5 years ago

Unless noted otherwise there is never hidden data sharing in dlib.

ceztko commented 5 years ago

Thank you. Immutable data sharing is welcome, if any.