主要功能类似生产者-消费者模型:
- 主线程使用某种资源
- 子线程在主线程使用资源后打印出资源的使用情况
- 要求在主线程使用资源后子线程立刻打印使用情况,如此执行
#include "semaphore.h"
#include <thread>
#include <mutex>
#include <iostream>
using namespace std;
Semaphore sem(0);
Semaphore sem1(1);
void funA()
{
while (true) {
sem.wait();
cout << "funA" << endl;
sem1.signal();
}
}
void funB()
{
sem1.wait();
cout << "funB" << endl;
sem.signal();
}
int main()
{
thread th(funA);
funB();
funB();
funB();
funB();
funB();
th.join();
}