QT——QSemaphore(信号量)

它来处理一定数量的资源,生产者不断向缓冲写入数据直到缓冲末端 ,消费者从缓冲不断从缓冲头部 读取数据。

加入头文件:

#include

  • QSemaphore( int n = 0 )
  • ~QSemaphore()
  • void acquire ( int n = 1 )
  • int available () const
  • void release ( int n = 1 )
  • bool tryAcquire ( int n = 1 )
  • bool tryAcquire ( int n, int timeout )

 


 

例子:

QSemaphore sem(10);
 sem.acquire(3);//请求3个信号
 sem.acquire(8);//请求8个>7,没有这么多,会被阻塞
 sem.tryAcquire(8);//请求8个>7,没有这么多,但不会被阻塞
 int n=sem.available();//还剩7个
 sem.release(3);//释放3个
 n=sem.available();//还剩10个


你可能感兴趣的:(QT——QSemaphore(信号量))