将g_thread_new与struct结合使用

时间:2013-09-15 22:36:35

标签: c++ linux multithreading gdk

我想在自己的线程中启动struct方法:

g_thread_new( "NewThread", mymethod , NULL)

问题是,如果我将方法设置为“静态”,程序只会编译:

static gpointer mymethod(gpointer nrp) { puts(this->mystring) ; ... }

但是如果我将方法设置为“static”,我就无法访问结构实例变量,例如this-> mystring。

有没有办法将g_thread_new与类方法和访问实例变量一起使用?

1 个答案:

答案 0 :(得分:0)

您可以而且应该做的是在静态函数中接收void*作为线程中的参数。那个void*是你希望它被调用的函数的对象。只需将其强制转换为对象的类型并调用方法。

另外,请考虑使用boost::thread