Pulling threads of thread pool
Prev | Chapter 3. Multi-/Super-threaded tree | Next

## Pulling threads of thread pool

AgsThreadPool serves you instantiated and running threads. To pull an AgsReturnableThread issue ags_thread_pool_pull(). The following example does instantiate a thread pool and starts it. After, it pulls two threads and the callbacks are invoked.

Example 3.3. Pulling threads of thread-pool

    #include <glib.h>
    #include <glib-object.h>

    #include <ags/libags.h>

    void setup_callback(AgsApplicationContext *application_context, gpointer data);
    void thread_run_callback(AgsThread *thread, gpointer data);

    gchar *thread_0_str = "thread 0";
    gchar *thread_1_str = "thread 1";

    void
    setup_callback(AgsApplicationContext *application_context, gpointer data)
    {
      AgsThread *main_loop;
      AgsThread *thread_0, *thread_1;
      AgsThreadPool *thread_pool;

      main_loop = ags_concurrency_provider_get_main_loop(AGS_CONCURRENCY_PROVIDER(application_context));

      thread_pool = ags_thread_pool_new(main_loop);
      ags_concurrency_provider_set_thread_pool(AGS_CONCURRENCY_PROVIDER(application_context),
    					   thread_pool);

      ags_thread_pool_start(thread_pool);

      /* pull thread 0 */
      thread_0 = ags_thread_pool_pull(thread_pool);

      g_rec_mutex_lock(AGS_RETURNABLE_THREAD_GET_RESET_MUTEX(thread_0));

      g_atomic_pointer_set(&(AGS_RETURNABLE_THREAD(thread_0)->safe_data),
                           thread_0_str);

      ags_returnable_thread_connect_safe_run(AGS_RETURNABLE_THREAD(thread_0),
                                             thread_run_callback);

      ags_returnable_thread_set_flags(thread_0,
    				  AGS_RETURNABLE_THREAD_IN_USE);

      g_rec_mutex_unlock(AGS_RETURNABLE_THREAD_GET_RESET_MUTEX(thread_0));

      /* pull thread 1 */
      thread_1 = ags_thread_pool_pull(thread_pool);

      g_rec_mutex_lock(AGS_RETURNABLE_THREAD_GET_RESET_MUTEX(thread_1));

      g_atomic_pointer_set(&(AGS_RETURNABLE_THREAD(thread_1)->safe_data),
                           thread_1_str);

      ags_returnable_thread_connect_safe_run(AGS_RETURNABLE_THREAD(thread_1),
                                             thread_run_callback);

      ags_returnable_thread_set_flags(thread_1,
    				  AGS_RETURNABLE_THREAD_IN_USE);

      g_rec_mutex_unlock(AGS_RETURNABLE_THREAD_GET_RESET_MUTEX(thread_1));
    }

    void
    thread_run_callback(AgsThread *thread, gpointer data)
    {
      g_message("%s", (gchar *) data);
    }

    int
    main(int argc, char **argv)
    {
      AgsApplicationContext *application_context;

      application_context = ags_thread_application_context_new();
      g_object_ref(application_context);

      g_signal_connect_after(application_context, "setup",
    			 G_CALLBACK(setup_callback), NULL);

      ags_application_context_prepare(application_context);
      ags_application_context_setup(application_context);

      /* main loop run */
      g_main_loop_run(g_main_loop_new(g_main_context_default(),
    				  TRUE));

      return(0);
    }

Prev | Up | Next
Threads in general | Home | Worker-threads to do tic-less parallelism