[英]use g_thread_new with a struct
I want to start a struct method in an own thread: 我想在自己的线程中启动struct方法:
g_thread_new( "NewThread", mymethod , NULL)
The problem is, the program only compiles if I set the method to "static": 问题是,仅当我将方法设置为“ static”时,程序才编译:
static gpointer mymethod(gpointer nrp) { puts(this->mystring) ; ... }
But if I set the method to "static" I cannot access the struct instance variables like this->mystring. 但是,如果我将方法设置为“静态”,则无法访问结构实例变量,例如this-> mystring。
Is there a way to use g_thread_new with class methods AND access instance variables? 有没有一种方法可以将g_thread_new与类方法和访问实例变量一起使用?
What you can and should do is receive a void*
in your static function as a parameter from the thread. 您可以并且应该做的是在静态函数中从线程中接收void*
作为参数。 That void*
is the object that you want it's function called. void*
是您想要其函数调用的对象。 Just cast it to the object's type and call the method. 只需将其转换为对象的类型并调用方法即可。
Also, consider using boost::thread
. 另外,考虑使用boost::thread
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.