Monday, February 17, 2020

1114. Print in Order

Q:
Suppose we have a class:
public class Foo {
  public void first() { print("first"); }
  public void second() { print("second"); }
  public void third() { print("third"); }
}
The same instance of Foo will be passed to three different threads. Thread A will call first(), thread B will call second(), and thread C will call third(). Design a mechanism and modify the program to ensure that second() is executed after first(), and third() is executed after second().

Example 1:
Input: [1,2,3]
Output: "firstsecondthird"
Explanation: There are three threads being fired asynchronously. The input [1,2,3] means thread A calls first(), thread B calls second(), and thread C calls third(). "firstsecondthird" is the correct output.
Example 2:
Input: [1,3,2]
Output: "firstsecondthird"
Explanation: The input [1,3,2] means thread A calls first(), thread B calls third(), and thread C calls second(). "firstsecondthird" is the correct output.

Note:
We do not know how the threads will be scheduled in the operating system, even though the numbers in the input seems to imply the ordering. The input format you see is mainly to ensure our tests' comprehensiveness.

A:

************use simple while loop *********

class Foo {
public:
    bool isFirst, isSecond;
    Foo() {
        isFirst = false;
        isSecond = false;
        
    }

    void first(function<void()> printFirst) {
        
        // printFirst() outputs "first". Do not change or remove this line.
        printFirst();
        isFirst = true;
    }

    void second(function<void()> printSecond) {
        while(!isFirst)
        {
            
        }
        // printSecond() outputs "second". Do not change or remove this line.
        printSecond();
        isSecond = true;
    }

    void third(function<void()> printThird) {
        while(!isSecond)
        {
            
        }
        // printThird() outputs "third". Do not change or remove this line.
        printThird();
    }
};

***********   use two  mutex ************

class Foo {
public:
    
    Foo() {
        m1.lock();
        m2.lock();        
    }

    void first(function<void()> printFirst) {
        
        // printFirst() outputs "first". Do not change or remove this line.
        printFirst();
        m1.unlock();
    }

    void second(function<void()> printSecond) {
        m1.lock();
        // printSecond() outputs "second". Do not change or remove this line.
        printSecond();
        m2.unlock();
    }

    void third(function<void()> printThird) {        
        m2.lock();
        // printThird() outputs "third". Do not change or remove this line.
        printThird();
        
    }
private:
    mutex m1;
    mutex m2;
};

1 comment:

  1. You should see how my partner Wesley Virgin's story starts in this shocking and controversial VIDEO.

    You see, Wesley was in the military-and shortly after leaving-he found hidden, "SELF MIND CONTROL" secrets that the government and others used to get everything they want.

    As it turns out, these are the exact same tactics lots of celebrities (notably those who "come out of nowhere") and the greatest business people used to become rich and successful.

    You've heard that you utilize only 10% of your brain.

    That's really because most of your BRAINPOWER is UNCONSCIOUS.

    Perhaps that conversation has even taken place INSIDE OF YOUR own head... as it did in my good friend Wesley Virgin's head seven years back, while riding an unlicensed, garbage bucket of a car with a suspended license and with $3.20 in his pocket.

    "I'm very frustrated with living paycheck to paycheck! When will I become successful?"

    You've taken part in those types of questions, right?

    Your very own success story is waiting to happen. You just have to take a leap of faith in YOURSELF.

    CLICK HERE TO LEARN WESLEY'S SECRETS

    ReplyDelete