How do I wake up a sleeping pthread?

一笑奈何 提交于 2019-11-30 09:57:44

What you are looking for is pthread_cond_t object, pthread_cond_timedwait and pthread_cond_wait functions. You could create conditional variable isThereAnyTaskToDo and wait on it in event thread. When new event is added, you just wake event thread with pthread_cond_signal().

You have several possibilities both on *NIX platforms and on Windows. Your timer thread should wait using some kind of timed wait on event/conditional variable object. On POSIX platforms you can use pthread_cond_timedwait(). On Windows, you can either choose to compute the necessary time delta and use WaitForSingleObject() on event handle, or you could use combination of event object with CreateTimerQueueTimer() or CreateWaitableTimer(). Boost does also have some synchronization primitives that you could use to implement this with the POSIX-like primitives but portably.

UPDATE:

POSIX does have some timer functionality as well, see create_timer()

caf

I agree with Greg and wilx - pthread_cond_timedwait() can be used to implement the behaviour you're after. I just wanted to add that you can simplify your event thread main loop:

  1. try to get the next event in the event queue
  2. If there is no pending event, go straight to 4
  3. Get the time that the next event is supposed to occur
  4. Wait on condition variable with pthread_cond_timedwait() until next event (or with pthread_cond_wait() if no scheduled events)
  5. Try to get the next event in the event queue
  6. If there are no events that have expired yet, go back to 4
  7. Update queue (remove event, re-insert if it's a repeating event)
  8. Jump back to 5

So you don't care why you woke up - whenever you wake up, you check the current time and run any events that have expired, then go back to waiting. In most cases when a new event is added you'll find that no events have expired, of course - you'll just recalculate the wait time.

You'll probably want to implement the queue as a Priority Queue, so that the next-event-to-expire is always at the front.

Your current solution contains race conditions - for example, here:

QueueLock();                      // lock around queue access
bool empty = eventqueue.empty();  // check if empty
QueueUnlock();                    // unlock

pthread_mutex_lock(&eventmutex);  // lock event mutex (for condition)
if (empty)
{
    pthread_cond_wait(&eventclock, &eventmutex); // wait forever if empty
}

Consider what happens if the queue is initially empty, but another thread races with this and pushes a new value in between QueueUnlock() and the pthread_mutex_lock(&eventmutex) - the wakeup for the new event will be missed. Note also that in SleepUntilNextEvent() you access eventqueue.top() without holding the queue lock.

The mutex passed to pthread_cond_wait() is supposed to be the mutex protecting the shared state that the signal is relevant to. In this case that "shared state" is the queue itself, so you can fix these problems by using just one mutex protecting the queue:

void Scheduler::RunEventLoop()
{

    pthread_mutex_lock(&queuemutex);
    while (threadrunning)
    {
        while (!eventqueue.empty() && e.Due())
        {                          // while pending due events exist
            Event e = eventqueue.top();
            eventqueue.pop();

            pthread_mutex_unlock(&queuemutex);
            e.DoEvent();           // perform the event
            e.Next();              // decrement repeat counter
            pthread_mutex_lock(&queuemutex);
                                   // reschedule event if necessary
            if (e.ShouldReschedule()) eventqueue.push(e);
        }

        SleepUntilNextEvent();     // wait for something to happen
    }
    pthread_mutex_unlock(&queuemutex);

    return;                        // if threadrunning is set to false, exit
}

/* Note: Called with queuemutex held */
void Scheduler::SleepUntilNextEvent()
{
    if (eventqueue.empty())
    {
        pthread_cond_wait(&eventclock, &queuemutex); // wait forever if empty
    }
    else
    {
        timespec t =                  // get absolute time of wakeup
            Bottime::GetMillisAsTimespec(eventqueue.top().Countdown() + 
                                         Bottime::GetCurrentTimeMillis());
        pthread_cond_timedwait(&eventclock, &queuemutex, &t); // sleep until event
    }
}

Note that pthread_cond_wait() and pthread_cond_timedwait() release the mutex while they are waiting (the mutex is released and the wait begins atomically with respect to the mutex being signalled), so the Scheduler is not holding the mutex while it is sleeping.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!