Win32 alternative to pthread

Win32 alternative to pthread

本文关键字:pthread to alternative Win32      更新时间:2023-10-16

是否可以使用标准Win32 CreateMutex样式代码编写此内容。我只是想知道我是否想向我们的应用程序介绍一个新库,还是我能找到自己写这篇文章的方法。我只是无法弄清楚如何在关键节中等待。这是我当前使用Pthread库的工作代码。

T remove() {
    pthread_mutex_lock(&m_mutex);
    while (m_queue.size() == 0) {
        pthread_cond_wait(&m_condv, &m_mutex);
    }
    T item = m_queue.front();
    m_queue.pop_front();
    pthread_mutex_unlock(&m_mutex);
    return item;
}

在VC-2012前支持,最好的替代方案是Boost.Thread支持条件变量。

这是我的尝试。这不是Win32中有条件的等待锁的最佳实现,但我认为它有效。它可以使用仔细的代码审查审查。

一个警告 - 它不一定保证有序公平性,因为所有等待线程最初可能会阻止等待事件。调度程序将在这一点上恢复所有线程以继续运行(直至随后的阻塞肠callectioncection呼叫),但不一定按照线程到达remove()呼叫开始的顺序。对于大多数应用程序而言,这可能并不重要,只有几个线程,但这是大多数线程框架保证的东西。

其他警告 - 对于简洁起见,我遗漏了检查所有这些Win32 API的返回值的重要步骤。

CRITICAL_SECTION m_cs;
HANDLE m_event;
void Init()
{
   InitializeCriticalSection(&m_cs);
   m_event = CreateEvent(NULL, TRUE, FALSE, NULL); // manual reset event
}
void UnInit()
{
    DeleteCriticalSection(&m_cs);
    CloseHandle(m_event);
    m_event = NULL;
}
T remove()
{
    T item;
    bool fGotItem = false;
    while (fGotItem == false)
    {
        // wait for event to be signaled
        WaitForSingleObject(m_event, INFINITE);
        // wait for mutex to become available
        EnterCriticalSection(&m_cs);
        // inside critical section
        {
            // try to deque something - it’s possible that the queue is empty because another 
            // thread pre-empted us and got the last item in the queue before us
            size_t queue_size = m_queue.size();
            if (queue_size == 1)
            {
               // the queue is about to go empty
               ResetEvent(m_event);
            }
            if (queue_size > 0)
            {
                fGotItem = true;
                item = m_queue.front();
                m_queue.pop();        
            }
        }
        LeaveCriticalSection(&m_cs);
    }
    return item;
}
void Add(T& item)
{
    // wait for critical section to become available
    EnterCriticalSection(&m_cs);
    // inside critical section
    {
        m_queue.push_back(item);
        SetEvent(m_event); // signal other threads that something is available
    }
    LeaveCriticalSection(&m_cs);
}

Windows Vista引入了新的本机Win32条件变量和Slim Reader/Writer Locter locter Primitives cogry cogry for for thise of此类型的情况,例如:

使用关键部分:

CRITICAL_SECTION m_cs;
CONDITION_VARIABLE m_condv;
InitializeCriticalSection(&m_cs);
InitializeConditionVariable(&m_condv);
...
void add(T item)
{ 
    EnterCriticalSection(&m_cs);
    m_queue.push_back(item);
    LeaveCriticalSection(&m_cs);
    WakeConditionVariable(&m_condv);
}
T remove()
{ 
    EnterCriticalSection(&m_cs);
    while (m_queue.size() == 0)
        SleepConditionVariableCS(&m_condv, &m_cs, INFINITE);
    T item = m_queue.front();
    m_queue.pop_front();
    LeaveCriticalSection(&m_cs);
    return item;
}

使用SRW锁:

SRWLOCK m_lock;
CONDITION_VARIABLE m_condv;
InitializeSRWLock(&m_lock);
InitializeConditionVariable(&m_condv);
...
void add(T item)
{ 
    AcquireSRWLockExclusive(&m_lock);
    m_queue.push_back(item);
    ReleaseSRWLockExclusive(&m_lock);
    WakeConditionVariable(&m_condv);
}
T remove()
{ 
    AcquireSRWLockExclusive(&m_lock);
    while (m_queue.size() == 0)
        SleepConditionVariableSRW(&m_condv, &m_lock, INFINITE, 0);
    T item = m_queue.front();
    m_queue.pop_front();
    ReleaseSRWLockExclusive(&m_lock);
    return item;
}