e***a 发帖数: 18 | 1 There are situations that exception could be throw within constructor and we
usually employ auto_ptr to protect resources. But if we do not have auto_
ptr can we use the following technique to handle it?
Please search "QUESTION" and see my question.
Thanks!
#include
using namespace std;
class E1 {
public: E1() {
int i = 1;
int j = 2;
int k = 3;
printf("i = %d, j = %d, |
t****t 发帖数: 6806 | 2 out-of-order execution does NOT change the result of your program. otherwise
it's called bug.
we
【在 e***a 的大作中提到】 : There are situations that exception could be throw within constructor and we : usually employ auto_ptr to protect resources. But if we do not have auto_ : ptr can we use the following technique to handle it? : Please search "QUESTION" and see my question. : Thanks! : #include : using namespace std; : class E1 { : public: E1() { : int i = 1;
|
t******c 发帖数: 348 | 3 since you throw an exception in the constructor of E1,
i don't think an E1 object is successfully constructed,
that means E1 object still doesn't exist.
there is no reason to call ~E1() to destory something not born yet.
so there is no *beep* resources for you to protect and release!
we
【在 e***a 的大作中提到】 : There are situations that exception could be throw within constructor and we : usually employ auto_ptr to protect resources. But if we do not have auto_ : ptr can we use the following technique to handle it? : Please search "QUESTION" and see my question. : Thanks! : #include : using namespace std; : class E1 { : public: E1() { : int i = 1;
|
e***a 发帖数: 18 | 4 thanks for coming to help.
little bit confused about this out-of-order execution stuff.
first:
int a = 1; // 1
int b = 2; // 2
...
it is possible that b = 2 executed before a = 1;
second:
class A;
class B;
...
A a;
B b;
is it possible that B b executed before A a?
last:
class A {
____public: A() { throw 1; }
...
};
A a; // exception is thrown within constructor of A
B b;
is it possible that B b executed and b constructed before the exception is
thrown? |
c****e 发帖数: 1453 | 5 I think the answer is NO. When they are in a same file, the order is
guaranteed. Only those static objects across different files can have the
order problems, where their initialization order is not guaranteed. |
t****t 发帖数: 6806 | 6 i repeat, the so called out-of-order execution, if any, does NOT change the
result of your program. otherwise, it's called a bug.
【在 e***a 的大作中提到】 : thanks for coming to help. : little bit confused about this out-of-order execution stuff. : first: : int a = 1; // 1 : int b = 2; // 2 : ... : it is possible that b = 2 executed before a = 1; : second: : class A; : class B;
|
e***a 发帖数: 18 | 7 I understand your statement but I want to get to the bottom of it.
I was asked about this during an interview.
They asked me about why mutex perterson implementation will fail in modern
processor.
(check out http://en.wikipedia.org/wiki/Peterson%27s_algorithm).
"Many modern CPUs reorder instruction execution and memory accesses to
improve execution efficiency. Such processors invariably give some way to
force ordering in a stream of memory accesses, typically through a memory
barrier instruction |
t****t 发帖数: 6806 | 8 ok, it's different in multi-thread environment. however, i don't see your
original program involved multi-thread?
so called out-of-order execution will do the retire stage according the
correct order. the partial result will be thrown if it's not needed.
on
【在 e***a 的大作中提到】 : I understand your statement but I want to get to the bottom of it. : I was asked about this during an interview. : They asked me about why mutex perterson implementation will fail in modern : processor. : (check out http://en.wikipedia.org/wiki/Peterson%27s_algorithm). : "Many modern CPUs reorder instruction execution and memory accesses to : improve execution efficiency. Such processors invariably give some way to : force ordering in a stream of memory accesses, typically through a memory : barrier instruction
|
|
c********x 发帖数: 84 | 9 I guess they want mutex to prevent deal lock, same old crap. |