diff --git a/dlib/iosockstream/iosockstream.h b/dlib/iosockstream/iosockstream.h index e8b1a68ff..c9c51ac0b 100644 --- a/dlib/iosockstream/iosockstream.h +++ b/dlib/iosockstream/iosockstream.h @@ -59,6 +59,7 @@ namespace dlib const network_address& addr ) { + auto_mutex lock(class_mutex); close(); con.reset(connect(addr)); buf.reset(new sockstreambuf(con.get())); @@ -79,6 +80,7 @@ namespace dlib unsigned long timeout ) { + auto_mutex lock(class_mutex); close(timeout); con.reset(connect(addr.host_address, addr.port, timeout)); buf.reset(new sockstreambuf(con.get())); @@ -91,6 +93,7 @@ namespace dlib unsigned long timeout = 10000 ) { + auto_mutex lock(class_mutex); rdbuf(0); try { @@ -126,12 +129,21 @@ namespace dlib unsigned long timeout ) { + auto_mutex lock(class_mutex); if (con) { con_timeout.reset(new dlib::timeout(*this,&iosockstream::terminate_connection,timeout,con)); } } + void shutdown ( + ) + { + auto_mutex lock(class_mutex); + if (con) + con->shutdown(); + } + private: void terminate_connection( @@ -142,6 +154,7 @@ namespace dlib } scoped_ptr con_timeout; + rmutex class_mutex; shared_ptr_thread_safe con; scoped_ptr buf; diff --git a/dlib/iosockstream/iosockstream_abstract.h b/dlib/iosockstream/iosockstream_abstract.h index d5ff61e8a..2328f426e 100644 --- a/dlib/iosockstream/iosockstream_abstract.h +++ b/dlib/iosockstream/iosockstream_abstract.h @@ -22,8 +22,10 @@ namespace dlib stream's output buffers. THREAD SAFETY - It is not safe to touch this object from more than one thread at a time. - Therefore, you should mutex lock it if you need to do so. + It is not safe for multiple threads to make concurrent accesses to the same + instance of this object (except for calls to shutdown() which are always + threadsafe). Therefore, you should mutex lock an instance of this object + if you need to touch it from multiple threads. !*/ public: @@ -147,6 +149,15 @@ namespace dlib - This function has no effect on this object. !*/ + void shutdown ( + ); + /*! + ensures + - Immediately closes the TCP connection and causes all I/O operations on + this object to return an error. + - It is safe to call this function from any thread, therefore, you can use + it to signal when you want a connection to terminate from another thread. + !*/ }; // ----------------------------------------------------------------------------------------