O'Reilly logo

Boost C++ Application Development Cookbook by Antony Polukhin

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Creating an execution thread

On modern multi-core compilers, to achieve maximal performance (or just to provide a good user experience), programs usually must use multiple execution threads. 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 <algorithm> #include <fstream> #include <iterator> void set_not_first_run(); bool is_first_run(); // Function, that executes for a long time void fill_file_with_data(char fill_char, std::size_t size, const char* filename){ std::ofstream ofs(filename); std::fill_n(std::ostreambuf_iterator<char>(ofs), size, fill_char); set_not_first_run(); } // ... // Somewhere in thread that draws a user interface if (is_first_run()) { // This will ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required