avatar
e*e
1
看到个题目:
implement a test-and-set: check if a register is 0 or not. If it is 0,
set it to 1, and return 0
请问应该从何下手?谢谢
avatar
c*g
2
int TestAndSet(int* register)
{
int v;
v = Swap(register, 1);
return v!=0;
}
Swap() should be atomic.

【在 e*******e 的大作中提到】
: 看到个题目:
: implement a test-and-set: check if a register is 0 or not. If it is 0,
: set it to 1, and return 0
: 请问应该从何下手?谢谢

avatar
k*f
3
要控制reg,只能上汇编了
有些reg你读不到的,也没法改的

【在 e*******e 的大作中提到】
: 看到个题目:
: implement a test-and-set: check if a register is 0 or not. If it is 0,
: set it to 1, and return 0
: 请问应该从何下手?谢谢

avatar
e*e
4
register是keyword,在这里怎么能做Input variable呢?

【在 c*****g 的大作中提到】
: int TestAndSet(int* register)
: {
: int v;
: v = Swap(register, 1);
: return v!=0;
: }
: Swap() should be atomic.

avatar
c*g
5
com'n, it's pseudo-code

【在 e*******e 的大作中提到】
: register是keyword,在这里怎么能做Input variable呢?
avatar
e*e
6
那用register跟普通int变量有什么区别呢?

int TestAndSet(int* register)
{
int v;
v = Swap(register, 1);
return v!=0;
}
Swap() should be atomic.

【在 c*****g 的大作中提到】
: int TestAndSet(int* register)
: {
: int v;
: v = Swap(register, 1);
: return v!=0;
: }
: Swap() should be atomic.

avatar
m*e
7
You need to learn assembly. This is highly arch-dependent. But usually there
is one instruction or prefix for exactly this purpose.

【在 e*******e 的大作中提到】
: 看到个题目:
: implement a test-and-set: check if a register is 0 or not. If it is 0,
: set it to 1, and return 0
: 请问应该从何下手?谢谢

avatar
m*t
8
test and set is basic for thread synchronization. but yes, the underlying
implementation is hardware dependent. register variable is just a way of
telling compiler try to put this variable into register, compiler does not
guaranttee to honor such request.
avatar
j*r
9
Pseudo code:
int register;
int testandset(){
int ret = register;
register = 1;
return ret;
}
int function(){
while( testandset() );
...
}
Actually, testandset is a hardware instruction which is used to implement
synchronous primitives. So it must be atomic.

【在 e*******e 的大作中提到】
: 看到个题目:
: implement a test-and-set: check if a register is 0 or not. If it is 0,
: set it to 1, and return 0
: 请问应该从何下手?谢谢

avatar
c*x
10

asm
{
AX XOR 1;
}
that is no brainer.

【在 e*******e 的大作中提到】
: 看到个题目:
: implement a test-and-set: check if a register is 0 or not. If it is 0,
: set it to 1, and return 0
: 请问应该从何下手?谢谢

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