Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ - Clutter 1.0 - calling function from thread causes segfault

I am struggling with calling a clutter function from an extra thread. I use boost::thread for threading and the clutter library 1.0.

To be specific, the thread contains a looped function that emits boost::signals2::signal with parameters of x and y coordinates every once in a while. That signal is connected to a function that hands those variables to clutter, i.e. x,y in

clutter_stage_get_actor_at_pos(CLUTTER_STAGE(actor), CLUTTER_PICK_ALL, x, y);

And that is where i get a segfault.

Apparently clutter has some thread-handling routines. I tried calling

g_thread_init(NULL);

clutter_threads_init();

before starting clutter_main(). I also tried enclosing the clutter function in

clutter_threads_enter();

clutter_stage_get_actor_at_pos(CLUTTER_STAGE(actor), CLUTTER_PICK_ALL, x, y);

clutter_threads_leave();

but that does also not do the trick..

Every hint is appreciated, thank you in advance!

Addendum

I just forged a minimal sample of what I am trying to do. I already 'protected' the clutter_main() routine as suggested. Some functions of clutter seem to work (e.g setting stage color or setting actor position) from the seperate thread. Is there still something wrong with my code?

#include <clutter/clutter.h>
#include <boost/thread.hpp>


ClutterActor *stage;
ClutterActor* rect = NULL;


void receive_loop()
{
while(1)
{
    sleep(1);
    clutter_threads_enter();

    ClutterActor* clicked =  clutter_stage_get_actor_at_pos(CLUTTER_STAGE(stage), CLUTTER_PICK_ALL,300, 500);
    
    clutter_threads_leave();
}

}


int main(int argc, char *argv[]) 
{

    clutter_init(&argc, &argv);

g_thread_init(NULL);
clutter_threads_init();


    stage = clutter_stage_get_default();
    clutter_actor_set_size(stage, 800, 600);


rect = clutter_rectangle_new();
clutter_actor_set_size(rect, 256, 128);
clutter_actor_set_position(rect, 300, 500);
clutter_group_add (CLUTTER_GROUP (stage), rect);    


    clutter_actor_show(stage);


boost::thread thread = boost::thread(&receive_loop);


clutter_threads_enter();
    clutter_main();
clutter_threads_leave();

    return 0;
}
like image 865
verb-sap Avatar asked Sep 30 '10 14:09

verb-sap


1 Answers

Well, I think I found the answer..

Clutter Docs Gerneral

It says in section "threading model":

The only safe and portable way to use the Clutter API in a multi-threaded environment is to never access the API from a thread that did not call clutter_init() and clutter_main().

The common pattern for using threads with Clutter is to use worker threads to perform blocking operations and then install idle or timeour sources with the result when the thread finished.

Clutter provides thread-aware variants of g_idle_add() and g_timeout_add() that acquire the Clutter lock before invoking the provided callback: clutter_threads_add_idle() and clutter_threads_add_timeout().

So my correction to the minimal sample code would be to alter the receive_loop() to

void receive_loop()
{
while(1)
{
    sleep(1);

    int pos[2];
    pos[0] = 400;
    pos[1] = 200;
    
    clutter_threads_add_idle_full (G_PRIORITY_HIGH_IDLE,
                             get_actor,
                             &pos,
                             NULL);
}
}

and to add the get_actor function (as in the example code on the menitioned doc page)

static gboolean
get_actor (gpointer data)
{
    int* pos = (int*) data;
    ClutterActor* clicked = clutter_stage_get_actor_at_pos(CLUTTER_STAGE(stage), CLUTTER_PICK_ALL, pos[0], pos[1]);

    return FALSE;
}

clutter_threads_add_idle_full takes care of thread lock etc..

like image 115
verb-sap Avatar answered Sep 21 '22 13:09

verb-sap