[英]C++ pthread hand over object to thread
I think with my code, I hand over the address of the object to the thread.我想通过我的代码,我将对象的地址交给线程。 However I cannot access the object directly (args.getTerminationStatus).
但是我无法直接访问该对象(args.getTerminationStatus)。
Unfortunately I only make a copy of that passed object and therefore changes of attributes take no effect (the while loop ist running forever, even thought the attribute behind getTerminationStatus() has changed outside).不幸的是,我只复制了传递的对象,因此属性的更改无效(while 循环永远运行,甚至认为 getTerminationStatus() 背后的属性已在外部更改)。 Would be nice if you have some ideas.
如果你有一些想法就好了。 Thanks in advance.
提前致谢。
int main(){
Sensors barometer(1, "Barometer", 0); //ID, Description, Value
pthread_t t1;
int ret1;
ret1 = pthread_create(&t1, NULL, &barometerThread, &barometer); //Hand over object to thread
if(ret1 != 0){
cout << "Error: Thread 1 (Luftdruckmesser) konnte nicht erstellt werden." << endl;
return 1;
}
sleep(10);
barometer.finish(); // Sets TerminationStatus to true
void *barometerThread(void *args){
Sensors dev = *(Sensors *) (args); // I think this is just a copy of that passed object
cout << "Luftdruck-Thread gestartet. Device-ID: " << dev.getID() << " Descr.: " << dev.getDescription() << " Value: " << dev.getValue() << endl;
while (!dev.getTerminationStatus()){
cout << "Still in loop: " << dev.getTerminationStatus() << endl;
sleep(1);
}
pthread_exit(0);
}
You do not need to make a copy.您不需要制作副本。 As you are absolutely certain that the
void*
you pass to the function is pointing to a Sensor
you can cast the pointer:由于您绝对确定传递给函数的
void*
指向Sensor
您可以转换指针:
void *barometerThread(void *args){
Sensor* dev_ptr = (Sensor*)args;
// ....
Usually a proper C++ cast ( static_cast
/ dynamic_cast
) should be prefered, though as we are dealing with a void*
and anyhow it is all up to you to make sure that the cast is correct, a c-style cast is "ok".通常应该首选适当的 C++ 转换(
static_cast
/ dynamic_cast
),尽管我们正在处理void*
并且无论如何这完全取决于您以确保转换正确,c 样式转换是“可以的”。
Note that you are missing the return
in the function but it must return a void*
.请注意,您缺少函数中的
return
,但它必须返回一个void*
。 If it does not you invoke undefined behavior.如果不是,则调用未定义的行为。 Also there is
std::thread
which keeps you away from such void*
anachronisms.还有
std::thread
可以让您远离这种void*
时代错误。
You can avoid the copy by changing the conversion line to:您可以通过将转换行更改为:
Sensors& dev = *(Sensors *) (args);
so that now it will be a reference to a Sensors
object rather than a copy.所以现在它将是对
Sensors
对象的引用,而不是副本。
Assuming getID()
is a Sensors
class method you can then do:假设
getID()
是Sensors
类方法,您可以执行以下操作:
cout << dev.getID();
after you've converted it.在你转换它之后。
If you haven't considered it yet, using std::thread
would make life a bit easier here (since it accepts std::function
s which you can bind arbitrary typed data to).如果您还没有考虑过,使用
std::thread
会让这里的生活更轻松一些(因为它接受std::function
,您可以将任意类型的数据绑定到它)。 Another thing to keep in mind is that the Sensors
class instance will be accessed from two threads, so make sure barometer.finish()
and dev.getTerminationStatus()
are both thread safe!要记住的另一件事是
Sensors
类实例将从两个线程访问,因此请确保barometer.finish()
和dev.getTerminationStatus()
都是线程安全的!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.