C++使用Bind创建线程的问题
我有一个关于线程和绑定交互的小问题。
很简单,我的情况
class myclass {
// ...
// ...
void dosome();
void doanother(myclass2, myclass3*);
void run() {
this->_myt = boost::shared_ptr<boost::thread>(
new boost::thread(boost::bind(&myclass::dosome, this)));
}
// ...
boost::shared_ptr<boost::thread> _myt;
//...
};
还好,到目前为止一切都还好。据我所知,bind 能够将函数或指向函数的指针绑定到某些值,或者更好,绑定到某些参数值。 当我调用 myclass 对象时,函数 run() 会启动一个新线程。我有一个问题,为什么bind在运行时接受参数this,而函数dosome不使用任何参数?是因为对于类函数来说,总是有一个隐式参数,即指向类的指针???
好吧,这不是我唯一的问题。 在 dosome 中,这是我的第一个线程的执行流程,我这样做:
void myclass::dosome() {
myclass2 m;
myclass3* x = new myclass3;
boost::shared_ptr<boost::thread>(new boost::thread(
boost::bind(&myclass::doanother, m, x, this))); // Line X
}
嗯,我想执行另一个线程。 第一个问题:shared_prt是一个智能指针,这意味着如果dosome()从它的exec流中退出,它从该范围执行的线程将持续存在,成为一个shared_ptr...请告诉我这是正确的。 第二个问题:编译器对 X 行的指令非常生气...... 问题是绑定,它可能不喜欢我在那里传递的内容......为什么?这是一个成员函数(我谈论 doanother)并且有两个参数加上 this... 问题出在哪里?
谢谢
I have a little problem concerning thread and bind interaction.
It is simple, i have this situation
class myclass {
// ...
// ...
void dosome();
void doanother(myclass2, myclass3*);
void run() {
this->_myt = boost::shared_ptr<boost::thread>(
new boost::thread(boost::bind(&myclass::dosome, this)));
}
// ...
boost::shared_ptr<boost::thread> _myt;
//...
};
OK, well till now everything is okay. I understand that bind is able to bind a function or a pointer to a function to some values, or better, some argument values.
When I call on a myclass object the function run() a new thread is started. I have a question, why does bind, in run, takes parameter this, when function dosome does not use any parameter? is it because there is always, for class functions, an implicit argument that is the pointer to the class???
OK, this is not my only problem.
Inside dosome, which is the execution flow of my first thread I do this:
void myclass::dosome() {
myclass2 m;
myclass3* x = new myclass3;
boost::shared_ptr<boost::thread>(new boost::thread(
boost::bind(&myclass::doanother, m, x, this))); // Line X
}
Well I want to exec another thread.
First question: shared_prt is a smart pointer, this means that if dosome() gets out from its exec flow, the thread it executed from that scope will persist, being a shared_ptr... please tell me this is correct.
Second question: compiler gets really mad for the instruction at line X...
The problem is the bind, it probably does not like what I passed there... why? this is a member function (I talk about doanother) and there are two arguments plus this...
Where is the problem?
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以将其视为隐式参数。或者您可以将其视为
bind
需要调用您的成员函数,例如something->your_member()
,因此您必须告诉它什么something
is.在第二个示例中,您不会将
shared_ptr
存储在任何地方,因此它将在创建后立即被销毁。它指向的线程对象也会被销毁。然而,线程的执行本身并不绑定到线程对象,因此这将以完全相同的方式工作:这会启动一个新线程并释放它,就像在您的代码中一样。线程继续运行。
如上所示,
this
应作为绑定的第二个参数传递。You can think of it as an implicit parameter. Or you can think of this like
bind
needs to call your member function likesomething->your_member()
, so you must tell it whatsomething
is.You don't store the
shared_ptr
anywhere, in the second example, so it will be destroyed immediately after it's created. The thread object it points to will be destroyed too. However thread's execution itself is not bound to the thread object, so this will work in exactly the same way:This starts a new thread and looses it, just like in your code. The thread continues to run.
As you see above,
this
should be passed as the second parameter to bind.对于 boost::bind 'this' 是第一个参数(或者更确切地说 myclass::doanother 的第一个参数是 'this'):
所有类方法都有第一个参数“this”,我们不必键入它,编译器会自动添加它。类的每个实例都使用相同的代码,但有自己的成员变量。 “this”参数是指向该方法要使用的成员变量的指针。
另外,您真的想在另一个线程中绑定到此内容吗?这意味着多个线程将可以访问同一实例的成员变量,这是可行的,但可能不是我这样做的方式。
For the boost::bind 'this' is the first parameter (or rather the first parameter of myclass::doanother is 'this'):
All class methods have a first parameter of 'this', we never have to type it, the compiler adds it automatically. Each instance of a class uses the same code but has it's own member variables. The 'this' parameter is a pointer to the member variables to be used by the method.
Also do you really want to be binding to this in another thread. It means multiple threads will have access to the member variables of the same instance which is doable but probably not the way I'd do it.
尝试分部分回答..
不。没有其他对该对象的引用,因此当shared_ptr超出范围时,该线程对象将被删除,并且可能会发生各种错误......
其次,
boost::bind
采用指向成员函数的指针,然后是对象,然后是参数......Try to answer in parts..
NO. There are no other references to this object, so when the shared_ptr goes out of scope, the thread object will be deleted, and all sorts of crap could happen...
Second,
boost::bind
takes the pointer to member function and then the object, followed by the parameters...