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.
Hi, I'm trying to find some portable way to create a memory fence (aka memory barrier) in a multithreaded program that should run on Linux and AIX, compiled with gcc-4.8.x or newer (though it wouldn't hurt if the solution weren't gcc-specific).
Gcc-4.8.x doesn't support C11 stdatomic.h but it has __sync_synchronize
In newer gcc versions there is atomic_thread_fence from stdatomic.h
Also some pthread-functions imply memory fence as a side-effect but I didn't find a standalone 'pthread_msync' (or similar).
Also some other functions (e.g. semop) imply memory fence.
Right now I think it will be some #ifdef-hacking depending on gcc-version, but I would like to hear some better suggestions.
Note: part of the problem is that instead of semaphores/mutexes I use pipes for thread-synchronization/communication as it feels more flexible: for example you cannot wait for two or more semaphores/mutexes in the same time, but you can wait for two or more pipes to become readable with select(2).
Doesn't seem to be much action on this. I like the idea of synchronizing on pipes - I can picture something like erlangs message passing - but it does seem to me you can synchronize on multiple semaphores. Below is a trivial example where on thread waits for events to occur on one thread before signaling a third thread.
About waiting for more than one sources: say there is a global request-queue and a worker-thread-specific request-queue (for special usages, e.g.: terminate this thread).
The controller thread usually sends requests to the global queue, but sometimes sends thread-specific requests too.
Now the worker-threads should wait on both request-queues simultaneously.
About waiting for more than one sources: say there is a global request-queue and a worker-thread-specific request-queue (for special usages, e.g.: terminate this thread).
The controller thread usually sends requests to the global queue, but sometimes sends thread-specific requests too.
Now the worker-threads should wait on both request-queues simultaneously.
Terminating the thread can be handled by pthread_cancel of course but it does seem that asking a thread to do something different then normal, and do it now vs, do it the next time you're selected, is outside of the scope of what the pthread synchronization methods were designed for. If it isn't time dependent, such as reset your state before the next run, a flag would handle it.
I think I would be inclined to handle the case where the thread needs to perform a different action on request, with a separate set of threads.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.