Posted by Lucas Hermann Negri on Sun 14 Jun 02:25
report abuse | download | new post
- #include <glib/gthread.h>
- /* we need to eat some cpu time! */
- gint fib(gint n)
- {
- if(n == 1 || n == 2) return 1;
- return fib(n - 1) + fib(n - 2);
- }
- /* our background task caller */
- gpointer bg_task(gpointer data)
- {
- gint res = fib( GPOINTER_TO_INT(data) );
- return GINT_TO_POINTER(res);
- }
- int main(int argc, char* argv[])
- {
- g_thread_init(NULL);
- GError *error1 = NULL, *error2 = NULL, *error3 = NULL, *error4 = NULL;
- g_print("initializing tasks...\n");
- GThread* t1 = g_thread_create(bg_task, GINT_TO_POINTER(40), TRUE, &error1);
- GThread* t2 = g_thread_create(bg_task, GINT_TO_POINTER(42), TRUE, &error2);
- GThread* t3 = g_thread_create(bg_task, GINT_TO_POINTER(5), TRUE, &error3);
- GThread* t4 = g_thread_create(bg_task, GINT_TO_POINTER(10), TRUE, &error4);
- if(error1 || error2 || error3 || error4)
- {
- g_print("Error!");
- return 1;
- }
- g_print("waiting for the tasks...\n");
- gint res1 = GPOINTER_TO_INT(g_thread_join(t1));
- gint res2 = GPOINTER_TO_INT(g_thread_join(t2));
- gint res3 = GPOINTER_TO_INT(g_thread_join(t3));
- gint res4 = GPOINTER_TO_INT(g_thread_join(t4));
- g_print("results: %d, %d, %d, %d\n", res1, res2, res3, res4);
- return 0;
- }
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.