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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:44:09+00:00 2026-06-17T06:44:09+00:00

I am using Glib to develop a multi-threading C software. I would like to

  • 0

I am using Glib to develop a multi-threading C software.

I would like to have a set of alive threads. Once some thread finishes, another thread starts with a different parameter. It is something like a thread pool.

I am using glib thread to implement the multi-threading. But I cannot find a lot of tutorials from Google. I can now start a set of threads, but have no idea about the waiting. Some code of mine:

GThread *threads[n_threads];
thread_aux_t *data = (thread_aux_t*) calloc(n_threads, sizeof(thread_aux_t));
for (i = 0; i < n_threads; ++i) {
    data[i].parameter = i;
    threads[i] = g_thread_create((GThreadFunc) pe_lib_thread, data + i,
            TRUE, NULL);
}

/* wait for threads to finish */
for (i = 0; i < n_threads; ++i) {
    g_thread_join(threads[i]); // How to start a new thread depending on the return value?
}
free(data);

Thanks.


Problem solved. Update:

Just found a thread pool implementation of glib: Thread Pools. I have run it and it works correctly.
The code is written as:

// 'query' is for this new thread, 
// 'data' is the global parameters set when initiating the pool
void *pe_lib_thread(gpointer query, gpointer data) {
}

void run_threads() {
    GThreadPool *thread_pool = NULL;
    // Global parameters by all threads.
    thread_aux_t *data = (thread_aux_t*) calloc(1, sizeof(thread_aux_t));

    data->shared_hash_table = get_hash_table();
    g_thread_init(NULL);
    thread_pool = g_thread_pool_new((GFunc) pe_lib_thread, data, n_threads,
            TRUE, NULL);
    // If n_threads is 10, there are maximum 10 threads running, others are waiting.
    for (i = 0; i < n_queries; i++) {
        query = &queries[i];
        g_thread_pool_push(thread_pool, (gpointer) query, NULL);
    }
    g_thread_pool_free(thread_pool, 0, 1);
}
  • 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-17T06:44:11+00:00Added an answer on June 17, 2026 at 6:44 am

    g_thread_join returns the return value, so you just check it.
    Let’s say you want to create a new process if the return value is 17.

    for (i = 0; i < n_threads; ++i) {
        if (threads[i] && g_thread_join(threads[i]) == 17) {  
            /* Start a new thread. */
            threads[i] = g_thread_create((GThreadFunc) pe_lib_thread, data + i,
                TRUE, NULL);
        } else {
            threads[i] = NULL;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Eclipse CDT to develop a C software. I would like to
Using Glib Testing framework, I would like to know if I can reuse a
I am using GLib's doubly linked list structure, GList. I would like to know
Since I'm using gtk3 and gtkmm3 on embedded I would like to have the
I am using glib for creating threads: g_thread_create() How can I acquire thread id
Compiling FluidSynth 1.1.1 from source. I have unresolved external symbol with glib, using Visual
When using GLib 1.2's GHashTable with the g_hash_table_foreach() method, is it safe to remove
I am using GLib in a Windows application. Where do I place the GLib
Currently I'm developing a multi-thread application. I use a TreeView to display the states
I am using glib, it has a lot of functions that return strings that

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.