[LeetCode1114] Sequential printing (multi-threading, semaphore)

Article directory

1. The topic

insert image description here
insert image description here

2. Ideas

This is the first time to do this kind of multi-threading problem on leetcode. For this kind of "synchronization" problem, you need to understand where the thread needs to be blocked (corresponding to P in the PV operation), and then find the corresponding V part (release resources). And the topic here does not require to examine the access of critical resources (mutual exclusion relationship), only the synchronization relationship needs to be considered. First, printB needs to be blocked, because it needs to wait for A to complete the printing, so the Pa operation is required here (as shown in the figure below), and the V part is where the resources are released, that is, after A prints, Va needs to be printed, that is, wake up B to start printing. The relationship between B and C is the same.

aSynchronization semaphore: It can be understood as the printed resource of A to be acquired by B, and the initial value is 0.
bSynchronization semaphore: Similarly, the initial value is 0.
insert image description here
There is no semaphore implementation and encapsulation in the C++ standard library, and the C language can be used <sempahore.h>.

(1) sem_initFunction: The header file required to call the function: semaphore.h

  • Function prototype:int sem_init(sem_t *sem, int pshared, unsigned int value);
  • Parameter explanation:
    • sem : points to the semaphore object
    • pshared : Indicates the type of semaphore. When 1, it is used for processes; when 0, it is used for threads.
    • value : specifies the size of the semaphore value
  • Return value: 0 is returned on success, -1 on failure, and errno is set.
  • Role: Create a semaphore and assign an initial value to the semaphore value.

(2) sem_postFunction: The header file required to call the function: semaphore.h

  • Function prototype:int sem_post(sem_t *sem);
  • Parameter explanation:
    • sem : points to the semaphore object
    • Return value: 0 is returned on success, -1 on failure, and errno is set.
    • Function: Increase the semaphore by 1 in an atomic operation

(3) sem_wait: The header file required to call the function: semaphore.h

  • Function prototype: sem_wait(sem_t *sem);
  • Parameter explanation:
    • sem : points to the semaphore object
  • Return value: return 0 on success, -1 on failure, and set errno
  • Function: Wait for the semaphore in a blocking manner. When the value of the semaphore is greater than zero, execute the function and reduce the semaphore by one. When the semaphore is zero, the thread calling the function will block.

3. Code

#include <semaphore.h>
class Foo {
    
    
private:
    sem_t sem_1, sem_2;
public:
    Foo() {
    
    
        sem_init(&sem_1, 0, 0);
        sem_init(&sem_2, 0, 0);
    }

    void first(function<void()> printFirst) {
    
    
        
        // printFirst() outputs "first". Do not change or remove this line.
        printFirst();
        //以原子操作的方式为将信号量增加1
        sem_post(&sem_1);
    }

    void second(function<void()> printSecond) {
    
    
        sem_wait(&sem_1);
        // printSecond() outputs "second". Do not change or remove this line.
        printSecond();
        //以原子操作的方式为将信号量增加1
        sem_post(&sem_2);
    }

    void third(function<void()> printThird) {
    
    
        sem_wait(&sem_2);
        // printThird() outputs "third". Do not change or remove this line.
        printThird();
    }
};

Guess you like

Origin blog.csdn.net/qq_35812205/article/details/123585972