현재 완료된 std :: thread 결합과 관련하여 문제가 있습니다.완료된 std :: thread로 결합시 교착 상태가 발생했습니다.
//Consumer.h
class Consumer
{
public:
//Ctor, Dtor
void Start();
void Release();
private:
void Run();
std::atomic<bool> m_terminate;
std::thread m_thread;
};
//Consumer.cpp
void Consumer::Start()
{
m_thread = std::thread(&EncodingProcessor::Run, this);
}
void Consumer::Release()
{
m_terminate = true;
if (m_thread.joinable())
m_thread.join(); //Here is the deadlock
}
void Consumer::Run()
{
while (true)
{
if (m_terminate)
break;
//This queue is blocking, it is fed with events from an encoder
PMVR::HardwareEncoder::EventWithId currentEvent = m_hwEncoder.GetEncodeEvent();
//If there is an event, again, wait for it, until the according frame is encoded
WaitForSingleObject(currentEvent.handle, INFINITE);
//Lock the encoded bitstream
PMVR::HardwareEncoder::BitStreamBufferInfo currentData =
m_hwEncoder.LockBitstream(currentEvent.id);
std::vector<unsigned char> tmp((unsigned char*)currentData.bitstreamPointer,
(unsigned char*)currentData.bitstreamPointer + currentData.bitstreamSize);
//And process it.
m_consumer->ProcessEncodingResult(currentData.bitstreamPointer, currentData.bitstreamSize);
m_hwEncoder.UnlockBitstream(currentEvent.id);
}
}
그래서 내가 스레드를 시작할 수 있습니다 나는 단순한 소비자 클래스가 있습니다. 스레드는 필요한 작업을 수행합니다. 스레드를 끝낼 수 있으므로 Run()
내부의 루프가 손상됩니다. 그러나 스레드에 참여하려면 교착 상태가 발생합니다.
우리는 main()
이 완료된 후에 공개 된 스레드 beeing에 대해 이야기하지 않습니다. 나는 Release()
키를 누를 수도 있지만 작동하지 않을 수도 있습니다.
편집 : Start()
이 방법으로 호출됩니다
m_processorThread = new Consumer(*m_hwEncoder,
std::make_unique<FileSystemWriter>("file.h264"));
m_processorThread->Start();
Release()
이 방법으로 호출됩니다
if (glfwGetKey(handler->GetWindow(), GLFW_KEY_M) && !m_pressed)
{
m_pressed = true;
sessionAPI.Close();
}
sessionAPI.close()
은 Release()
를 호출합니다. 아무것도 더.
는 Edit2가 :
미안 해요, 당신 말이 맞아요. 지금까지 게시 한 코드는 작동 중입니다 ... 문제는 Run()
메서드 내부에있는 것 같습니다 (업데이트 된 내용, 위 참조).
루프 상단에서 깨기 때문에 그 아래의 모든 내용이 실행되지 않을 것입니다 ... GetEncodeEvent()
이 교착 상태를 만드는 것처럼 보입니다. 하지만 왜? 스레드가 무언가를 기다리지 않는 지점에서 전체 루프를 깨는 우아한 방법이 있습니까? 또한, 이벤트의 제공자는 여전히 살아있다, 그래서
'Run'과'Release' 호출 방법을 보여줄 수 있습니까? – NathanOliver
전체 [mcve]가 크게 도움이됩니다. – AndyG
'EncodingProcessor :: Run()'은 무엇을하고 있습니까? 다른 스레드에서 실행되는 함수 인 것 같습니다. 그래서 교착 상태에 빠질 수있는 이유를 알아 내야합니다. – bnaecker