1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
/*
* Copyright 2008 Klaus Triendl
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free
* Software Foundation, 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <iostream>
#include <sigc++/adaptors/lambda/base.h> // sigc::var
#include <sigc++/signal.h>
#include <glibmm/main.h>
#include <glibmm/thread.h>
#include <sigx/glib_auto_dispatchable.h>
#include <sigx/glib_threadable.h>
#include <sigx/signal_f.h>
#include <sigx/connection_wrapper.h>
using namespace std;
// fwd decl
class MainThread;
class MyThread: public sigx::glib_threadable
{
public:
MyThread();
protected:
// virtuals from sigx::threadable
virtual void on_startup();
private:
struct ThreadData;
Glib::Private<ThreadData> m_ThreadData;
// signal api
public:
// expose the 'nothing' signal
sigx::signal_f<sigc::signal<void> > signal_nothing;
};
struct MyThread::ThreadData
{
sigc::signal<void> m_sigNothing;
};
class MainThread: public sigx::glib_auto_dispatchable
{
public:
MainThread();
void run();
protected:
void on_self_ready();
void on_end_mainthread();
protected:
Glib::RefPtr<Glib::MainLoop> m_mainloop;
MyThread m_mythread;
sigx::connection_wrapper m_connMyThreadNothing;
};
using namespace std;
MyThread::MyThread():
sigx::glib_threadable(),
// initialize the signal api
signal_nothing(*this, m_ThreadData, &ThreadData::m_sigNothing)
{}
//virtual
void MyThread::on_startup()
{
m_ThreadData.set(new ThreadData);
}
MainThread::MainThread():
sigx::glib_auto_dispatchable(),
m_mainloop(),
m_mythread(),
m_connMyThreadNothing()
{}
void MainThread::run()
{
m_mainloop = Glib::MainLoop::create();
// one-shot idle handler
Glib::signal_idle().connect(
sigc::bind_return(
sigc::mem_fun(this, &MainThread::on_self_ready),
false
)
);
m_mainloop->run();
}
void MainThread::on_end_mainthread()
{
// disconnecting after restarting mythread shouldn't work (connection invalid) but yield no errors
m_connMyThreadNothing.disconnect();
// destroying after restarting mythread shouldn't (connection invalid) work but yield no errors
m_connMyThreadNothing = sigx::connection_wrapper();
m_mythread.finish();
m_mainloop->quit();
}
void do_nothing_dummy()
{}
void MainThread::on_self_ready()
{
// start mythread
m_mythread.run();
// connect to the thread's 'nothing' signal
m_connMyThreadNothing =
m_mythread.signal_nothing().connect(
sigx::open_tunnel_with(&do_nothing_dummy, *this)
);
// wait and test the connection status
m_connMyThreadNothing.connected();
// restart the thread
m_mythread.finish();
m_mythread.run();
// one-shot idle handler to
Glib::signal_idle().connect(
sigc::bind_return(
sigc::mem_fun(this, &MainThread::on_end_mainthread),
false
)
);
}
int main()
{
Glib::thread_init();
MainThread app;
app.run();
cout << "press <enter>.";
cin.get();
return 0;
}
|