Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7845671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:19:01+00:00 2026-06-02T17:19:01+00:00

I have a gtkmm application and I’m trying to put some long running tasks

  • 0

I have a gtkmm application and I’m trying to put some long running tasks into separate threads so they don’t lock the GUI. Here’s a tutorial I’ve based my design on:

http://www.velvetcache.org/2008/09/30/gtkmmglibmm-thread-example

I use Glib::Dispatcher signals to notify the GUI thread when the work is done or something needs to be updated, however I’m not sure how to pass the data between the worker thread and GUI thread. So far I’ve been passing a pointer to the class which creates the worker to the worker and then modifying public members of the class, but something tells me it’s not the most correct to do it. Here’s an example:

class Some_GUI_class
{
public:
    std::string thread_message;

private:
    Worker_class* worker;

    void start_worker()
    {
        if (worker != NULL) return;

        worker = new Worker_class(this);
        worker->sig_message.connect(sigc::mem_fun(*this, &Some_GUI_class::display_message_from_thread);
        worker.start();
    }        

    void display_message_from_thread()
    {
        some_label->set_text(thread_message);
    }
}

class Worker_class
{
public:
    Worker_class(Some_GUI_class* gui_class) : gui_class(gui_class)
    {}

    void start()
    {
        thread = Glib::Thread::create(sigc::mem_fun(*this, &Worker_class::run), true);
    }

    Glib::Dispather sig_message;

protected:
    Glib::Thread* thread;
    Glib::Mutex mutex;

    Some_GUI_class* gui_class;

    void run()
    {
        // ...
        gui_class->thread_message = "Message from a thread!";
        sig_message();                
    }

}

This essentialy works, but I guess if the GUI thread wanted to modify thread_message at the same time there would be a problem? Is it safe to do it like this then as long as I’m sure the variables are only modified by a single thread or is there a better way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T17:19:04+00:00Added an answer on June 2, 2026 at 5:19 pm

    You have a race condition. Even if your gui thread doesn’t modify thread_message, allowing the GUI thread to read it while another thread is modifying it is not going to give you long term happiness. This is because std::string is not itself protected from multiple threads accessing it, and has multiple internal fields. If one thread is in the process of modifying one of its internal fields, while another is reading them, the internal state will not be consistent from the point of view of the second.

    You can use a mutex in the GUI class to protect access to the variables which might be accessed by another thread. Lock and unlock the mutex in get/set routines, and use those routines for all other accesses to ensure that only one thread gets to access or modify the variables at one time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application which uses gtkmm. The client's machine does not have this
I'm working on a plugin for a smaller application using gtkmm. The plugin I'm
I have program using gtkmm, gtkglextmm and exiv2. I want to include these libraries
have been trying couple of hours now to make my iphone app universal. The
I have a simple (and also trivial) banking application that I wrote in C++.
Have a group of related projects running in SQL Server 2005 for which I
I started a GTKMM application about 6 months ago and it's reached the point
I want to use dtsearch in my desktop application written in C++ and Gtkmm.
Have some dates in my local Oracle 11g database that are in this format:
I am choosing a GUI toolkit for C++ to learn. I have done some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.