一个物体可以知道它已经死了吗? [英] Can an object know it is dead?

查看:50
本文介绍了一个物体可以知道它已经死了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我怀疑线程问题导致一个对象在一个线程上下文中被破坏,而其中一个成员函数是

仍然在另一个线程上下文中运行。作为确认

的一种方法,我想让对象检查它是否在

引用的成员函数中存活,如果不存在则报告给我。但是,我不能想象一个符合标准的方式让对象知道

它已经被破坏了。


任何人都可以确认*不能这样做吗?如果不能,可以

任何人建议任何替代方案吗?

提前感谢所有回复。


Best ,

Dave

Hello all,

I suspect that a threading issue is leading to an object being
destructed in one thread context while one of its member functions is
still running in another thread context. As a means of confirming
this, I would like to have the object check if it is alive in the
referenced member function and report to me if it is not. However, I
cannot think of a standards-compliant way for an object to know that
it has been destructed.

Can anybody confirm that this *cannot* be done? If it cannot, can
anybody suggest any alternatives?

Thanks in advance for all responses.

Best,
Dave

推荐答案

"成为*********** @ yahoo.com" < be *********** @ yahoo.comwrote:
"be***********@yahoo.com" <be***********@yahoo.comwrote:

大家好,


我怀疑一个线程问题导致一个对象在一个线程上下文中被破坏,而其中一个成员函数仍然是在另一个线程上下文中运行的
。作为确认

的一种方法,我想让对象检查它是否在

引用的成员函数中存活,如果不存在则报告给我。但是,我不能想象一个符合标准的方式让对象知道

它已经被破坏了。


任何人都可以确认*不能这样做吗?如果不能,那么任何人都可以提出任何替代方案吗?
Hello all,

I suspect that a threading issue is leading to an object being
destructed in one thread context while one of its member functions is
still running in another thread context. As a means of confirming
this, I would like to have the object check if it is alive in the
referenced member function and report to me if it is not. However, I
cannot think of a standards-compliant way for an object to know that
it has been destructed.

Can anybody confirm that this *cannot* be done? If it cannot, can
anybody suggest any alternatives?



无法移植。但是你可能会尝试输入一个在析构函数中设置为true的dead

标志,然后在所有

成员函数断言中(dead == false)可能会工作。


符合标准的替代方法是设置锁定

机制。一个简单的地图< void *,size_t将工作。每当一个对象

需要另一个(或它自己)保持活着时,它会增加与该对象相关的

地图中的值。在析构函数中,对象可以检查

以确保其引用为零。这样的事情:


map< void *,size_tlock;


class Object {

~Object() {

断言(锁[this] == 0);

}


void func(){

++ lock [this];

//做好工作

--lock [this];

}

};


void user(Object * o){

++ lock [o];

//使用''o''

--lock [o];

}


当然这是明智的包装对象中的增量和减量

(RAII)

类Locker {

const void * obj;

Locker(const void * obj):obj(obj){

++ lock [obj];

}

~Locker(){

--lock [obj];

}

};

It cannot be done portably. However you might try putting in a "dead"
flag that is set to true in the destructor, then in all the
member-functions assert( dead == false ) might work.

An alternative that is standards conforming is to set-up a locking
mechanism. A simple map<void*, size_twill work. Whenever an object
needs another (or itself) to stay alive, it increments the value in the
map associated with that object. In the destructor, the object can check
to make sure that its reference is zero. Something like this:

map<void*, size_tlock;

class Object {
~Object() {
assert( lock[this] == 0 );
}

void func() {
++lock[this];
// do work
--lock[this];
}
};

void user( Object* o ) {
++lock[o];
// work with ''o''
--lock[o];
}

Of course it is wise to wrap the increment and decrement in an object
(RAII)

class Locker {
const void* obj;
Locker( const void* obj ): obj(obj) {
++lock[obj];
}
~Locker() {
--lock[obj];
}
};


2月23日上午10:54,better_cs _... @ yahoo.com

< better_cs _... @ yahoo.comwrote:
On Feb 23, 10:54 am, "better_cs_...@yahoo.com"
<better_cs_...@yahoo.comwrote:

大家好,


我怀疑在hreading问题导致一个对象在一个线程上下文中被破坏,而其中一个成员函数是

仍在另一个线程上下文中运行。作为确认

的一种方法,我想让对象检查它是否在

引用的成员函数中存活,如果不存在则报告给我。但是,我不能想象一个符合标准的方式让对象知道

它已经被破坏了。


任何人都可以确认*不能这样做吗?如果不能,那么任何人都可以提出任何替代方案吗?
Hello all,

I suspect that a threading issue is leading to an object being
destructed in one thread context while one of its member functions is
still running in another thread context. As a means of confirming
this, I would like to have the object check if it is alive in the
referenced member function and report to me if it is not. However, I
cannot think of a standards-compliant way for an object to know that
it has been destructed.

Can anybody confirm that this *cannot* be done? If it cannot, can
anybody suggest any alternatives?



忽略这方面的线程方面,最简单的方法是添加一个

私有bool成员和一个isValid()成员函数: br />

[未经测试的代码]


class yourClass {

private:

bool valid_ ;

受保护:

bool isValid(){return valid_; }

public:

yourClass():valid_(true){}

~yourClass(){valid_ = false; }

//其余的你的类

};


现在让每个成员函数在继续之前检查isValid()。对于

调试目的,您可能只需添加一个断言(isValid());到每个成员函数的开头是




线程使得当然更加复杂 - 你需要

某种互斥或其他设备,以确保在会员功能中对象不被破坏。


祝你好运,


Tom

Ignoring the threads aspect of this, the simplest way is to add a
private bool member and an isValid() member function:

[untested code]

class yourClass {
private:
bool valid_;
protected:
bool isValid() { return valid_; }
public:
yourClass() : valid_(true) {}
~yourClass() { valid_ = false; }
// Rest of yourClass
};

Now have each member function check isValid() before proceeding. For
debugging purposes, you might just add an assert(isValid()); to the
beginning of each member function.

Threads make this much more complicated of course - you would need
some sort of mutex or other device to ensure that the object wasn''t
destructed while in the member function.

Best regards,

Tom


是*********** @ yahoo.com 写道:

大家好,


我怀疑线程问题导致一个对象在一个线程上下文中被破坏,而其中一个成员函数是

仍然在另一个线程上下文中运行。作为确认

的一种方法,我想让对象检查它是否在

引用的成员函数中存活,如果不存在则报告给我。但是,我不能想象一个符合标准的方式让对象知道

它已经被破坏了。


任何人都可以确认*不能这样做吗?如果不能,那么任何人都可以提出任何替代方案吗?
Hello all,

I suspect that a threading issue is leading to an object being
destructed in one thread context while one of its member functions is
still running in another thread context. As a means of confirming
this, I would like to have the object check if it is alive in the
referenced member function and report to me if it is not. However, I
cannot think of a standards-compliant way for an object to know that
it has been destructed.

Can anybody confirm that this *cannot* be done? If it cannot, can
anybody suggest any alternatives?



无法完成。


智能指针。

Can''t be done.

Smart pointers.


这篇关于一个物体可以知道它已经死了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆