目录
参考:?C++并发编程之线程同步 sem信号量_c++ sem_t-CSDN博客
信号量是一种变量,其访问是原子操作的。这就使得应用程序可以对其采用两种操作。
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)
/*************************************************************************
> 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**********************************/
信号量本身比较简单,使用起来也非常的方便。就是一个发送信号量,一个等待信号量,比较简单。