How to Port Windows Interprocess Event with AutoResetEvent to Linux
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How to Port Windows Interprocess Event with AutoResetEvent to Linux
My current Windows code uses a named event for inter-process communication. There can be many processes waiting on the event, when it's signaled, all the waiting processes are released and then the event returns to a non-signaled state. I use the PulseEvent function for this.
How do I do this in Linux? The pthread functions seem to be inter-thread communication. I don't see where semaphores provide the same behavior as the Windows event. The Posix semaphore seems to only release one waiting thread. If the semaphore could release all waiting threads then that would work. Is there something in Linux I'm missing?
I don't think the init value matters since it still wouldn't provide the necessary behavior. The behavior needed is: the gate is closed, people queue up waiting for the gate to open, when the gate opens all those waiting are allowed through, then the gate closes again.
A semaphore would be more like football. You can have 11 people on the field at one time. As players come off the field, others can go on to take their place. This is fundamentally a different type of problem from the gated problem which pthread conditions support but only within the same process.
Are you just looking for pause and kill? You can wait for a signal, and send a signal to all members of a process group. You just need initialization to set up the process group and register the handler, and a signal handler for the signal you want to use.
One of the thorniest complexities of the Wine project was to provide suitable emulation of Windows process-synchronization primitives under the (very different!) auspices of Unix/Linux/POSIX. In fact, one of the main purposes of their wineserver process is specifically to support these very-advanced semantics.
But in this case, I think that condition variables will provide you with what you need.
Last edited by sundialsvcs; 12-17-2013 at 05:50 PM.
I don't know how it is implemented under Windows. I.e. I am not also saying how it's implemented in Linux - that's why I've decided to quickly go through the above PDF.
Intuitively the WINE idea to have a special server looks more appealing to me.
After you realize the man pages are misleading since you have to invoke the futex function via a syscall, it actually is quite trivial. You allocate a small chunk of named shared memory which is your value and leave it set to 0. Using the wait and wake options are all the threads/processes need to use. Whenever a wake is called, you provide a value for int max so all threads waiting are released. This provides the behavior of the Event in Windows using AutoResetEvent option. This is orders of magnitude less overhead than using a WINE approach.
After you realize the man pages are misleading since you have to invoke the futex function via a syscall, it actually is quite trivial. You allocate a small chunk of named shared memory which is your value and leave it set to 0. Using the wait and wake options are all the threads/processes need to use. Whenever a wake is called, you provide a value for int max so all threads waiting are released. This provides the behavior of the Event in Windows using AutoResetEvent option. This is orders of magnitude less overhead than using a WINE approach.
The syscall interface looks quite awkward. In Russian there is a joke:
Q: Что будет, если скрестить ужа с ежом ?
A: 2 километра колючей проволоки.
Translation:
Q: What are you going to get if you crossbreed adder (animal) with hedgehog ?
A: 2km of barbed wire.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.