在gstreamer中做线程同步
创始人
2024-03-02 23:39:31
0

在gstreamer中做线程同步,这里分别用到了C++ condition_variable和glib的GCond和GMutex做线程同步,C++代码用在外部代码,glib的用法用在pipeline的代码中。

C++ condition_variable的背景知识

使用条件变量condition_variable做线程同步,可用于阻止一个线程或同时阻止多个线程,直到另一个线程修改共享变量(condition),并通知condition_variable,才会继续执行。

当调用它的wait函数时,它使用一个mutex来锁定线程。使得该线程保持阻塞状态,直到被另一个线程调用同一个condition_variable对象上的notify函数才被唤醒。

condition_variable类型的对象必须使用unique_lock等待。

  • wait 阻塞当前线程,直到notify_all()或notify_one()被执行。
  • wait_for 阻塞当前线程,直到条件变量被唤醒或在指定的超时持续时间之后。
  • wait_until 阻塞当前线程,直到唤醒条件变量或到达指定的时间点。

condition_variable的代码实例

本例中,ctrl+c的时候退出程序,所以下面signal_andler回调时,执行notify_all()

注册signal_andler:

static std::mutex srv_mtx;
static std::condition_variable srv_cv;struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = signal_andler;
sigfillset(&sa.sa_mask);
sigaction(SIGINT, &sa, NULL);

notify_all:

void signal_andler(int sig) {std::unique_lock lock(srv_mtx);server_exit = 1;srv_cv.notify_all();
}

wait的代码:

  std::unique_lock lock(srv_mtx);while (!server_exit) {printf("waiting ...\n");srv_cv.wait(lock);printf("done.\n");}

gstreamer mainloop的同步

在gstreamer pipeline中,mainloop在执行了g_main_loop_quit()之后,mainloop就会退出,所以需要对mainloop线程进行同步,释放thread资源。

定义GCond和GMutex:

  GCond main_loop_cond;GMutex main_loop_mutex;g_mutex_init(&main_loop_mutex);g_cond_init(&main_loop_cond);

如下,在g_main_loop_run()之后,lock之后修改main_loop_,broadcast之后然后释放锁:

  g_main_loop_run(player->main_loop_);g_mutex_lock(&player->main_loop_mutex);g_main_context_unref(player->context_);g_main_loop_unref(player->main_loop_);player->context_ = nullptr;player->main_loop_ = nullptr;g_cond_broadcast(&player->main_loop_cond);g_mutex_unlock(&player->main_loop_mutex);

在主线程,等待main_loop_的状态变化:

  gint64 end_time = g_get_monotonic_time () + 10 * G_TIME_SPAN_MILLISECOND;g_mutex_lock(&main_loop_mutex);while (main_loop_) {g_cond_wait_until(&main_loop_cond, &main_loop_mutex, end_time);}g_mutex_unlock(&main_loop_mutex);g_thread_unref(thread_);thread_ = nullptr;

或者是g_cond_wait,不带超时:

  g_mutex_lock(&main_loop_mutex);while (main_loop_) {g_cond_wait(&main_loop_cond, &main_loop_mutex);}g_mutex_unlock(&main_loop_mutex);g_thread_unref(thread_);thread_ = nullptr;

参考

std::condition_variable::wait

相关内容

热门资讯

监控摄像头接入GB28181平... 流程简介将监控摄像头的视频在网站和APP中直播,要解决的几个问题是:1&...
Windows10添加群晖磁盘... 在使用群晖NAS时,我们需要通过本地映射的方式把NAS映射成本地的一块磁盘使用。 通过...
protocol buffer... 目录 目录 什么是protocol buffer 1.protobuf 1.1安装  1.2使用...
在Word、WPS中插入AxM... 引言 我最近需要写一些文章,在排版时发现AxMath插入的公式竟然会导致行间距异常&#...
Fluent中创建监测点 1 概述某些仿真问题,需要创建监测点,用于获取空间定点的数据࿰...
educoder数据结构与算法...                                                   ...
MySQL下载和安装(Wind... 前言:刚换了一台电脑,里面所有东西都需要重新配置,习惯了所...
MFC文件操作  MFC提供了一个文件操作的基类CFile,这个类提供了一个没有缓存的二进制格式的磁盘...
有效的括号 一、题目 给定一个只包括 '(',')','{','}'...
【Ctfer训练计划】——(三... 作者名:Demo不是emo  主页面链接:主页传送门 创作初心ÿ...