概述
下面代码通过WaitForMultipleObjects防止线程空跑,而降低了CPU
int push_packet(AVPacket* packet)
{
AVPacket* t_packet = (AVPacket*)av_malloc(sizeof(AVPacket));;
av_init_packet(t_packet);
int nRet = av_copy_packet(t_packet,packet);
if(nRet != 0)
{
av_free(t_packet);
t_packet = NULL;
LOG_THIS_INFO("av_copy_packet fail.");
return -1;
}
m_Mutex.lock();
m_list_packets.push_back(t_packet);
m_Mutex.unlock();
SetEvent(m_event);
return 0;
}
bCan = true;
while(bCan)
{
.....
HANDLE events[2];
m_Mutex.lock();
events[0] = _cancel_flag;
events[1] = m_event;
m_Mutex.unlock();
DWORD events_cnt =2;
ResetEvent(m_event);
WaitForMultipleObjects(events_cnt,events,FALSE, INFINITE );
switch (dwEvent)
{
case WAIT_OBJECT_0 + 0: /* to stop thread */
{
bCan = false;
}
break;
case WAIT_OBJECT_0 + 1:
{
}
break;
}
}
最后
以上就是贪玩柜子为你收集整理的WaitForMultipleObjects 降低cpu的全部内容,希望文章能够帮你解决WaitForMultipleObjects 降低cpu所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复