简体   繁体   English

为什么当我在其中什么都不做(不做任何循环)时,我的 while 循环不起作用?

[英]Why is my while loop not working when I do nothing inside of it (do-nothing loop)?

I'm simply trying to use a while loop to help control the frames per second of my program that displays a live camera feed along with data corresponding to what that camera is looking at.我只是试图使用一个while循环来帮助控制我的程序的每秒帧数,该程序显示实时摄像机源以及与摄像机正在查看的数据相对应的数据。 The reason this is not trivial is because the loop that is calling getFramesAfterCapture() is running much faster than the camera can acquire images (since it is being called in an update method that collects the current frame and the data that both need to be displayed).这不是微不足道的原因是因为调用getFramesAfterCapture()的循环运行速度比相机获取图像的速度要快得多(因为它是在一个更新方法中调用的,该方法收集当前帧和都需要显示的数据)。

My idea was to have this function simply wait like using a mutex until the the condition in my while loop is no longer met.我的想法是让这个 function 像使用互斥锁一样等待,直到不再满足我的 while 循环中的条件。 The callback is running in another thread and captureComplete is a static member variable of the class so it is the only instance of it.回调在另一个线程中运行,并且captureComplete是 class 的 static 成员变量,因此它是它的唯一实例。 The value of this bool changes when the callback that captures the frames from my camera completes capturing a new image.当从我的相机捕获帧的回调完成捕获新图像时,此 bool 的值会发生变化。 It must be done in this way because I need the data that is being displayed to the screen to be synched with the capture of the frames this way the data and video display match.必须以这种方式完成,因为我需要将显示到屏幕的数据与帧的捕获同步,这样数据和视频显示匹配。

Here is my code:这是我的代码:

    long CALLBACK FrameGrabber::destCallback(LPVOID lpUser, LPVOID lpReserved) {
        // copy the data into the bitmap
        if (WaitForSingleObject(hMutex, 10) == WAIT_OBJECT_0)
        {
            DWORD dwSize = VIDEO_SIZE;
            //get bitmap
            dpGetSurfaceData(hDest, bitmap + sizeof(BITMAPINFOHEADER), &dwSize);
    
            ReleaseMutex(hMutex);
            // set capture to complete so that waitForCapture releases from the do-nothing loop
            WaitForSingleObject(captureCmpltMutex, INFINITE);
            captureComplete = true;
            ReleaseMutex(captureCmpltMutex);
        }
        
        return 0;
    }
    
    BYTE* FrameGrabber::getFrameAfterCapture() {
        // loop while capture is still not completed
        ////while (!captureComplete);
        while (!captureComplete) { 
            std::cout << "Waiting for capture to complete..." << std::endl;
        }
    
        //std::cout << "Capture Complete!" << std::endl;
        // when capture is completed set captureComplete back to false so that this waits again until the next frame is acquired
        WaitForSingleObject(captureCmpltMutex, INFINITE);
        captureComplete = false;
        ReleaseMutex(captureCmpltMutex);
    
        return bitmap;
    }

My loop works fine as it is...我的循环工作正常,因为它是......

    while (!captureComplete) { 
        std::cout << "Waiting for capture to complete..." << std::endl;
    }

But does not work when I change it to this...但是当我将其更改为此时不起作用...

    while (!captureComplete);

Or this...或这个...

    while (!captureComplete) { }

Or this...或这个...

    while (!captureComplete) continue;

Or this...或这个...

    while (!captureComplete){ continue; }

I prefer any of the other options because I would rather not keep a print statement in my program when it is complete.我更喜欢其他任何选项,因为我宁愿在程序完成后不在程序中保留打印语句。 I want my code here to look nice and clean like the other options that don't seem to work.我希望我的代码看起来像其他似乎不起作用的选项一样干净整洁。

If there is another more elegant option I am willing to change it I just need a solution that works and looks clean.如果有另一个更优雅的选择,我愿意改变它,我只需要一个有效且看起来干净的解决方案。

I was able to figure out a more elegant solution using a binary semaphore instead of a mutex or a static member boolean.我能够使用二进制信号量而不是互斥锁或 static 成员 boolean 找出更优雅的解决方案。 The difference between using a mutex and a binary semaphore is that a binary semaphore does not require me to release the semaphore at the end of getFrameCapture().使用互斥锁和二进制信号量的区别在于,二进制信号量不需要我在 getFrameCapture() 结束时释放信号量。 Basically when a semaphore is released, the count it holds is increased by one.基本上,当一个信号量被释放时,它持有的计数加一。 And oppositely when the wait function returns, the count the semaphore holds is decreased by one.相反,当等待 function 返回时,信号量持有的计数减一。

This allowed for the signal behavior where if I were to wait for the mutex at the beginning and release it just before the getFrameCapture() method returns the whole point of the function waiting for the frame to be captured is lost.这允许信号行为,如果我要在开始时等待互斥锁并在 getFrameCapture() 方法返回 function 等待捕获帧的整个点之前释放它就会丢失。 Likewise if I were to not release it just before the function is returned, the mutex will return WAIT_ABANDONED telling the programmer that the thread exited unexpectedly and the mutex has been abandoned by it.同样,如果我在 function 返回之前不释放它,互斥锁将返回 WAIT_ABANDONED 告诉程序员线程意外退出并且互斥锁已被它放弃。

Sol:溶胶:

long CALLBACK FrameGrabber::destCallback(LPVOID lpUser, LPVOID lpReserved) {
    // copy the data into the bitmap
    if (WaitForSingleObject(hMutex, 10) == WAIT_OBJECT_0)
    {
        DWORD dwSize = VIDEO_SIZE;
        //get bitmap
        dpGetSurfaceData(hDest, bitmap + sizeof(BITMAPINFOHEADER), &dwSize);

        ReleaseMutex(hMutex);
        //Release semaphore (emit signal)
        ReleaseSemaphore(captureCmpltSemaphore, 1, NULL);
    }
    
    return 0;
}

BYTE* FrameGrabber::getFrameAfterCapture() {
    //Wait for semaphore to be released (Signaled)
    WaitForSingleObject(captureCmpltSemaphore, INFINITE);
    //return bitmap
    return bitmap;
}

I referenced this link to find my solution: https://docs.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-releasesemaphore我参考了这个链接来找到我的解决方案: https://docs.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-releasesemaphore

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

相关问题 如何定义“Do-Nothing”排序? - How can I define a “Do-Nothing” sort? 用任何签名创建一个“do-nothing”`std :: function`? - Create a “do-nothing” `std::function` with any signature? 为什么我在 do while 循环中的 switch case 无法正常工作,并且即使它应该继续,它也会跳出循环? - Why is my switch case which is inside a do while loop not working and breaks out of the loop even when it should continue? 三元运算符的空白或无任何第二或第三个参数 - Blank or do-nothing 2nd or 3rd argument for ternary operator 为什么我应该没有错时却出现链接错误 - Why do I get a linking error when there should be nothing wrong 为什么从while循环中遇到分段错误? - Why do I get a segmentation fault from my while loop? 为什么我在do while语句中遇到无限循环? - why I get the infinite loop in my do while statement? 不输入任何内容时结束循环 - Ending a loop when nothing is entered 当我希望它返回菜单时,为什么我的 Do-While 循环不断中断 - Why does my Do-While loop keep breaking when I want it to return back to the Menu 为什么当我输入一个被接受的数字时我的 do-while 循环没有中断? - Why is my do-while loop not breaking when I enter a number that is accepted?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM