Linux/POSIX equivalent for Win32's CreateEvent, SetEvent, WaitForSingleObject

linux posix event
pevents
waitforsingleobject
createevent
setevent
programming with posix threads
pthread_cond_init
best book on posix threads

I have written a small class for synchronizing threads of both Linux (actually Android) and Windows.

Here is the Win32 implementation of my interface :

    class SyncObjectWin32 : public SyncObject
    {
    private:

        const HANDLE m_hEvent;

    public:

        SyncObjectWin32()
          : m_hEvent( ::CreateEvent( NULL, FALSE, FALSE ) )
        {
            if( NULL == m_hEvent )
                throw core::Exception( "sys::SyncObjectWin32::SyncObjectWin32() - Failed to create event." );
        }

        ~SyncObjectWin32()
        {
            ::CloseHandle( m_hEvent );
        }

        void WaitForSignal()
        {
            ::WaitForSingleObject( m_hEvent );
        }

        void Signal()
        {
            ::SetEvent( m_hEvent );
        }
    };

The problem is that i'm not sure what would be the POSIX equivalent. So far i've written the following class, based on this SO question, but since the answer is incomplete i'm not sure about how to finish my class :

    class SyncObjectPosix
    {
    private:

        pthread_mutex_t m_oMutex;

    public:

        SyncObjectPosix()
        {
            pthread_mutex_lock( m_oMutex );                 // lock mutex
            bool & signalled = find_signal( condition );  // find predicate
            signalled = true;                          // set predicate
            pthread_mutex_unlock( m_oMutex );               // unlock mutex
            pthread_cond_signal( condition );            // signal condition variable
        }

        ~SyncObjectPosix()
        {

        }

        void WaitForSignal()
        {
            pthread_mutex_lock(mutex);                         // lock mutex
            bool & signalled = find_signal( condition );          // find predicate
            while (!signalled)
            {
              pthread_cond_timedwait(condition, m_oMutex, timeout);
            }
            signalled = false;                                 // reset predicate
            pthread_mutex_unlock( m_oMutex );                       // unlock mutex
        }

        void Signal()
        {

        }
    };

The POSIX equivalent for what you described is POSIX condition variables. Note that condition variable must always be used in pair with a POSIX mutex, but quite frequently several condition variables use the same mutex, so if you aren't going to use the mutex exclusively for the condition variable, you shouldn't place it in the class. The mappings by meaning in your case between Win32 and POSIX API should be:

CreateEvent -> pthread_cond_init

CloseHandle -> pthread_cond_destroy

WaitForSingleObject -> pthread_cond_wait or pthread_cond_timedwait

SetEvent -> pthread_cond_signal or pthread_cond_broadcast

Fortunately, there is a lot of documentation regarding this, though I recommend the fundamental Programming POSIX Threads.

Migrate Win32 C/C++ applications to Linux on POWER, Part 3, In Linux, the POSIX semaphore supports this functionality, but the pthreads do not. You need to Win32 code and equivalent Linux code  Run the program as-is on the Windows Subsystem for Linux (WSL). In WSL your program executes directly on the machine hardware, not in a virtual machine. WSL also enables direct filesystem calls between Windows and Linux systems, removing the need for SSL transport.


Check also eventfd. It seems to be almost equivalent of CreateEvent if you need just one consumer and one producer.

CreateEvent --> eventfd

CloseHandle --> close

SetEvent --> write

WaitForSingleObject --> read

WaitForMultipleObjects --> select and read corresponding fd

Some more reading http://www.sourcexr.com/articles/2013/10/26/lightweight-inter-process-signaling-with-eventfd

WaitForMultipleObjects and WIN32 events for Linux/OS X/*nix and , POSIX-compliant operating systems with pthreads offer additional really neat synchronization primitives not found on Windows, such as condition  Is there a rough equivalent to the Linux/Unix stdio.h popen() function in the Win32 API? If so, where can I find it? Edit: I need to know this to patch an omission in the D standard library. Any answer must use only standard Win32 API, no MSVC-specific functions. Also, I’d prefer something that’s not horribly low-level, if it exists.


The pthreads equivalent of your code is:

class SyncObjectPosix
{
private:

    bool signalled;
    pthread_mutex_t mutex;
    pthread_cond_t cond;

public:

    SyncObjectPosix()
    {
        signalled = false;
        pthread_mutex_init(&mutex, NULL);
        pthread_cond_init(&cond, NULL);
    }

    ~SyncObjectPosix()
    {
        pthread_mutex_destroy(&mutex);
        pthread_cond_destroy(&cond);
    }

    void WaitForSignal()
    {
        pthread_mutex_lock(&mutex);
        while (!signalled)
        {
            pthread_cond_wait(&cond, &mutex);
        }
        signalled = false;
        pthread_mutex_unlock(&mutex);
    }

    void Signal()
    {
        pthread_mutex_lock(&mutex);
        signalled = true;
        pthread_mutex_unlock(&mutex);
        pthread_cond_signal(&cond);
    }
};

Emulation of win32 event objects in POSIX, Event objects in POSIX. by Lars Melander. Contrary to popular belief, event objects – a construct particular to MS Windows and not found in *NIX OS:es – are​  I am trying to port code from Linux to Win32, and I've run into the situation where I need the equivalent to the "link" function defined for linux. The CopyFile function on Win32 won't do what I need.


Our open-source pevents library is an implementation of just that for all platforms. It's a very small (single-file) bit of C++ code that you can just add to your existing project and get access to the Windows event api built on top of pthreads synchronization primitives.

The most important tidbit is that it includes WaitForMultipleObjects support.

https://github.com/neosmart/pevents

Running Linux programs on Windows, Run the program as-is on the Windows Subsystem for Linux (WSL). code to use Windows-specific code (for example, Win32 or DirectX APIs). Is there a rough equivalent to the Linux/Unix stdio.h popen() function in the Win32 API? If so, where can I find it? Edit: I need to know this to patch an omission in the D standard library. Any answer must use only standard Win32 API, no MSVC-specific functions. Also, I'd prefer something that's not horribly low-level, if it exists.


neosmart/pevents: Implementation of Win32 events for *nix , pevents. pevents is a cross-platform low-level library meant to provide an implementation of the WIN32 events for POSIX systems. pevents is built on pthreads  The first (and most important) is an implementation of WIN32 manual/auto-reset events for Linux. While there’s nothing WIN32 events can do that POSIX condition variables can’t, the differences between the syntax and usage semantics of both has resulted in entirely different programming paradigms on the different platforms, making it hard for some developers to port code from one platform to the other or even write code from scratch on the platform they’re unfamiliar with.


Win32 API Emulation on UNIX for Software DSM, The situation using the POSIX thread API is even worse, because under Windows NT have more a less equivalent values under UNIX. (__SPARC), Solaris x86 (__X86) and Linux x86 (__LINUXX86) in the following way:. In Win32, _endthread() is used to end the execution of the current thread. In Linux, it is recommended to use pthread_exit() to exit a thread to avoid implicitly calling the exit routine. In Linux, the retval is the return value of the thread,


POSIX, The Portable Operating System Interface (POSIX) is a family of standards specified by the IEEE UWIN from AT&T Research implements a POSIX layer on top of the Win32 APIs. a Unix-like environment for scripting, connectivity and porting Unix and Linux software to both 32- and 64-bit Microsoft Windows systems. The "Core" Linux API is POSIX and glibc. Linux is NOT graphical by default, so there is no core graphics library. Really, Windows could be stripped down to not have graphics also and thus not have parts of the win32 API like GDI. This you must understand. Linux is very lightweight compared to Windows. For Linux there are two main graphical toolkits, GTK and Qt.