Redian新闻
>
是不是以前被海关罚过款是不是就不能申请global entry了?
avatar
是不是以前被海关罚过款是不是就不能申请global entry了?# Money - 海外理财
b*n
1
两个process,一个输出苹果,一个输出橘子,要求两个process必须take turns。
要求不能有busy waiting。
avatar
i*8
2
如题
avatar
b*n
3
这就是busy waiting的答案吧。
avatar
w*i
4
鏄紝鎴戜滑灏辨氮璐归挶浜
avatar
L*1
5
被问过一道一样的题目,不过是foo和bar,转一个大神的解答:
Semaphore sem1=new Semaphore(1);
Semaphore sem2=new Semaphore(0); //only when permit number is bigger
than 0
void functionFoo() {
while(1){
sem1.acquire();
System.out.print("Foo");
sem2.release();
}
}
void functionBar() {
while(1) {
sem2.acquire();
System.out.print("Bar");
sem1.release();
}
}
sem2=0 确保一上来block
sem1=1 确保第一次不会block
require就是-1
release就是+1
avatar
p*2
6

这个就是ping pong吧。可以用akka做。

【在 b*****n 的大作中提到】
: 两个process,一个输出苹果,一个输出橘子,要求两个process必须take turns。
: 要求不能有busy waiting。

avatar
l*s
7
这个解法不错。不用busy waiting一般就是在condition variable上面await &
notification,忘了Semaphore也有signaling的作用。
如果可以用Go的话,用channel很容易实现。类似在java里面用blockingQueue也可以,
一个blockingQueue放apple,一个blockingQueue放orange。开始的时候apple的
blockingQueue里面有一个apple,orange的blockingQueue里面是空。然后拿一个apple
后放orange,拿一个orange放一个apple。

【在 L*****1 的大作中提到】
: 被问过一道一样的题目,不过是foo和bar,转一个大神的解答:
: Semaphore sem1=new Semaphore(1);
: Semaphore sem2=new Semaphore(0); //only when permit number is bigger
: than 0
: void functionFoo() {
: while(1){
: sem1.acquire();
: System.out.print("Foo");
: sem2.release();
: }

avatar
k*a
8
busy waiting是什么
avatar
g*e
9
sem_t sm
sm.capacity=1
sm.init=0
thread 1:
while 1
sm.up
"apple"
thread 2:
while 1
sm.down
"orange"
avatar
m*k
10
+1
actually same idea in essence as Logan91's

apple

【在 l********s 的大作中提到】
: 这个解法不错。不用busy waiting一般就是在condition variable上面await &
: notification,忘了Semaphore也有signaling的作用。
: 如果可以用Go的话,用channel很容易实现。类似在java里面用blockingQueue也可以,
: 一个blockingQueue放apple,一个blockingQueue放orange。开始的时候apple的
: blockingQueue里面有一个apple,orange的blockingQueue里面是空。然后拿一个apple
: 后放orange,拿一个orange放一个apple。

avatar
I*d
12
二爷用的技术好新。。膜拜

【在 p*****2 的大作中提到】
:
: 这个就是ping pong吧。可以用akka做。

avatar
g*g
13
It says 2 processes, not 2 threads. Just send a message back and force. No
need to sync.
avatar
p*2
14
进程的怎么写?
写个来看看吧

【在 g*****g 的大作中提到】
: It says 2 processes, not 2 threads. Just send a message back and force. No
: need to sync.

avatar
m*k
15
same idea , old school way,
replace the blockingQueue to real msgQ like rabbitMQ,
or buzz word like kafka
avatar
b*0
16
另一种解法,多进程的话把shared data放到share memory就可以
int flag = 0;
mutex mtx;
bool run = true;
void Foo() {
while (run) {
lock_guard lock(mtx);
if (flag == 0) {
cout << "Foo";
flag = 1;
}
}
}
void Bar() {
while (run) {
lock_guard lock(mtx);
if (flag == 1) {
cout << "Bar";
flag = 0;
}
}
}

【在 b*****n 的大作中提到】
: 两个process,一个输出苹果,一个输出橘子,要求两个process必须take turns。
: 要求不能有busy waiting。

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