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 8361749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:46:11+00:00 2026-06-09T11:46:11+00:00

I’m trying to add a timeout source specific to a particular GThread. In the

  • 0

I’m trying to add a timeout source specific to a particular GThread.

In the main thread, I can create a GMainContext (g_main_context_new) and add a timeout (g_timeout_add). However, when I try to do it in a thread created with g_thread_create it simply doesn’t work, the GSourceFunc is never called and I have no clue why.

For the moment I’ve only got this the documentation:

Callbacks require a bit of attention. Callbacks from GTK+ (signals)
are made within the GTK+ lock. However callbacks from GLib (timeouts,
IO callbacks, and idle functions) are made outside of the GTK+ lock.
So, within a signal handler you do not need to call
gdk_threads_enter(), but within the other types of callbacks, you do.

But my timeout function (for testing purposes) only prints in the console, so I don’t think it’s a problem of resource protection and mutexes.

The thread structure is:

Main thread –> no GLib main context explicitly created

  • capture thread

  • process thread –> should have a GLib main context and a timeout source

  • show thread

I appreciate any help.

Thanks in advance.

  • 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-09T11:46:12+00:00Added an answer on June 9, 2026 at 11:46 am

    Do you use g_timeout_add() or g_source_attach()?

    g_timeout_add() and g_timeout_add_full() does not allow you to specify which main context to add. It always use the default main context. if you are not using the default main context in your main thread, it is perfectly ok to use it in your process thread. You can read about it in the description.

    A GMainContext can only be running in a single thread

    The default main context is implicitly created by many functions, including g_main_context_default(). So please make sure that you have not using it in your main thread.

    You can use g_source_attach() to add a timeout source to your own main context if you decided to do so. There is no timeout function you can use to specify your main context. So, just do it by your self.

    The following code is basically same as: g_timeout_add_full(G_PRIORITY_DEFAULT, 100, func, l, notify);

    #include <glib.h>
    
    void notify(gpointer data)
    {
            g_main_loop_quit((GMainLoop *)data);
    }
    
    gboolean func(gpointer data)
    {
            static gint i = 0;
            g_message("%d", i++);
            return (i < 10) ? TRUE : FALSE;
    }
    
    gpointer thread(gpointer data)
    {
            GMainContext *c;
            GMainContext *d;
            GMainLoop *l;
            GSource *s;
    
            c = g_main_context_new();
            d = g_main_context_default();
    
            g_message("local: %p", c);
            g_message("default: %p", d);
    
    #if 1
            l = g_main_loop_new(c, FALSE);
            s = g_timeout_source_new(100);
            g_source_set_callback(s, func, l, notify);
            g_source_attach(s, c);
            g_source_unref(s);
    #else
            l = g_main_loop_new(d, FALSE);
            g_timeout_add_full(G_PRIORITY_DEFAULT, 100, func, l, notify);
    #endif
    
            g_main_loop_run(l);
            g_message("done");
    
            return NULL;
    }
    
    int main(int argc, char *argv[])
    {
            GError *error = NULL;
            GThread *t;
    
            g_thread_init(NULL);
            t = g_thread_create(thread, NULL, TRUE, &error);
            g_thread_join(t);
    
            return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Does anyone know how can I replace this 2 symbol below from the string

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.