Creating a thread of execution
On modern multi-core compilers, to achieve maximal performance (or just to provide a good user experience), programs usually use multiple threads of execution. Here is a motivating example in which we need to create and fill a big file in a thread that draws the user interface:
#include <cstddef> // for std::size_t bool is_first_run(); // Function that executes for a long time. void fill_file(char fill_char, std::size_t size, const char* filename); // Called in thread that draws a user interface: void example_without_threads() { if (is_first_run()) { // This will be executing for a long time during which // users interface freezes... fill_file(0, 8 * 1024 * 1024, "save_file.txt"); } }
Getting ready
This recipe requires knowledge of boost::bind
or std::bind
.
How to do it...
Starting a thread of execution was never so easy:
#include <boost/thread.hpp> // Called in thread that draws a user interface: void example_with_threads...