是不是以前被海关罚过款是不是就不能申请global entry了?# Money - 海外理财b*n2017-01-19 08:011 楼两个process,一个输出苹果,一个输出橘子,要求两个process必须take turns。要求不能有busy waiting。
L*12017-01-19 08:015 楼被问过一道一样的题目,不过是foo和bar,转一个大神的解答:Semaphore sem1=new Semaphore(1);Semaphore sem2=new Semaphore(0); //only when permit number is biggerthan 0void 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 确保一上来blocksem1=1 确保第一次不会blockrequire就是-1release就是+1
p*22017-01-19 08:016 楼这个就是ping pong吧。可以用akka做。【在 b*****n 的大作中提到】: 两个process,一个输出苹果,一个输出橘子,要求两个process必须take turns。: 要求不能有busy waiting。
l*s2017-01-19 08:017 楼这个解法不错。不用busy waiting一般就是在condition variable上面await ¬ification,忘了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();: }
g*e2017-01-19 08:019 楼sem_t smsm.capacity=1sm.init=0thread 1:while 1sm.up"apple"thread 2:while 1sm.down"orange"
m*k2017-01-19 08:0110 楼+1actually same idea in essence as Logan91'sapple【在 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。
g*g2017-01-19 08:0113 楼It says 2 processes, not 2 threads. Just send a message back and force. Noneed to sync.
p*22017-01-19 08:0114 楼进程的怎么写?写个来看看吧【在 g*****g 的大作中提到】: It says 2 processes, not 2 threads. Just send a message back and force. No: need to sync.
m*k2017-01-19 08:0115 楼same idea , old school way,replace the blockingQueue to real msgQ like rabbitMQ,or buzz word like kafka
b*02017-01-19 08:0116 楼另一种解法,多进程的话把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。