gpt4 book ai didi

C++ - Clutter 1.0 - 从线程调用函数导致段错误

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:32:53 24 4
gpt4 key购买 nike

我正在努力从一个额外的线程调用一个困惑的函数。我使用 boost::thread 进行线程处理和 clutter 库 1.0。

具体来说,该线程包含一个循环函数,该函数每隔一段时间发出带有 x 和 y 坐标参数的 boost::signals2::signal。该信号连接到一个函数,该函数将这些变量交给困惑,即 x,y in

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

这就是我遇到段错误的地方。

显然 clutter 有一些线程处理例程。我试着打电话

g_thread_init(NULL);

clutter_threads_init();

在开始 clutter_main() 之前。我还尝试将 clutter 函数包含在

clutter_threads_enter();

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

clutter_threads_leave();

但这也没有用..

感谢您的每一个提示,提前致谢!

附录

我只是伪造了一个我正在尝试做的事情的最小样本。我已经按照建议“保护”了 clutter_main() 例程。杂乱的某些功能似乎在单独的线程中起作用(例如设置舞台颜色或设置 Actor 位置)。我的代码还有问题吗?

#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;
}

最佳答案

嗯,我想我找到了答案..

Clutter Docs Gerneral

它在“线程模型”部分说:

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().

所以我对最小示例代码的更正是将 receive_loop() 更改为

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);
}
}

并添加 get_actor 函数(如在提到的文档页面上的示例代码中)

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 负责线程锁等。

关于C++ - Clutter 1.0 - 从线程调用函数导致段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3831767/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com