开发者

C++ Win/Linux thread syncronization Event

开发者 https://www.devze.com 2022-12-30 06:46 出处:网络
Hello I have some code that is cross-platform by unsing #ifdef OS, I have a Queue protected by a CriticalSection on Windows, and by a pthread_mutex_t on Linux.

Hello I have some code that is cross-platform by unsing #ifdef OS,

I have a Queue protected by a CriticalSection on Windows, and by a pthread_mutex_t on Linux.

I would like to implement a Wait(timeou开发者_如何转开发t) call that would block a thread until something has been enqueued. I though about using WaitForSingleObject on windows but it don't seem to support CriticalSection. Which Win32 and which Linux functions should I use to Wait and Signal for a condition to happen.

Thank


I think that boost's conditions might be what you need. It is crossplatform so you won't have to bother with different implementations depending on OS.

Another alternative is to use Windows Events with WaitForSingleObject() and the quite new linux eventfd() with select() or poll().


Using Boost will allow you to do threading and synchronization for both platforms without a bunch of ifdefs.


Seems like conditions variable is what I was looking for.

On windows They work with critical Section and SleepConditionVariableCS

On linux pthread_cond_timedwait work with pthread.

Thanks all.


With pthread, a condition variable.

On Windows, it looks like you want a Semaphore. Win32 Semaphores have a counter that starts at zero - at which point the handle is not signaled. As you add items to the queue, you would increase the semaphore counter with ReleaseSemaphore - Each count added to the semaphore will satisfy one call to a WaitforXXXObject function, so, if you added 3 items to a queue, you would ReleaseSemaphore with a count of 3. WaitFor... would then return 3 times before the handle became non signalled again.


You can (sort of) simulate a try with a timeout using TryEnterCriticalSection, but for the most part, if you want a timeout you might be better off using a mutex instead (when you get down to it, a critical section is mostly a wrapper around a mutex).

Another possibility would be to use the Win32 pthreads library, which will probably let your Linux code compile under Win32 unchanged (and you'd simply eliminate your own Win32 code).

This is pretty similar to the threading support that's been added to the C++ 0x library, though it doesn't (even try to) follow the new standard precisely. If you want to follow the standard, you could use Anthony Williams' Just Thread library (warning: fairly reasonably priced, but not free in either sense).

Edit (in response to Billy O'neal's questions): Thinking about it a bit more, there actually is source code easily available that shows most of what's going on. The CRITICAL_SECTION data structure is defined in winbase.h as a typedef of an RTL_CRITICAL_SECTION. That, in turn, is defined in WinNT.h as:

typedef struct _RTL_CRITICAL_SECTION {
    PRTL_CRITICAL_SECTION_DEBUG DebugInfo;

    //
    //  The following three fields control entering and exiting the critical
    //  section for the resource
    //

    LONG LockCount;
    LONG RecursionCount;
    HANDLE OwningThread;        // from the thread's ClientId->UniqueThread
    HANDLE LockSemaphore;
    ULONG_PTR SpinCount;        // force size on 64-bit systems when packed
} RTL_CRITICAL_SECTION, *PRTL_CRITICAL_SECTION;

If memory serves, the basic idea of how this is used runs something like:

  1. If this thread already owns the critical section, increment RecursionCount and return
  2. Otherwise, do SpinCount attempts to enter CS via fast path using atomic ops on LockCount
  3. Otherwise, wait on LockSemaphore
0

精彩评论

暂无评论...
验证码 换一张
取 消