简体   繁体   English

如何将多个线程同步到一个?

[英]How to synchronize multiple threads to one?

I have a multithreaded application where I want to allow all but one of the threads to run synchronously. 我有一个多线程应用程序,我想允许其中一个线程以外的所有线程同步运行。 However, when a specific thread wakes up I need the rest of the threads to block. 但是,当特定线程唤醒时,我需要其余线程进行阻塞。

My Current implementation is: 我当前的实现是:

void ManyBackgroundThreadsDoingWork()
{
    AquireMutex(mutex);

    DoTheBackgroundWork();

    ReleaseTheMutex(mutex);
}


void MainThread()
{
    AquireMutex(mutex);

    DoTheMainThreadWork();

    ReleaseTheMutex(mutex);
}

This works, in that it does indeed keep the background threads from operating inside the critical block while the main thread is doing its work. 之所以起作用,是因为它确实使后台线程在主线程执行其工作时不会在关键块内运行。 However, There is a lot of contention for the mutex amongst the background threads even when they don't necessarily need it. 但是,即使后台线程不一定需要互斥锁,也有很多争用。 The main thread runs intermittently and the background threads are able to run concurrently with each other, just not with the main thread. 主线程间歇地运行,而后台线程能够彼此并发运行,而不是与主线程并发运行。

What i've effectively done is reduced a multithreaded architecture to a single threaded one using locks... which is silly. 我有效完成的工作是使用锁将多线程体系结构简化为单线程体系结构……这很愚蠢。 What I really want is an architecture that is multithreaded for most of the time, but then waits while a small operation completes and goes back to being multithreaded. 我真正想要的是一种大多数情况下都是多线程的体系结构,但是等待一小步操作完成后又回到多线程。

Edit: An explanation of the problem. 编辑:问题的解释。

What I have is an application that displays multiple video feeds coming from pcie capture cards. 我所拥有的应用程序可以显示来自pcie采集卡的多个视频源。 The pcie capture card driver issues callbacks on threads it manages into what is effectively the ManyBackgroundThreadsDoingWork function. pcie采集卡驱动程序在其管理的线程上发出回调,这些回调实际上是ManyBackgroundThreadsDoingWork函数。 In this function I copy the captured video frames into buffers for rendering. 在此功能中,我将捕获的视频帧复制到缓冲区以进行渲染。 The main thread is the render thread that runs intermittently. 主线程是间歇运行的渲染线程。 The copy threads need to block during the render to prevent tearing of the video. 复制线程需要在渲染期间阻塞,以防止视频撕裂。

My initial approach was to simply do double buffering but that is not really an option as the capture card driver won't allow me to buffer frames without pushing the frames through system memory. 我最初的方法是简单地进行双缓冲,但这并不是真正的选择,因为捕获卡驱动程序不允许我缓冲帧而不将帧推入系统内存。 The technique being used is called "DirectGMA" from AMD that allows the capture card to push video frames directly into the GPU memory. AMD使用的技术称为“ DirectGMA”,它允许采集卡将视频帧直接推入GPU内存。 The only method for synchronization is to put a glFence and mutex around the actual rendering as the capture card will be continuously streaming data to the GPU Memory. 同步的唯一方法是在实际渲染周围放置glFence和互斥锁,因为捕获卡将不断将数据流传输到GPU内存。 The driver offers no indication of when a frame transfer completes. 驱动程序不提供帧传输完成时间的指示。 The callback supplies enough information for me to know that a frame is ready to be transferred at which point I trigger the transfer. 回调提供了足够的信息,让我知道在准备触发传输时已准备好传输帧。 However, I need to block transfers during the scene render to prevent tearing and artifacts in the video. 但是,我需要在场景渲染期间阻止传输,以防止视频中的撕裂和伪影。 The technique described above is the suggested technique from the pcie card manufacturer. 上述技术是pcie卡制造商建议的技术。 The technique, however, breaks down when you want more than one video playing at a time. 但是,当您一次要播放多个视频时,该技术就会失效。 Thus, the question. 因此,这个问题。

You need a lock that supports both shared and exclusive locking modes, sometimes called a readers/writer lock . 您需要同时支持共享锁定模式和独占锁定模式的锁,有时也称为读取器/写入器锁 This permits multiple threads to get read (shared) locks until one thread requests an exclusive (write) lock. 这允许多个线程获得读取(共享)锁,直到一个线程请求排他(写入)锁。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM