由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 一个multithread的小问题
相关主题
Multithread求教一个今天被面到的多线程的问题
multi thread复习请教请教如何保证函数时thread safe的?
昨天onsite被问到的 multithreading 题目L 电面
上周的几道电面题拿到了个小offer,鼓励一下和我一样转行的同学
Thread 和 concurrency怎么准备?怎么才是 精简,准确呢? spinlock VS semaphore 的 区别??
问一道multithreading的题求问:有什么多线程的复习资料不?
面试跟c++multithreading有关的工作,都会被问哪些问题啊?请教大侠们hash table 多线程问题
pure storage 面试题如何用C语言(非C++ JAVA任何其它语言)实现一个统一使用的swap宏
相关话题的讨论汇总
话题: r0话题: amount话题: 00
进入JobHunting版参与讨论
1 (共1页)
w****f
发帖数: 684
1
PIE chapter 8 bank example:
public boolean deposit( double amount){
...
newBalance = userBalance + amount;
userBalance = newBalance;
...
有些困惑(maybe naive)
why not
userBalance +=amount;
对thread 安全有影响吗?
c********r
发帖数: 286
2
这个跟跟thread safe没有关系吧,我想只是作者没那么写而已,实际是可以的。
这个要叫Monitor来确保thread safe
w****f
发帖数: 684
3
感觉应该有些关系。multithread 时可能出现的问题会不同。
a****l
发帖数: 8211
4
this is wrong. the author is stupid. the book is heading to trash bin.

【在 w****f 的大作中提到】
: PIE chapter 8 bank example:
: public boolean deposit( double amount){
: ...
: newBalance = userBalance + amount;
: userBalance = newBalance;
: ...
: 有些困惑(maybe naive)
: why not
: userBalance +=amount;
: 对thread 安全有影响吗?

N****p
发帖数: 1691
5
一个int或者一个指针的assignment是原子的 而a+=b不一定
一般的设计里都有monitor全程保护 所以没必要这样
但是如果没有保护的话,比如built-in type 可以直接声明一个volatile不用mutex
这样做可以在前一句a+=b被打断的情况下 不影响 共享的userbalance
对于这个example,这样做没有什么意义,因为object是built-in
但是在+=比较复杂(比如string+),而swap比较简单(直接swap
pointer和built-in member)的情况下,可以先创建一个temporary,实现string+,然
后在critical section 里面把共享的object和这个temporary swap,这样的pattern可
以减少critical section的长度
也是新手 欢迎拍砖
w****f
发帖数: 684
6
"a+=b被打断的情况" 什么情况下会被打断?
不太明白why a+=b 不是原子的?


【在 N****p 的大作中提到】
: 一个int或者一个指针的assignment是原子的 而a+=b不一定
: 一般的设计里都有monitor全程保护 所以没必要这样
: 但是如果没有保护的话,比如built-in type 可以直接声明一个volatile不用mutex
: 这样做可以在前一句a+=b被打断的情况下 不影响 共享的userbalance
: 对于这个example,这样做没有什么意义,因为object是built-in
: 但是在+=比较复杂(比如string+),而swap比较简单(直接swap
: pointer和built-in member)的情况下,可以先创建一个temporary,实现string+,然
: 后在critical section 里面把共享的object和这个temporary swap,这样的pattern可
: 以减少critical section的长度
: 也是新手 欢迎拍砖

w****f
发帖数: 684
7
ohoh。。。
还是很有价值的。

【在 a****l 的大作中提到】
: this is wrong. the author is stupid. the book is heading to trash bin.
w**z
发帖数: 8232
8
a+=b需要multiple instruction完成,当然不是atomic, even long assignments is
not atomic.

【在 w****f 的大作中提到】
: ohoh。。。
: 还是很有价值的。

w****f
发帖数: 684
9
能说详细些吗? a+=b 怎样muliple instruction?

【在 w**z 的大作中提到】
: a+=b需要multiple instruction完成,当然不是atomic, even long assignments is
: not atomic.

N****p
发帖数: 1691
10
读/写一个 native machine word that is properly aligned 是原子操作,所有的
architecture上都可以一步完成
x86有汇编可以一句话搞定+=,但是有些CPU architecture可能就不行,所以认为它不
是原子的
这个和C语言或者任何语言无关
看这里
http://stackoverflow.com/questions/1790204/in-c-is-i-1-atomic
在object比较大的时候 这个pattern可以缩短critical section
和operator=里面的copy and swap有相似
相关主题
问一道multithreading的题求教一个今天被面到的多线程的问题
面试跟c++multithreading有关的工作,都会被问哪些问题啊?请教如何保证函数时thread safe的?
pure storage 面试题L 电面
进入JobHunting版参与讨论
n**********r
发帖数: 41
11

这PIE是什么书?全名是什么啊?

【在 w****f 的大作中提到】
: PIE chapter 8 bank example:
: public boolean deposit( double amount){
: ...
: newBalance = userBalance + amount;
: userBalance = newBalance;
: ...
: 有些困惑(maybe naive)
: why not
: userBalance +=amount;
: 对thread 安全有影响吗?

a****l
发帖数: 8211
12
it is wrong to assume instruction level details at c code level. This is how
smart newbie mess up the system.

【在 N****p 的大作中提到】
: 读/写一个 native machine word that is properly aligned 是原子操作,所有的
: architecture上都可以一步完成
: x86有汇编可以一句话搞定+=,但是有些CPU architecture可能就不行,所以认为它不
: 是原子的
: 这个和C语言或者任何语言无关
: 看这里
: http://stackoverflow.com/questions/1790204/in-c-is-i-1-atomic
: 在object比较大的时候 这个pattern可以缩短critical section
: 和operator=里面的copy and swap有相似

w****f
发帖数: 684
13
能详细说一下您的解释

how

【在 a****l 的大作中提到】
: it is wrong to assume instruction level details at c code level. This is how
: smart newbie mess up the system.

s******e
发帖数: 493
14
probably after the compiler optimization, the executable will be the same in
two cases.
l****c
发帖数: 838
15
some processors have to use register as one operand.
If you implement a = a + b on ARM, it is like:
ldr r0, =a (address of variable)
ldr r1, [r0]
ldr r0, =b
ldr r2, [r0]
add r1, r1, r2
ldr r0, =a
str r1, [r0]
I don't remember the details and the above code is not optimized,but you get
the idea.
If you want to know what a function looks like at instruction level
on x86, compile it, and use objdump to get the assembly code.
If you don't know objdump is, look at gcc toolchain and the man page.
The IDE does not help you.
This application:
int main()
{
int a, b, s;
a = 3;
b = 4;
b += a;

return 0;
}
Translates into several sections, the major part on my pc:
08048394
:
8048394: 55 push %ebp
8048395: 89 e5 mov %esp,%ebp
8048397: 83 ec 10 sub $0x10,%esp
804839a: c7 45 fc 03 00 00 00 movl $0x3,-0x4(%ebp)
80483a1: c7 45 f8 04 00 00 00 movl $0x4,-0x8(%ebp)
80483a8: 8b 45 fc mov -0x4(%ebp),%eax
80483ab: 01 45 f8 add %eax,-0x8(%ebp)
80483ae: b8 00 00 00 00 mov $0x0,%eax
80483b3: c9 leave
80483b4: c3 ret
80483b5: 90 nop
80483b6: 90 nop
I don't much about x86 assembly, but b +=a seems to need at least
2 instructions.

【在 w****f 的大作中提到】
: 能详细说一下您的解释
:
: how

l****c
发帖数: 838
16
userBalance +=amount is just a short cut of
userBalance = userBalance + amount;
not difference after compile

【在 w****f 的大作中提到】
: PIE chapter 8 bank example:
: public boolean deposit( double amount){
: ...
: newBalance = userBalance + amount;
: userBalance = newBalance;
: ...
: 有些困惑(maybe naive)
: why not
: userBalance +=amount;
: 对thread 安全有影响吗?

E*******0
发帖数: 465
17
先顶下,回头看下。
N****p
发帖数: 1691
18
Test & Set, Compare & Swap 等原子操作 是现代CPU必须支持的。否则没法实现Mutex
,也就别谈Multithread。
Boost实现Mutex:
lock() BOOST_INTERLOCKED_COMPARE_EXCHANGE
unlock() interlocked_bit_test_and_set
这里interlocked 就指atomic
你可以说不同的Architecture支持原子性操作的方式不同,但是都是支持的。

how

【在 a****l 的大作中提到】
: it is wrong to assume instruction level details at c code level. This is how
: smart newbie mess up the system.

x*****p
发帖数: 1707
19
I think that the author might be thinking of non-blocking algorithm for
thread-safe issue.
w****f
发帖数: 684
20
Programming Interviews Exposed

【在 n**********r 的大作中提到】
:
: 这PIE是什么书?全名是什么啊?

相关主题
拿到了个小offer,鼓励一下和我一样转行的同学请教大侠们hash table 多线程问题
怎么才是 精简,准确呢? spinlock VS semaphore 的 区别??如何用C语言(非C++ JAVA任何其它语言)实现一个统一使用的swap宏
求问:有什么多线程的复习资料不?paging和 segmentation有什么区别?
进入JobHunting版参与讨论
l****c
发帖数: 838
21
I think the linux kernel code helps understand the atomic implementation:
X86:
http://lxr.free-electrons.com/source/arch/x86/include/asm/atomi
ARM:
http://lxr.free-electrons.com/source/arch/arm/include/asm/atomi

Mutex

【在 N****p 的大作中提到】
: Test & Set, Compare & Swap 等原子操作 是现代CPU必须支持的。否则没法实现Mutex
: ,也就别谈Multithread。
: Boost实现Mutex:
: lock() BOOST_INTERLOCKED_COMPARE_EXCHANGE
: unlock() interlocked_bit_test_and_set
: 这里interlocked 就指atomic
: 你可以说不同的Architecture支持原子性操作的方式不同,但是都是支持的。
:
: how

t****t
发帖数: 6806
22
while what you said is true, the DEGREE of atomic operation is different
across platforms. for example, some CPU may support 64-bit atomic add while
other CPU may only support 32-bit atomic add. so what acetel said is correct
, it's not correct to assume asm level code from c level code.

Mutex

【在 N****p 的大作中提到】
: Test & Set, Compare & Swap 等原子操作 是现代CPU必须支持的。否则没法实现Mutex
: ,也就别谈Multithread。
: Boost实现Mutex:
: lock() BOOST_INTERLOCKED_COMPARE_EXCHANGE
: unlock() interlocked_bit_test_and_set
: 这里interlocked 就指atomic
: 你可以说不同的Architecture支持原子性操作的方式不同,但是都是支持的。
:
: how

N****p
发帖数: 1691
23
Thanks for the clarification.
The Linux kernel implementation above also gives a good explanation.

while
correct

【在 t****t 的大作中提到】
: while what you said is true, the DEGREE of atomic operation is different
: across platforms. for example, some CPU may support 64-bit atomic add while
: other CPU may only support 32-bit atomic add. so what acetel said is correct
: , it's not correct to assume asm level code from c level code.
:
: Mutex

1 (共1页)
进入JobHunting版参与讨论
相关主题
如何用C语言(非C++ JAVA任何其它语言)实现一个统一使用的swap宏Thread 和 concurrency怎么准备?
paging和 segmentation有什么区别?问一道multithreading的题
关于multithread programming大家看什么书面试跟c++multithreading有关的工作,都会被问哪些问题啊?
failed bloomberg phone interviewpure storage 面试题
Multithread求教一个今天被面到的多线程的问题
multi thread复习请教请教如何保证函数时thread safe的?
昨天onsite被问到的 multithreading 题目L 电面
上周的几道电面题拿到了个小offer,鼓励一下和我一样转行的同学
相关话题的讨论汇总
话题: r0话题: amount话题: 00