title: Emit safely a signal from a thread and connect it to a widget
body: <p>I'm using Gtkmm and multithreading.</p>
  
  <p>I have a class "NetworkWorker" doig stuffs with the network in a secondary thread.
  In this class i want to make many signals which will be handled by my class "MainWindow".</p>
  
  <p>The methods which handle these signals, will edit append text in a TextView.</p>
  
  <p>I have the following code:</p>
  
  <p>NetworkWorker.h</p>
  
  <pre><code>#ifndef         NETWORKWORKER_H_
  # define        NETWORKWORKER_H_
  
  # include       &lt;sigc++/sigc++.h&gt;
  # include       &lt;glibmm/threads.h&gt;
  # include       &lt;string&gt;
  
  class NetworkWorker
  {
   public:
    NetworkWorker();
    ~NetworkWorker();
  
    void start();
    void stop();
  
    sigc::signal&lt;void, std::string&amp;&gt;&amp; signal_data_received();
  
   private:
    void run();
  
    sigc::signal&lt;void, std::string&amp;&gt; m_signal_data_received;
  
    Glib::Threads::Thread* m_thread;
    Glib::Threads::Mutex m_mutex;
    bool m_stop;
  };
  
  #endif
  </code></pre>
  
  <p>NetworkWorker.c</p>
  
  <pre><code>#include        &lt;cstdlib&gt;
  #include        &lt;glibmm/timer.h&gt;
  #include        &lt;glibmm/threads.h&gt;
  #include        &lt;iostream&gt;
  #include        &lt;sigc++/sigc++.h&gt;
  
  #include        "NetworkWorker.h"
  
  NetworkWorker::NetworkWorker() :
    m_thread(NULL), m_stop(false)
  {
  
  }
  
  NetworkWorker::~NetworkWorker()
  {
    stop();
  }
  
  void NetworkWorker::start()
  {
    if (!m_thread)
      m_thread = Glib::Threads::Thread::create(sigc::mem_fun(*this, &amp;NetworkWorker::run));
  }
  
  void NetworkWorker::stop()
  {
    {
      Glib::Threads::Mutex::Lock lock(m_mutex);
      m_stop = true;
    }
  
    if (m_thread)
      m_thread-&gt;join();
  }
  
  sigc::signal&lt;void, std::string&amp;&gt;&amp; NetworkWorker::signal_data_received()
  {
    return m_signal_data_received;
  }
  
  void NetworkWorker::run()
  {
    while (true)
      {
        {
          Glib::Threads::Mutex::Lock lock(m_mutex);
          if (m_stop)
            break;
        }
        Glib::usleep(5000);
        std::cout &lt;&lt; "Thread" &lt;&lt; std::endl;
        std::string* str = new std::string("MyData");
        m_signal_data_received.emit(*str);
      }
  }
  </code></pre>
  
  <p>MainWindow.h</p>
  
  <pre><code>#ifndef         MAIN_WINDOW_H_
  # define        MAIN_WINDOW_H_
  
  # include       &lt;gtkmm/textview.h&gt;
  # include       &lt;gtkmm/window.h&gt;
  # include       &lt;string&gt;
  
  class MainWindow : public Gtk::Window
  {
   public:
    MainWindow();
    ~MainWindow();
  
    void appendText(const std::string&amp; str);
  
   private:
    Gtk::TextView m_text_view;
  };
  
  #endif
  </code></pre>
  
  <p>MainWindow.c</p>
  
  <pre><code>#include        &lt;gtkmm/notebook.h&gt;
  #include        &lt;gtkmm/widget.h&gt;
  #include        &lt;iostream&gt;
  #include        &lt;string&gt;
  
  #include        "MainWindow.h"
  
  MainWindow::MainWindow()
  {
    set_title("My App");
    set_default_size(800, 600);
    add(m_text_view);
  }
  
  MainWindow::~MainWindow()
  {
  
  }
  
  void MainWindow::appendText(const std::string&amp; str)
  {
    std::string final_text = str + "\n";
    Glib::RefPtr&lt;Gtk::TextBuffer&gt; buffer = m_text_view.get_buffer();
    Gtk::TextBuffer::iterator it = buffer-&gt;end();
    buffer-&gt;insert(it, final_text);
    Glib::RefPtr&lt;Gtk::Adjustment&gt; adj = m_text_view.get_vadjustment();
    adj-&gt;set_value(adj-&gt;get_upper() - adj-&gt;get_page_size());
  }
  </code></pre>
  
  <p>and my main.cpp</p>
  
  <pre><code>#include        &lt;cstdlib&gt;
  #include        &lt;gtkmm/main.h&gt;
  #include        &lt;iostream&gt;
  #include        &lt;string&gt;
  
  #include        "MainWindow.h"
  #include        "NetworkWorker.h"
  
  void            recv(const std::string&amp; str)
  {
    std::cout &lt;&lt; str &lt;&lt; std::endl;
  }
  
  int             main(int argc, char **argv)
  {
    Gtk::Main     app(Gtk::Main(argc, argv));
    MainWindow    main_window;
    NetworkWorker network_worker;
  
    main_window.show_all();
  
    network_worker.signal_data_received().connect(sigc::ptr_fun(&amp;recv));
    network_worker.signal_data_received().connect(sigc::mem_fun(main_window, &amp;MainWindow::appendText));
    network_worker.start();
  
    Gtk::Main::run(main_window);
    return (EXIT_SUCCESS);
  }
  </code></pre>
  
  <p>These snippetes have been a re-adapted for this question, so maybe some change is incoherent.</p>
  
  <p>When I execute this code, I have the following output:</p>
  
  <pre><code>$&gt; ./client 
  Thread
  MyData
  Thread
  MyData
  [...]
  Thread
  MyData
  Thread
  MyData
  (client:5596): Gtk-CRITICAL **: gtk_text_layout_real_invalidate: assertion 'layout-&gt;wrap_loop_count == 0' failed
  Thread
  MyData
  Thread
  MyData
  [...]
  Thread
  MyData
  Thread
  MyData
  [1]    5596 segmentation fault (core dumped)  ./client
  </code></pre>
  
  <p>Can some one help me to resolve this issue ? :)</p>