System V信号量和Posix信号量区别
Linux信号量(1)-SYSTEM V - 知乎 (zhihu.com)
System V 实现进程间通信:
(16条消息) 进程间通信方式——信号量(Semaphore)_sky_Mata的博客-优快云博客
POSIX有名信号量实现线程间同步
#include <iostream>
#include <thread>
#include<semaphore.h>
#include <unistd.h>
using namespace std;
sem_t sem;
void WaitSem()
{
while(1)
{
std::cout << this_thread::get_id() << " I am wait_sem" << std::endl;
sem_wait(&sem);
std::cout << this_thread::get_id() << " I get get_sem" << std::endl;
}
}
void PostSem()
{
while(1)
{
while(1)
{
sleep(1);
std::cout << this_thread::get_id() << " I am runing sem_post begin" << std::endl;
sem_post(&sem);
std::cout << this_thread::get_id() << " I am runing sem_post end" << std::endl;
}
}
}
int main()
{
// 线程间共享
sem_init(&sem, 0, 0);
thread t1(WaitSem);
thread t2(PostSem);
t1.join();
t2.join();
return 0;
}