<html><!-- Created using the cpp_pretty_printer from the dlib C++ library. See http://dlib.net for updates. --><head><title>dlib C++ Library - multithreaded_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 multithreaded_object. This is a very simple example. It creates 3 threads that just print messages to the screen. Example program output: 0 INFO [1] mto: thread1(): hurray threads! 0 INFO [2] mto: thread2(): hurray threads! 0 INFO [3] mto: thread2(): hurray threads! 700 INFO [1] mto: thread1(): hurray threads! 800 INFO [2] mto: thread2(): hurray threads! 801 INFO [3] mto: thread2(): hurray threads! 1400 INFO [1] mto: thread1(): hurray threads! 1604 INFO [2] mto: thread2(): hurray threads! 1605 INFO [3] mto: thread2(): hurray threads! 2100 INFO [1] mto: thread1(): hurray threads! 2409 INFO [2] mto: thread2(): hurray threads! 2409 INFO [3] mto: thread2(): hurray threads! 2801 INFO [1] mto: thread1(): hurray threads! 3001 INFO [0] mto: paused threads 6001 INFO [0] mto: starting threads back up from paused state 6001 INFO [2] mto: thread2(): hurray threads! 6001 INFO [1] mto: thread1(): hurray threads! 6001 INFO [3] mto: thread2(): hurray threads! 6705 INFO [1] mto: thread1(): hurray threads! 6805 INFO [2] mto: thread2(): hurray threads! 6805 INFO [3] mto: thread2(): hurray threads! 7405 INFO [1] mto: thread1(): hurray threads! 7609 INFO [2] mto: thread2(): hurray threads! 7609 INFO [3] mto: thread2(): hurray threads! 8105 INFO [1] mto: thread1(): hurray threads! 8413 INFO [2] mto: thread2(): hurray threads! 8413 INFO [3] mto: thread2(): hurray threads! 8805 INFO [1] mto: thread1(): hurray threads! The first column is the number of milliseconds since program start, the second column is the logging level, the third column is the thread id, and the rest is the log message. */</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'>#include</font> <font color='#5555FF'><</font>dlib<font color='#5555FF'>/</font>logger.h<font color='#5555FF'>></font> <font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> std; <font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> dlib; logger <b><a name='dlog'></a>dlog</b><font face='Lucida Console'>(</font>"<font color='#CC0000'>mto</font>"<font face='Lucida Console'>)</font>; <font color='#0000FF'>class</font> <b><a name='my_object'></a>my_object</b> : <font color='#0000FF'>public</font> multithreaded_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'>// register which functions we want to run as threads. We want one thread running </font> <font color='#009900'>// thread1() and two threads to run thread2(). So we will have a total of 3 threads </font> <font color='#009900'>// running. </font> <font color='#BB00BB'>register_thread</font><font face='Lucida Console'>(</font><font color='#5555FF'>*</font><font color='#0000FF'>this</font>,<font color='#5555FF'>&</font>my_object::thread1<font face='Lucida Console'>)</font>; <font color='#BB00BB'>register_thread</font><font face='Lucida Console'>(</font><font color='#5555FF'>*</font><font color='#0000FF'>this</font>,<font color='#5555FF'>&</font>my_object::thread2<font face='Lucida Console'>)</font>; <font color='#BB00BB'>register_thread</font><font face='Lucida Console'>(</font><font color='#5555FF'>*</font><font color='#0000FF'>this</font>,<font color='#5555FF'>&</font>my_object::thread2<font face='Lucida Console'>)</font>; <font color='#009900'>// start all our registered threads going by calling the start() 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 threads to stop before letting this object destruct itself. </font> <font color='#009900'>// Also note, you are *required* to wait for the threads 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='thread1'></a>thread1</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <b>{</b> <font color='#009900'>// This is a 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> dlog <font color='#5555FF'><</font><font color='#5555FF'><</font> LINFO <font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>thread1(): hurray threads!</font>"; dlib::<font color='#BB00BB'>sleep</font><font face='Lucida Console'>(</font><font color='#979000'>700</font><font face='Lucida Console'>)</font>; <b>}</b> <b>}</b> <font color='#0000FF'><u>void</u></font> <b><a name='thread2'></a>thread2</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <b>{</b> <font color='#009900'>// This is a 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> dlog <font color='#5555FF'><</font><font color='#5555FF'><</font> LINFO <font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>thread2(): hurray threads!</font>"; dlib::<font color='#BB00BB'>sleep</font><font face='Lucida Console'>(</font><font color='#979000'>800</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'>// tell the logger to output all messages </font> dlog.<font color='#BB00BB'>set_level</font><font face='Lucida Console'>(</font>LALL<font face='Lucida Console'>)</font>; <font color='#009900'>// Create an instance of our multi-threaded object. </font> my_object t; dlib::<font color='#BB00BB'>sleep</font><font face='Lucida Console'>(</font><font color='#979000'>3000</font><font face='Lucida Console'>)</font>; <font color='#009900'>// Tell the multi-threaded object to pause its threads. This causes the </font> <font color='#009900'>// threads to block on their next calls to should_stop(). </font> t.<font color='#BB00BB'>pause</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; dlog <font color='#5555FF'><</font><font color='#5555FF'><</font> LINFO <font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>paused threads</font>"; dlib::<font color='#BB00BB'>sleep</font><font face='Lucida Console'>(</font><font color='#979000'>3000</font><font face='Lucida Console'>)</font>; dlog <font color='#5555FF'><</font><font color='#5555FF'><</font> LINFO <font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>starting threads back up from paused state</font>"; <font color='#009900'>// Tell the threads to unpause themselves. This causes should_stop() to unblock </font> <font color='#009900'>// and to let the threads 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'>3000</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'>// threads because we have set the destructor up to do so. </font><b>}</b> </pre></body></html>