如何在c ++中中断Sleep线程 [英] How to interupt the Sleeping thread in c++

查看:767
本文介绍了如何在c ++中中断Sleep线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在vs 2008中编码。



我的代码是:

 queue< int> ; globalqueue; 
DWORD WINAPI popq( void * vp)
{
while 1
{
while (globalqueue.size()> 0 )
{
EnterCriticalSection(& section);
if (myqueue.size()> 0
{
printf( %d \ n,myqueue.front());
myqueue.pop();
}
LeaveCriticalSection(& section);
}
sleep( 1000 * 60 );
}
return 0 ;
}



另一个函数将插入队列。如果队列中没有数据可供弹出,我正在睡觉60秒。

在这里我不想睡觉。如果我要进入队列的任何数据,我需要立即唤醒线程。



有没有办法实现这个目标?

解决方案

我最近回答了一个问题,其中包含一个可能完全符合您需求的阻塞队列模板:具有条件变量的多生产者/消费者线程......为什么不能正常工作? [ ^ ]。仔细阅读答案,因为它也可能包含很多有用的信息。你的答案需要的是 BlockingQueue 模板。您可以通过在队列中放置一个特殊项目来中断程序执行结束时的睡眠 - 在我的示例中,我使用了一个NULL指针。



关于CRITICAL_SECTION + windows事件组合:事件和关键部分(关键部分== lock == mutex)通常一起用于在多个线程之间进行同步。多次触发的事件(不仅仅是一次)几乎总是与附带的锁(临界区或互斥或其他)一起使用。如果您是初学者,我对您使用criticalsection +事件组合有一个非常好的建议:当您没有持有锁时总是等待事件,并且在您持有锁时始终设置/重置事件。 (在持有锁时我的意思是被输入临界区)。这条规则有一些例外,但是当我开始使用线程时,这个例子对我编写正确的代码有很大帮助。


你可以使用WaitForMultipleObjects()API,它也有一个超时。使用SetEvent()API,您可以触发循环。



http://msdn.microsoft.com/en-us/library/windows/desktop/ms687025(v=vs.85).aspx [ ^

I am doing coding in vs 2008.

My code is:

queue<int> globalqueue;
DWORD WINAPI popq(void *vp)
{
    while(1)
    {
        while(globalqueue.size() >0 )
        {
            EnterCriticalSection(&section);
            if( myqueue.size() > 0 )
            {
                printf("%d\n", myqueue.front());
                myqueue.pop();
            }
            LeaveCriticalSection(&section);
        }
        sleep(1000*60);
    }
    return 0;
}


Another function will insert into the queue. If data is not available in the queue to pop I am sleeping for 60 sec.
Here i don't want to sleep. If any data I am pushing into the queue I need to wake up the thread immediately.

Is there any way to achieve that?

解决方案

I have recently answered a question that contains a blocking queue template that probably suits your needs perfectly: Multi producer/consumer threads with condition variables...why don't work well?[^]. Read the answer carefully as it may contain a lot of useful info for you too. The stuff you need from my answer is the BlockingQueue template. You can interrupt the sleep at the end of program execution by placing a special item into the queue - in my example I used a NULL pointer for this.

Regarding the CRITICAL_SECTION + windows event combo: Events and critical sections (critical section == lock == mutex) are often used together to do synchronization between multiple threads. An event that is triggered multiple times (not just once) is almost always used with an accompanying lock (critical section or mutex or whatever). If you are a beginner I have a very nice advice for you regarding the use of criticalsection+event combo: Always wait for the event when you don't hold the lock, and always set/reset the event while you are holding the lock. (On holding the lock I mean being entered in the criticalsection). There are some exceptions to this rule but this one helped me a lot in writing correct code when I began with threading.


you can use WaitForMultipleObjects() API which also has a timeout. With the SetEvent() API you can trigger a loop.

http://msdn.microsoft.com/en-us/library/windows/desktop/ms687025(v=vs.85).aspx[^]


这篇关于如何在c ++中中断Sleep线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆