(LeetCode) 1114. Sequential Print

Topic Source: https://leetcode-cn.com/problems/print-in-order/

We offer a class:

class Foo {public
  public void One () {Print ( "One");}
  public void TWO () {Print ( "TWO");}
  public void Three () {Print ( "Three");}
}
three different Foo will share a thread instance.

A thread will call one () method
thread B will call two () method
thread C will call three () method
please modify the program designed to ensure that two () method is executed after the method in one (), three () TWO method is performed after () method.

 

Example 1:

Enter: [1,2,3]
Output: "onetwothree"
explanation:
There are three threads are started asynchronously.
Input [1,2,3] One will call represents thread A () method, thread B will be called TWO () method will be called Three thread C () method.
The correct output is "onetwothree".
Example 2:

Input: [1,3,2]
Output: "onetwothree"
interpretation:
Input [1,3,2] One will call represents thread A () method will be called Three thread B () method will be called a thread C TWO () method.
The correct output is "onetwothree".
 

note:

Although digital inputs seems to suggest the order, but we do not guarantee thread in the operating system's scheduling order.

You see input format is mainly to ensure comprehensive testing.

Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/print-in-order
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.

 

Ideas:

  Provided two obstruction, three thread execution, a thread of execution before the completion of a release, after a thread to continue

 1 typedef struct {
 2     // User defined data may be declared here.
 3     //设置互斥量
 4     pthread_mutex_t mtx1;
 5     pthread_mutex_t mtx2;
 6 } Foo;
 7 
 8 Foo* fooCreate() {
 9     Foo* obj = (Foo*) malloc(sizeof(Foo));
10     
11     // Initialize user defined data here.
12     //初始化
13     pthread_mutex_init(&obj->mtx1,NULL);
14     pthread_mutex_init(&obj->mtx2,NULL);
15      // locked, 2 and 3 performed in advance to prevent 
16      the pthread_mutex_lock (& obj-> MTX1);
 . 17      the pthread_mutex_lock (& obj-> MTX2);
 18 is      return obj;
 . 19  }
 20 is  
21 is  void First (Foo * obj) {
 22 is      
23 is      // printFirst () the Outputs "First" or the Do not Change the Remove the this Line.. 
24-      printFirst ();
 25      // release the lock, so that the thread execution 2 
26      pthread_mutex_unlock (& obj-> MTX2);
 27  }
 28  
29  void SECOND, (Foo * obj) {
 30      
31      //.. printSecond () Outputs "SECOND" Remove the this or the Do Not Change Line
 32      // blocked, waiting for a thread to release the lock 
33 is      the pthread_mutex_lock (& obj-> MTX2);
 34 is      printSecond ();
 35      pthread_mutex_unlock (& obj-> MTX2);
 36      // release the lock, so that a thread of execution 3 
37 [      pthread_mutex_unlock (& obj-> MTX1);
 38 is  }
 39  
40  void THIRD (Foo * obj) {
 41 is      
42 is      // printThird () Outputs "THIRD" Remove the this or the Do Not Change Line..
 43      // blocked, waiting for the release of the thread 2 
44      pthread_mutex_lock (& obj-> MTX1);
45     printThird();
46     pthread_mutex_unlock(&obj->mtx1);
47 }
48 
49 void fooFree(Foo* obj) {
50     // User defined data may be cleaned up here.
51     pthread_mutex_destroy(&obj->mtx1);
52     pthread_mutex_destroy(&obj->mtx2);
53     //释放obj!!
54     free(obj); 
55 }

 

Guess you like

Origin www.cnblogs.com/qiu00/p/11568395.html