c++之信号量(Semaphore)

信号量(简介)

定义于头文件 <semaphore>
信号量 (semaphore) 是一种轻量的同步原件,用于制约对共享资源的并发访问( 控制线程的并发数量) 。在可以使用两者时,信号量能比条件变量更有效率。
① counting_semaphore 实现非负资源计数的信号量
② binary_semaphore 仅拥有二个状态的信号量

成员函数

构造和赋值函数

counting_semaphore constexpr explicit counting_semaphore( std::ptrdiff_t desired );
counting_semaphore( const counting_semaphore& ) = delete;
  1. 构造一个 std::counting_semaphore 类型对象,初始化其计数器的值为 desired 。
  2. 复制构造函数被删除。
    operator= counting_semaphore 不可赋值操作
    release :void release( std::ptrdiff_t update = 1 );

    原子地将内部计数器的值增加 update 。唤醒等待的线程。

acquire (获得,得到函数)若内部计数器大于 0 则尝试将它减少 1 线程继续 ;否则阻塞线程直至内部计数器大于 0,并能成功减少 1 ,线程唤醒继续执行

①try_acquire 尝试减少内部计数器而不阻塞

②try_acquire_for 尝试减少内部计数器,至多阻塞一段时长

③try_acquire_until 尝试减少内部计数器,阻塞直至一个时间点常量

④max 返回内部计数器的最大可能值

#include<semaphore>
#include<iostream>
#include<chrono>
#include<thread>
using namespace std;
std::counting_semaphore sm(0);           //初始化信号量为0
void threadproc()
{
	sm.acquire();     //获取资源 ,相当于P操作
	cout << "thread signal" << endl;
	std::this_thread::sleep_for(std::chrono::microseconds(100));
	cout << "thread end" << endl;
	sm.release(1);    //释放资源,相当于V操作
}



int main()
{
	thread t1(threadproc);
	cout << "main begain send signal" << endl;
	sm.release(1);
	std::this_thread::sleep_for(std::chrono::milliseconds(200));
	sm.acquire();
	cout << "mian end" << endl;
	t1.join();
	return 0;
}

acquire 与 与 release 之间的关系:

在实现中不包含真正的许可对象,并且 Semaphore 也不会将许可与 也不会将许可与
线程关联起来,因此在一个线程中获得的许可可以在另一个线程中释放。可以将 线程关联起来,因此在一个线程中获得的许可可以在另一个线程中释放。可以将 acquire 操作视为是消费,操作视为是消费一个许可,而 release 操作是创建一个许可,Semaphore 并不受限于它在创建时的初始许可数量。也就是说并不受限于它在创建时的初始许可数量。也就是说 acquire 与 release 并没有强制的一对一关系,release 一次就相当于新增一个许可,许可的数量可能会 一次就相当于新增一个许可,许可的数量可能会由于没有与 acquire 操作一对一而导致超出初始化时设置的许可个数。

acquire相当于是P操作,执行信号量个数-1,如果信号量个数减为负数则为阻塞,因为没有资源可供使用,release相当于V操作,执行时释放资源,信号量个数+1.

示例: 使用信号量循环打印 ABC

 #include <iostream>
 #include <thread>
 #include <semaphore>
 using namespace std;

 counting_semaphore sema(1);
 counting_semaphore semb(0);
 counting_semaphore semc(0);


 void pthread_fun1() //线程函数 1 打印 a
 {
     int i = 0;
     for (; i < 10; ++i)
     {
     sema.acquire();
     cout << "A" << endl;
     std::this_thread::sleep_for(std::chrono::milliseconds(200));
     semb.release();
     }
 }
 void pthread_fun2() //线程函数 2 打印 l
 {
     int i = 0;
     for (; i < 10; ++i)
     {
          semb.acquire();
          cout << "B" << endl;
          std::this_thread::sleep_for(std::chrono::milliseconds(200));
          semc.release();
     } 
 }
 void pthread_fun3() //线程函数 3 打印 i
 {
     int i = 0;
     for (; i < 10; ++i)
     {
          semc.acquire();
          cout << "C" << endl;
          std::this_thread::sleep_for(std::chrono::milliseconds(200));
          sema.release();
     }
 }
 int main()
 {
     thread s1(pthread_fun1);
     thread s2(pthread_fun2);
     thread s3(pthread_fun3);
     s1.join();
     s2.join();
     s3.join();
     system("pause");
     return 0;
 }
  • 14
    点赞
  • 48
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值