Redian新闻
>
C++: exception: out-of-order execution?
avatar
C++: exception: out-of-order execution?# Programming - 葵花宝典
e*a
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,
avatar
t*t
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;

avatar
t*c
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;

avatar
e*a
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?
avatar
c*e
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.
avatar
t*t
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;

avatar
e*a
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
avatar
t*t
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

avatar
c*x
9
I guess they want mutex to prevent deal lock, same old crap.
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。