[glibmm] Network examples: Use std::thread instead of Glib::Threads::Thread.
- From: Murray Cumming <murrayc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [glibmm] Network examples: Use std::thread instead of Glib::Threads::Thread.
- Date: Thu, 26 Nov 2015 10:06:57 +0000 (UTC)
commit a3c735b797b1e0fe3956b2257ba3a1b3bd911a56
Author: Murray Cumming <murrayc murrayc com>
Date: Fri Nov 6 12:03:58 2015 +0100
Network examples: Use std::thread instead of Glib::Threads::Thread.
Bug #757674
examples/network/resolver.cc | 28 +++++++++++++++++++++++-----
examples/network/socket-client.cc | 15 ++++++++++++++-
examples/network/socket-server.cc | 16 +++++++++++++++-
3 files changed, 52 insertions(+), 7 deletions(-)
---
diff --git a/examples/network/resolver.cc b/examples/network/resolver.cc
index a3c431a..483f65e 100644
--- a/examples/network/resolver.cc
+++ b/examples/network/resolver.cc
@@ -23,6 +23,7 @@
# include <config.h>
#endif
#include <giomm.h>
+#include <thread>
#include <iostream>
#include <cerrno>
@@ -203,14 +204,22 @@ lookup_thread (const Glib::ustring& arg)
lookup_one_sync (arg);
}
-static void
+static std::vector<std::thread*>
start_threaded_lookups (char **argv, int argc)
{
+ std::vector<std::thread*> result;
for (auto i = 0; i < argc; i++)
{
- Glib::Threads::Thread::create (sigc::bind (sigc::ptr_fun (lookup_thread),
- argv[i]));
- }
+ const Glib::ustring arg = argv[i];
+ const auto thread = new std::thread(
+ [arg]
+ {
+ lookup_thread(arg);
+ });
+ result.push_back(thread);
+ }
+
+ return result;
}
static void
@@ -497,18 +506,27 @@ main (int argc, char **argv)
nlookups = argc - 1;
loop = Glib::MainLoop::create (true);
+ std::vector<std::thread*> threads;
if (use_connectable)
do_connectable (argv[1], synchronous);
else
{
if (synchronous)
- start_threaded_lookups (argv + 1, argc - 1);
+ threads = start_threaded_lookups (argv + 1, argc - 1);
else
start_async_lookups (argv + 1, argc - 1);
}
loop->run ();
+ //Join and delete each thread:
+ std::for_each(threads.begin(), threads.end(),
+ [] (std::thread* thread)
+ {
+ thread->join();
+ delete thread;
+ });
+
#ifdef G_OS_UNIX
watch_conn.disconnect ();
#endif
diff --git a/examples/network/socket-client.cc b/examples/network/socket-client.cc
index 44fd94c..9204a25 100644
--- a/examples/network/socket-client.cc
+++ b/examples/network/socket-client.cc
@@ -1,6 +1,7 @@
#include <cstring>
#include <giomm.h>
#include <glibmm.h>
+#include <thread>
#include <iostream>
namespace
@@ -159,10 +160,15 @@ main (int argc,
return 1;
}
+ std::thread* thread = nullptr;
if (cancel_timeout)
{
cancellable = Gio::Cancellable::create ();
- Glib::Threads::Thread::create (sigc::bind (sigc::ptr_fun (cancel_thread), cancellable));
+ thread = new std::thread(
+ [cancellable] ()
+ {
+ cancel_thread(cancellable);
+ });
}
loop = Glib::MainLoop::create ();
@@ -320,5 +326,12 @@ main (int argc,
return 1;
}
+ //TODO: This won't happen if we returned earlier.
+ if(thread)
+ {
+ thread->join();
+ delete thread;
+ }
+
return 0;
}
diff --git a/examples/network/socket-server.cc b/examples/network/socket-server.cc
index 897c5d1..dcca8c5 100644
--- a/examples/network/socket-server.cc
+++ b/examples/network/socket-server.cc
@@ -1,3 +1,4 @@
+#include <thread>
#include <iostream>
#include <giomm.h>
#include <glibmm.h>
@@ -147,10 +148,15 @@ main (int argc,
return 1;
}
+ std::thread* thread = nullptr;
if (cancel_timeout)
{
cancellable = Gio::Cancellable::create ();
- Glib::Threads::Thread::create (sigc::bind (sigc::ptr_fun (cancel_thread), cancellable));
+ thread = new std::thread(
+ [cancellable] ()
+ {
+ cancel_thread(cancellable);
+ });
}
loop = Glib::MainLoop::create ();
@@ -321,5 +327,13 @@ main (int argc,
return 1;
}
+
+ //TODO: This won't happen if we returned earlier.
+ if(thread)
+ {
+ thread->join();
+ delete thread;
+ }
+
return 0;
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]