<html><!-- Created using the cpp_pretty_printer from the dlib C++ library. See http://dlib.net for updates. --><head><title>dlib C++ Library - threaded_object_ex.cpp</title></head><body bgcolor='white'><pre> <font color='#009900'>// The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt </font><font color='#009900'>/* This is an example illustrating the use of the threaded_object from the dlib C++ Library. This is a very simple example. It creates a single thread that just prints messages to the screen. */</font> <font color='#0000FF'>#include</font> <font color='#5555FF'><</font>iostream<font color='#5555FF'>></font> <font color='#0000FF'>#include</font> <font color='#5555FF'><</font>dlib<font color='#5555FF'>/</font>threads.h<font color='#5555FF'>></font> <font color='#0000FF'>#include</font> <font color='#5555FF'><</font>dlib<font color='#5555FF'>/</font>misc_api.h<font color='#5555FF'>></font> <font color='#009900'>// for dlib::sleep </font> <font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> std; <font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> dlib; <font color='#0000FF'>class</font> <b><a name='my_object'></a>my_object</b> : <font color='#0000FF'>public</font> threaded_object <b>{</b> <font color='#0000FF'>public</font>: <b><a name='my_object'></a>my_object</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <b>{</b> <font color='#009900'>// Start our thread going in the thread() function </font> <font color='#BB00BB'>start</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <b>}</b> ~<b><a name='my_object'></a>my_object</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <b>{</b> <font color='#009900'>// Tell the thread() function to stop. This will cause should_stop() to </font> <font color='#009900'>// return true so the thread knows what to do. </font> <font color='#BB00BB'>stop</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <font color='#009900'>// Wait for the thread to stop before letting this object destruct itself. </font> <font color='#009900'>// Also note, you are *required* to wait for the thread to end before </font> <font color='#009900'>// letting this object destruct itself. </font> <font color='#BB00BB'>wait</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <b>}</b> <font color='#0000FF'>private</font>: <font color='#0000FF'><u>void</u></font> <b><a name='thread'></a>thread</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <b>{</b> <font color='#009900'>// This is our thread. It will loop until it is told that it should terminate. </font> <font color='#0000FF'>while</font> <font face='Lucida Console'>(</font><font color='#BB00BB'>should_stop</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>false</font><font face='Lucida Console'>)</font> <b>{</b> cout <font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>hurray threads!</font>" <font color='#5555FF'><</font><font color='#5555FF'><</font> endl; dlib::<font color='#BB00BB'>sleep</font><font face='Lucida Console'>(</font><font color='#979000'>500</font><font face='Lucida Console'>)</font>; <b>}</b> <b>}</b> <b>}</b>; <font color='#0000FF'><u>int</u></font> <b><a name='main'></a>main</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <b>{</b> <font color='#009900'>// Create an instance of our threaded object. </font> my_object t; dlib::<font color='#BB00BB'>sleep</font><font face='Lucida Console'>(</font><font color='#979000'>4000</font><font face='Lucida Console'>)</font>; <font color='#009900'>// Tell the threaded object to pause its thread. This causes the </font> <font color='#009900'>// thread to block on its next call to should_stop(). </font> t.<font color='#BB00BB'>pause</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; dlib::<font color='#BB00BB'>sleep</font><font face='Lucida Console'>(</font><font color='#979000'>3000</font><font face='Lucida Console'>)</font>; cout <font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>starting thread back up from paused state</font>" <font color='#5555FF'><</font><font color='#5555FF'><</font> endl; <font color='#009900'>// Tell the thread to unpause itself. This causes should_stop() to unblock </font> <font color='#009900'>// and to let the thread continue. </font> t.<font color='#BB00BB'>start</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; dlib::<font color='#BB00BB'>sleep</font><font face='Lucida Console'>(</font><font color='#979000'>4000</font><font face='Lucida Console'>)</font>; <font color='#009900'>// Let the program end. When t is destructed it will gracefully terminate your </font> <font color='#009900'>// thread because we have set the destructor up to do so. </font><b>}</b> </pre></body></html>