使用boost螺纹和非静态类函数 [英] Using boost thread and a non-static class function
问题描述
所以,我做了一些研究,并发现你可以创建一个线程对象,并经使用this和boost ::绑定等,这确实不是一个非静态类函数启动多大意义,我和所有的例子我能找到有相同的类,因为它是先从所以这可能是使用的函数内启动了线程对象。然而,我,我启动线程在不同的类,所以我恐怕用本,我会说的本是我从,而不是一个函数在创建线程类(我可能错了,我需要更多地了解这个本的家伙)。这里是我有问题我的源代码的例子。
So I have done some research, and have found you can create a boost::thread object and have it start with a non-static class function by using "this" and boost::bind etc. It really doesn't make much sense to me and all the examples I could find had the boost::thread object launched within the same class as the function it was starting with so this could be used. I however, am launching the thread in a different class so I'm afraid by using "this", I will be saying the "this" is from the class I am creating the thread from, rather than the one the function is in (I'm probably wrong, I need to learn more about this "this" guy). Here is an example of my source I am having the problem with.
ANNGUI.h
class ANNGUI
{
private:
boost::thread *GUIThread;
Main *GUIMain;
public:
// Creates the entire GUI and all sub-parts.
int CreateGUI();
}
ANNGUI.cpp
ANNGUI.cpp
int ANNGUI::CreateGUI()
{
GUIMain = new Main();
GUIThread = new boost::thread(GUIMain->MainThreadFunc);
};
这是不是所有的源代码,但我想我的问题是在这里的某个地方,我知道我有对付这种不知怎么的,但我不能确定如何。我可以用一个静态函数,但我真的不希望让我的变量静态两种。
谢谢你。
This isn't all the source, but I think my problem is in here somewhere, I know I have to deal with the "this" somehow, but I'm unsure how. I Could use a static function, but I didn't really want to make my variables static either. Thanks.
另外,有没有使用任何Boost库的非常好的资源?他们的网站文档似乎不错,但在我的头上。
Also, Is there any very good resource for using any boost libraries? Their web site documentation seems good, but over my head.
推荐答案
的这个
关键字用来与的boost ::绑定
当你创建函数对象绑定到的对象的成员函数的。成员函数不能从实例而存在,因此与创建一个仿函数对象时,一位成员函数的boost ::绑定
,你需要一个指向一个实例。这正是在这个
关键字实际上是。如果您使用 A类的成员函数中这个
关键字,你得到的是一个指向类的当前实例。
The this
keyword is used with boost::bind
when the function object you're creating is bound to a object member function. Member functions can't exist apart from instances, so when creating a functor object out of a member function with boost::bind
, you need a pointer to an instance. That's exactly what the this
keyword actually is. If you use the this
keyword within a member function of a class, what you get is a pointer to the current instance of that class.
如果你是从调用绑定
的之外的类成员函数,你可以这样说:
If you were to call bind
from outside a class member function, you might say something like:
int main()
{
Foo f;
boost::thread* thr = new boost::thread(boost::bind(&Foo::some_function, &f));
}
在这里,我们使用的Foo :: some_function作为我们的线程函数。但是,我们不能用这个
,因为我们调用绑定
从主
。但同样的事情可以用实现这个
如果我们叫做绑定
从foo的成员函数,像这样在:
Here, we're using Foo::some_function as our thread function. But we can't use this
because we're calling bind
from main
. But the same thing could be achieved using this
if we called bind
from within a member function of Foo, like so:
void Foo::func1()
{
boost::thread* thr = new boost::thread(boost::bind(&Foo::some_function, this));
}
如果一个成员函数是静态的,或者仅仅是一个常规(非会员)的功能,那么你并不需要一个实例的指针都没有。你会只是做:
If a member function is static, or is simply a regular (non-member) function, then you don't need an instance pointer at all. You would just do:
boost::thread* thr = new boost::thread(some_regular_function);
这篇关于使用boost螺纹和非静态类函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!