[gtkmm-documentation] multithread example: Use std::thread, etc, instead of Glib::Threads::*.
- From: Murray Cumming <murrayc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtkmm-documentation] multithread example: Use std::thread, etc, instead of Glib::Threads::*.
- Date: Thu, 26 Nov 2015 11:23:35 +0000 (UTC)
commit 47d1a805c97ee3d8b1ec047723f68fcd71be1a0f
Author: Murray Cumming <murrayc murrayc com>
Date: Mon Oct 19 10:15:13 2015 +0200
multithread example: Use std::thread, etc, instead of Glib::Threads::*.
Because C++11 now has this concurrency API and we should use standard
C++ where possible. We cannot actually deprecate Glib::Threads until
a future (soon) gtkmm when we can depend on C++14:
https://mail.gnome.org/archives/gtkmm-list/2015-August/msg00070.html
Bug #755091
examples/Makefile.am | 1 +
examples/book/multithread/examplewindow.cc | 14 +++++--
examples/book/multithread/examplewindow.h | 2 +-
examples/book/multithread/exampleworker.cc | 59 +++++++++++++++-------------
examples/book/multithread/exampleworker.h | 4 +-
5 files changed, 47 insertions(+), 33 deletions(-)
---
diff --git a/examples/Makefile.am b/examples/Makefile.am
index 433cd3a..2f839e3 100644
--- a/examples/Makefile.am
+++ b/examples/Makefile.am
@@ -524,6 +524,7 @@ book_multithread_example_SOURCES = \
book/multithread/exampleworker.cc\
book/multithread/exampleworker.h \
book/multithread/main.cc
+book_multithread_example_CXXFLAGS = -pthread
book_notebook_example_SOURCES = \
book/notebook/examplewindow.cc \
diff --git a/examples/book/multithread/examplewindow.cc b/examples/book/multithread/examplewindow.cc
index 7a68803..7d0f98a 100644
--- a/examples/book/multithread/examplewindow.cc
+++ b/examples/book/multithread/examplewindow.cc
@@ -87,8 +87,11 @@ void ExampleWindow::on_start_button_clicked()
else
{
// Start a new worker thread.
- m_WorkerThread = Glib::Threads::Thread::create(
- sigc::bind(sigc::mem_fun(m_Worker, &ExampleWorker::do_work), this));
+ m_WorkerThread = new std::thread(
+ [this]
+ {
+ m_Worker.do_work(this);
+ });
}
update_start_stop_buttons();
}
@@ -145,7 +148,8 @@ void ExampleWindow::on_quit_button_clicked()
{
// Order the worker thread to stop and wait for it to stop.
m_Worker.stop_work();
- m_WorkerThread->join();
+ if (m_WorkerThread->joinable())
+ m_WorkerThread->join();
}
hide();
}
@@ -163,7 +167,9 @@ void ExampleWindow::on_notification_from_worker_thread()
if (m_WorkerThread && m_Worker.has_stopped())
{
// Work is done.
- m_WorkerThread->join();
+ if (m_WorkerThread->joinable())
+ m_WorkerThread->join();
+ delete m_WorkerThread;
m_WorkerThread = nullptr;
update_start_stop_buttons();
}
diff --git a/examples/book/multithread/examplewindow.h b/examples/book/multithread/examplewindow.h
index 520be0a..7a02f0c 100644
--- a/examples/book/multithread/examplewindow.h
+++ b/examples/book/multithread/examplewindow.h
@@ -51,7 +51,7 @@ private:
Glib::Dispatcher m_Dispatcher;
ExampleWorker m_Worker;
- Glib::Threads::Thread* m_WorkerThread;
+ std::thread* m_WorkerThread;
};
#endif // GTKMM_EXAMPLEWINDOW_H
diff --git a/examples/book/multithread/exampleworker.cc b/examples/book/multithread/exampleworker.cc
index e5cf2f7..b63bdcd 100644
--- a/examples/book/multithread/exampleworker.cc
+++ b/examples/book/multithread/exampleworker.cc
@@ -16,6 +16,7 @@
#include "exampleworker.h"
#include "examplewindow.h"
#include <sstream>
+#include <chrono>
ExampleWorker::ExampleWorker() :
m_Mutex(),
@@ -31,7 +32,7 @@ ExampleWorker::ExampleWorker() :
// separate get_fraction_done() and get_message() methods.
void ExampleWorker::get_data(double* fraction_done, Glib::ustring* message) const
{
- Glib::Threads::Mutex::Lock lock(m_Mutex);
+ std::lock_guard<std::mutex> lock(m_Mutex);
if (fraction_done)
*fraction_done = m_fraction_done;
@@ -42,20 +43,20 @@ void ExampleWorker::get_data(double* fraction_done, Glib::ustring* message) cons
void ExampleWorker::stop_work()
{
- Glib::Threads::Mutex::Lock lock(m_Mutex);
+ std::lock_guard<std::mutex> lock(m_Mutex);
m_shall_stop = true;
}
bool ExampleWorker::has_stopped() const
{
- Glib::Threads::Mutex::Lock lock(m_Mutex);
+ std::lock_guard<std::mutex> lock(m_Mutex);
return m_has_stopped;
}
void ExampleWorker::do_work(ExampleWindow* caller)
{
{
- Glib::Threads::Mutex::Lock lock(m_Mutex);
+ std::lock_guard<std::mutex> lock(m_Mutex);
m_has_stopped = false;
m_fraction_done = 0.0;
m_message = "";
@@ -64,36 +65,40 @@ void ExampleWorker::do_work(ExampleWindow* caller)
// Simulate a long calculation.
for (int i = 0; ; ++i) // do until break
{
- Glib::usleep(250000); // microseconds
+ std::this_thread::sleep_for(std::chrono::milliseconds(250));
- Glib::Threads::Mutex::Lock lock(m_Mutex);
+ {
+ std::lock_guard<std::mutex> lock(m_Mutex);
- m_fraction_done += 0.01;
+ m_fraction_done += 0.01;
- if (i % 4 == 3)
- {
- std::ostringstream ostr;
- ostr << (m_fraction_done * 100.0) << "% done\n";
- m_message += ostr.str();
- }
+ if (i % 4 == 3)
+ {
+ std::ostringstream ostr;
+ ostr << (m_fraction_done * 100.0) << "% done\n";
+ m_message += ostr.str();
+ }
- if (m_fraction_done >= 1.0)
- {
- m_message += "Finished";
- break;
- }
- if (m_shall_stop)
- {
- m_message += "Stopped";
- break;
+ if (m_fraction_done >= 1.0)
+ {
+ m_message += "Finished";
+ break;
+ }
+ if (m_shall_stop)
+ {
+ m_message += "Stopped";
+ break;
+ }
}
- lock.release();
+
caller->notify();
}
- Glib::Threads::Mutex::Lock lock(m_Mutex);
- m_shall_stop = false;
- m_has_stopped = true;
- lock.release();
+ {
+ std::lock_guard<std::mutex> lock(m_Mutex);
+ m_shall_stop = false;
+ m_has_stopped = true;
+ }
+
caller->notify();
}
diff --git a/examples/book/multithread/exampleworker.h b/examples/book/multithread/exampleworker.h
index 27ac005..6db9229 100644
--- a/examples/book/multithread/exampleworker.h
+++ b/examples/book/multithread/exampleworker.h
@@ -17,6 +17,8 @@
#define GTKMM_EXAMPLEWORKER_H
#include <gtkmm.h>
+#include <thread>
+#include <mutex>
class ExampleWindow;
@@ -34,7 +36,7 @@ public:
private:
// Synchronizes access to member data.
- mutable Glib::Threads::Mutex m_Mutex;
+ mutable std::mutex m_Mutex;
// Data used by both GUI thread and worker thread.
bool m_shall_stop;
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]