1、sem_t介绍
信号量是一种变量,其访问是原子操作的。这就使得应用程序可以对其采用两种操作。
1、等待信号量,当信号量是0的时候,程序阻塞等待。当信号量 > 0 的时候,程序就可以继续运行了。
2、发送信号量,其作用是让信号量的值+1。这就实现了线程的同步控制。
sem_t信号量的定义如下:
typedef union
{
char __size[__SIZEOF_SEM_T];
long int __align;
} sem_t;
比较重要的函数如下:
/* Initialize semaphore object SEM to VALUE. If PSHARED then share it
with other processes. */
extern int sem_init (sem_t *__sem, int __pshared, unsigned int __value)
/* Free resources associated with semaphore object SEM. */
extern int sem_destroy (sem_t *__sem) __THROW;
/* Open a named semaphore NAME with open flags OFLAG. */
extern sem_t *sem_open (const char *__name, int __oflag, ...) __THROW;
/* Close descriptor for named semaphore SEM. */
extern int sem_close (sem_t *__sem) __THROW;
/* Remove named semaphore NAME. */
extern int sem_unlink (const char *__name) __THROW;
/* Wait for SEM being posted.
This function is a cancellation point and therefore not marked with */
extern int sem_wait (sem_t *__sem);
#ifdef __USE_XOPEN2K
/* Similar to `sem_wait' but wait only until ABSTIME.
This function is a cancellation point and therefore not marked with
__THROW. */
extern int sem_timedwait (sem_t *__restrict __sem,
const struct timespec *__restrict __abstime);
#endif
/* Test whether SEM is posted. */
extern int sem_trywait (sem_t *__sem) __THROWNL;
/* Post SEM. */
extern int sem_post (sem_t *__sem) __THROWNL;
/* Get current value of SEM and store it in *SVAL. */
extern int sem_getvalue (sem_t *__restrict __sem, int *__restrict __sval)
sem_init : 初始化信号量
sem_destroy : 释放信号量
sem_post : 增加信号量的值。如果说有线程阻塞在这个信号量上,调用这个函数会使其中的一个线程不再阻塞,具体哪一个线程不再阻塞是由线程的调度策略决定的。
sem_timedwait : sem_wait递减(加锁)由sem指向的信号量。如果该信号量的值是大于0的,那么递减操作是可以完成,并且立即返回。如果当前值为0,那么对sem_timedwait的调用将一直阻塞,直到可以进行递减操作(例如:该信号量的值增加至大于0),或者是一个信号处理打断该操作。
2、sem_t具体使用示例
/*************************************************************************
> File Name: thread_sem.cpp
> Author: 小和尚敲木鱼
> Mail:
> Created Time: Tue 21 Sep 2021 12:33:59 AM PDT
************************************************************************/
#include <iostream>
#include <thread>
#include <unistd.h>
#include <semaphore.h>
#include <sys/time.h>
using namespace std;
/*****************************文件说明***********************************
**********************************************************************/
sem_t g_sem;
int64_t getTimeStampMsec()
{
struct timeval tv;
gettimeofday(&tv,NULL);
return (tv.tv_sec * 1000 + tv.tv_usec / 1000);
}
void function_handle(void *param)
{
std::cout << __func__ << ": get wait..." << std::endl;
sem_wait(&g_sem);
int * running = (int * )param;
std::cout << "function_handle running = " << *running << std::endl;
}
void function_post(int time)
{
std::cout << "function_post sem time = " << time << std::endl;
sem_post(&g_sem);
}
int main(int agc,char * agv[])
{
sem_init(&g_sem, 0, 0);
int time = 5;
std::thread thread1(function_handle,&time);
std::thread post_thread(function_post,time);
if (post_thread.joinable())
post_thread.join();
if (thread1.joinable())
thread1.join();
return 0;
}
//OUT
//function_post sem time = 5
//function_handle: get wait...
//function_handle running = 5
/**************************end of file**********************************/
3、总结
信号量本身比较简单,使用起来也非常的方便。就是一个发送信号量,一个等待信号量,非常的银杏。