由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - C++: exception: out-of-order execution?
相关主题
问一个constructor的问题 (转载)VS里debug不work求教!
pthread_create inside a constructor找工作被要求做ikmnet.com 的在线C++考试,请问怎么准备?
c++ exceptionC++ 什么时候用 "new" ?
有大侠讲讲RTTI和exception的问题么?C++ Q01: private inheritance.
question about Design Patterns一道编程题 - throw Exceptions
C++问题,confusing...一个c++ constructor的问题, thanks
C++ 用户定义exception的标准用法是什么?请教几个C++问题
请教一下,exception时,destructor一定会被调用么?请教个Bloomberg 的 C++ 题目
相关话题的讨论汇总
话题: e1话题: execution话题: exception话题: order话题: int
进入Programming版参与讨论
1 (共1页)
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.
1 (共1页)
进入Programming版参与讨论
相关主题
请教个Bloomberg 的 C++ 题目question about Design Patterns
c++ questionC++问题,confusing...
what is the difference?C++ 用户定义exception的标准用法是什么?
问个disable copy constructor的问题请教一下,exception时,destructor一定会被调用么?
问一个constructor的问题 (转载)VS里debug不work求教!
pthread_create inside a constructor找工作被要求做ikmnet.com 的在线C++考试,请问怎么准备?
c++ exceptionC++ 什么时候用 "new" ?
有大侠讲讲RTTI和exception的问题么?C++ Q01: private inheritance.
相关话题的讨论汇总
话题: e1话题: execution话题: exception话题: order话题: int