Boost.UI
User Interface Boost library
thread.cpp

Boost.Thread and std::thread usage example

See also
boost::ui::call_async
Thread (Wikipedia)
// Copyright (c) 2017 Kolya Kosenko
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
// Fill text box using multiple worker threads.
#include <boost/ui.hpp>
#include <boost/bind.hpp>
#include <boost/lexical_cast.hpp>
#include <sstream>
#include "to_string.hpp"
#if !defined(BOOST_NO_CXX11_HDR_THREAD)
#include <thread>
namespace thread_ns = std;
#else
#include <boost/thread.hpp>
namespace thread_ns = boost;
#endif
namespace ui = boost::ui;
class thread_dialog : public ui::dialog
{
typedef thread_dialog this_type;
public:
thread_dialog();
private:
void on_start();
void thread_entry(int thread_number, int repeats);
void write(std::wstring str);
ui::text_box m_output_widget;
ui::string_box m_threads_count_widget;
ui::string_box m_repeats_widget;
};
thread_dialog::thread_dialog() : ui::dialog(
#if !defined(BOOST_NO_CXX11_HDR_THREAD)
"C++11 "
#else
"Boost."
#endif
"Thread + Boost.UI Example")
{
ui::hbox(*this)
<< m_output_widget.create(*this)
.tooltip("Output")
.layout().justify().stretch()
<< ( ui::vbox().layout().justify()
<< ui::label(*this, "Threads count:")
.layout().margin(1, 1, 0, 1)
<< m_threads_count_widget.create(*this, to_string(
100 * thread_ns::thread::hardware_concurrency() ))
.layout().justify()
<< ui::label(*this, "Repeats:")
.layout().margin(1, 1, 0, 1)
<< m_repeats_widget.create(*this, "10")
.layout().justify()
<< ui::button(*this, "&Start threads")
.on_press(&this_type::on_start, this)
.layout().justify()
)
;
resize(600, 400);
}
void thread_dialog::on_start()
{
const int threads_count = boost::lexical_cast<int>(m_threads_count_widget.text());
const int repeats = boost::lexical_cast<int>(m_repeats_widget.text());
m_output_widget.clear();
for ( int i = 0; i < threads_count; i++ )
{
// Single thread
//thread_entry(i, repeats);
thread_ns::thread t(&this_type::thread_entry, this, i, repeats);
t.detach();
}
}
void thread_dialog::thread_entry(int thread_number, int repeats)
{
for ( int r = 0; r < repeats; r++ )
{
std::wostringstream ss;
ss << thread_number << L"-" << r << " ";
// Not thread safe
//write(ss.str());
// Thread safe
ui::call_async( boost::bind(&this_type::write, this, ss.str()) );
}
}
void thread_dialog::write(std::wstring str)
{
m_output_widget.text(m_output_widget.text() + str);
}
int ui_main()
{
thread_dialog().show_modal();
return 0;
}
int main(int argc, char* argv[])
{
return ui::entry(&ui_main, argc, argv);
}
boost::ui
Boost.UI library namespace.
Definition: window.hpp:20
boost
Boost C++ libraries namespace.
Definition: window.hpp:19
boost::ui::entry
int entry(int(*ui_main)(int, char *[]), int argc, char *argv[])
UI application entry.
boost::ui::call_async
void call_async(F &&f, Args &&... args)
Calls f in the idle time in UI thread. This function is thread safe.
Definition: thread.hpp:40
std
Standard C++ namespace.
Definition: string_io.hpp:117
ui.hpp
Master Boost.UI file.