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(timeout) 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:
If this thread already owns the critical section, increment RecursionCount and return
Otherwise, do SpinCount attempts to enter CS via fast path using atomic ops on LockCount
Otherwise, wait on LockSemaphore
Related
Linux is a new platform to me. I've coded on Windows in c++ for a number of years and have become comfortable with multithreading on that platform.
Along comes C++11 at a time when I need to learn c++ on the linux platform.
Linux appears to use pthreads for the most part - okay there's also boost::threads and QT have their own threads too. But with C++11 comes std::thread, a whole new (cross platform and C++ standard) way to do threads.
So I guess I'll have to learn pthreads and std::threads. Ultimately, std::thread seems more important, but there's a lot of legacy code out there, so I'll have to know both.
For thread synchronization on windows, I would use WaitForMultipleObjects to wait for a number of tasks to complete before continuing with further work.
Does a similar synchronization mechanism exist for pthreads? std::threads?
I've had a look at pthread_join, and it seems to have the facility to only wait on one thread at a time. Am I missing another pthread call maybe?
std::thread is boost::thread accepted into C++11 with some extras. My understanding is that if boost::thread gets replaced in code with std::thread it should still compile and work.
boost::thread is based on pthreads design, providing thin C++ wrappers over thread, mutex and condition variables. Thread cancellation though was left outside the scope of C++11, since there was no agreement how it should work in C++.
So, by learning pthreads you also learn std::thread concepts. std::thread adds mostly syntax sugar and convenience functions on top of pthreads C API.
With regards to WaitForMultipleObjects(), neither pthreads nor std::thread provide anything similar to its bWaitAll=FALSE mode, however, it's routinely simulated using pipes and select() on UNIX, or more modern eventfd() and epoll() on Linux. bWaitAll=TRUE mode can be simulated by waiting on all tasks in turn, since it doesn't proceed until all objects are ready anyway.
No, neither pthreads nor C++11 has direct equivalent of WaitForMultipleObjects (i.e. wait for any waitable "handle" type.) pthread_join can only be used to join threads, and only a single, specific thread.
The closest equivalent on posix platforms is to wait for multiple file descriptors using system calls such as select(), poll() or the linux-specific epoll(), but they require you to have a file descriptor to wait on, which is fine for I/O events but requires extra work from you to use them wait for mutexes, condition variables or other synchronisation objects. There are more general event libraries built on top of those system calls, e.g. libevent and libev and Boost ASIO, which support waiting for timers as well as I/O, but still not thread completion, mutex locks etc. with a single function like WaitForMultipleObjects
The alternatives you do have for pthreads and C++11 threads are to wait on different synchronisation types separately. You can wait for timers, wait for threads to complete, wait for mutexes, wait on condition variables, wait for asynchronous results to be ready (std::async in C++11, no direct equivalent in pthreads) ... but there's no call that will allow you to wait a heterogeneous set of those types all at once.
I could give you a really fancy answer but alas, this is where I learned them and it is a good introduction:
http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html
You use pthread_mutex_t for syncronization and pthread_join probably handles the wait for multiple tasks problem. It works exactly as you would expect.
Based on this, you must call pthread_join for each single thread you have created. Or to use mutexes, if there is a need to synchronize your threads.
Regarding WaitForMultipleObjects, this is generally called a Barrier Sync. Boost has an implementation called barrier. It uses conditional variables to implement it, in posix its a pthread_cond_t
Here is an answer I left recently explaining barrier sync.
I'm trying to use WaitForSingleObject(fork[leftFork], Infinite); to lock a variable using multiple threads but it doesn't seem to lock anything
I set the Handle fork[5] and then use the code below but it doesn't seem to lock anything.
while(forks[rightFork] == 0 || forks[leftFork] == 0) Sleep(0);
WaitForSingleObject(fork[leftFork], INFINITE);
forks[leftFork]--;
WaitForSingleObject(fork[rightFork], INFINITE);
forks[rightFork]--;
I have tried as a WaitForMultipleObjects as well and same result. When I create the mutex I use fork[i]= CreateMutex(NULL, FALSE,NULL);
I was wondering if this is only good for each thread or do they share it?
First of all, you haven't shown enough code for us to be able to help you with any great certainty of correctness. But, having made that proviso, I'm going to try anyway!
Your use of the word fork suggests to me that you are approaching Windows threading from a pthreads background. Windows threads are a little different. Rather confusingly, the most effective in-process mutex object in Windows is not the mutex, it is in fact the critical section.
The interface for the critical section is much simpler to use, it being essentially an acquire function and a corresponding release function. If you are synchronizing within a single process, and you need a simple lock (rather than, say, a semaphore), you should use critical sections rather than mutexes.
In fact, only yesterday here on Stack Overflow, I wrote a more detailed answer to a question which described the standard usage pattern for critical sections. That post has lots of links to the pertinent sections of MSDN documentation.
Having said that, it would appear that all you are trying to do is to synchronize the decrementing of an array of integer values. If that is so then you can do this most simply in a lock free manner with InterlockIncrement or one of its friends.
You only need to use a mutex when you are performing cross process synchronization. Indeed you should only use a mutex when you are synchronizing across a process because critical sections perform so much better (i.e. faster). Since you are updating a simple array here, and since there is no obviously visible IPC going on, I can only conclude that this really is in-process.
If I'm wrong and you really are doing cross-process work and require a mutex then we would need to see more code. For example I don't see any calls to ReleaseMutex. I don't know exactly how you are creating your mutexes.
If this doesn't help, please edit your question to include more code, and also a high level overview of what you are trying to achieve.
I need to write my own implementation of a condition variable much like pthread_cond_t.
I know I'll need to use the compiler provided primitives like __sync_val_compare_and_swap etc.
Does anyone know how I'd go about this please.
Thx
Correct implementation of condition variables is HARD. Use one of the many libraries out there instead (e.g. boost, pthreads-win32, my just::thread library)
You need to:
Keep a list of waiting threads (this might be a "virtual" list rather than an actual data structure)
Ensure that when a thread waits you atomically unlock the mutex owned by the waiting thread and add it to the list before that thread goes into a blocking OS call
Ensure that when the condition variable is notified then one of the threads waiting at that time is woken, and not one that waits later
Ensure that when the condition variable is broadcast then all of the threads waiting at that time are woken, and not any threads that wait later.
plus other issues that I can't think of just now.
The details vary with OS, as you are dependent on the OS blocking/waking primitives.
I need to write my own implementation of a condition variable much like pthread_cond_t.
The condition variables cannot be implemented using only the atomic primitives like compare-and-swap.
The purpose in life of the cond vars is to provide flexible mechanism for application to access the process/thread scheduler: put a thread into sleep and wake it up.
Atomic ops are implemented by the CPU, while process/thread scheduler is an OS territory. Without some supporting system call (or emulation using existing synchronization primitives) implementing cond vars is impossible.
Edit1. The only sensible example I know and can point you to is the implementation of the historical Linux pthread library which can be found here - e.g. version from 1997. The implementation (found in condvar.c file) is rather easy to read but also highlights the requirements for implementation of the cond vars. Spinlocks (using test-and-set op) are used for synchronizations and POSIX signals are used to put threads into sleep and to wake them up.
It depends on your requirements. IF you have no further requirements, and if your process may consume 100% of available CPU time, then you have the rare chance to experiment and try out different mutex and condition variables - just try it out, and learn about the details. Great thing.
But in reality, you are uusally bound to an operating system, and so you are captivated on the OSs threading primitives, because they represent the only kind of control to - yeah - process/threading/cpu ressource usage! So, in that case, you will not even have the chance to implement your OWN condition variables - if they are not based on the primites, that the OS provides you!
So... double check your environment, what do you control? What don't you control? And what makes sense?
I'm programming an interprocess communication module (Process A and Process B).
Is there any way the communication thread in B can run (be unlock) as soon as process A finishes a specific action, I mean without B doing any polling nor B waiting too much after A finishes its action?
Which are the concepts/models/design patterns governing these issues? (Something more precise than interprocess synchronization). Which libraries/methods do you recommend?
Thanks.
Edit: I'm looking for methods suitable for each of the three main OSes: Ms Windows, Apple Mac OS X, GNU/Linux.
This is quite hard job:
For Unix OSes you can use:
pthread condition and mutex with setpshared argument.
Note: it is supported well under Linux 2.6, Solaris, but it does not supported FreeBSD and Cygwin (don't know about Mac OS X)
For Unixes you may also use named semaphores, but I don't know the support level of them
For Windows there are some events...
This is hard job, especially for IPC...
So if you want something portable, I'd suggest to take a look on Boost.Interprocess that has conditions and mutexes...
But make sure that all feature supported on all OSes you want to support.
Things you should note about Boost.Interprocess
Check carefully level of support for each Unix OS you need to work with, because Boost.Interprosess uses pthread_* functions that are not always supported... and then fails back to emulation -- check the quality of such emulation
Also, check how this stuff works on Windows -- as far as I know that there is no "in-shared-memory" mutexes in Win32 API, generally named objects should be used, so check what is supported and how.
EDIT: I mistakenly thought you needed inter thread synchronizing, Revised for IPC
I think you need something like waitable events.
In Windows you can use CreateEvent(), to create (or get an existing) named, auto-reset event.
When process A completes processing, it should call SetEvent(), while process B should call WaitForSingleObject() to sleep until completion (or timeout).
Alternately, you can use semaphores created by CreateSemaphore(), initialized to 0.
Process A signals completion by calling ReleaseSemaphore(), while process B again uses WaitForSingleObject() to wait for completion.
Under Linux and OS X you can use semaphores to a similar effect.
use sem_open() to create a named semaphore, with 0 as its initial value.
When process A completes, it should call sem_post() to increment the semaphore, while process B should call sem_wait() to sleep until completion.
NOTE: the semaphore method may allow multiple completions to be signaled, you should handle this by setting a maximum count under Windows, or checking the current sem value for sanity with sem_getvalue()
I think condition variables fit what you're trying to do, here's a sample that would work on Linux and OSX
#include <pthread.h>
/* no error checking, quick and dirty sample */
pthread_mutex_t g_mutex;
pthread_cond_t g_cond;
int a_done = 0;
void init(void)
{
pthread_mutex_init(&g_mutex, NULL);
pthread_cond_init(&g_cond, NULL);
}
void thread_a(void *arg)
{
/* do something here... */
pthread_mutex_lock(&g_mutex);
a_done = 1;
pthread_cond_signal(&g_cond);
pthread_mutex_unlock(&g_mutex);
}
void thread_b(void *arg)
{
/* wait for a to complete */
pthread_mutex_lock(&g_mutex);
while (!a_done)
pthread_cond_wait(&g_cond, &g_mutex);
a_done = 0;
pthread_mutex_unlock(&g_mutex);
}
Under Windows, you can use pthreads-win32, or native condition variables under Vista, see the MSDN Condition Variables page for more information.
References:
pthread_cond_wait
pthread_cond_signal
If your OS supports signals you could unlock a mutex from a signal handler and send the signal from process A as soon as you finish the task.
Process B would be waiting on a mutex or other synchronization tool and A would be working on whatever, then when finishes sends signal USR1 for example, and USR1 handler in process B unlocks the corresponding synchronization tool.
The most common is to use select()/poll(). Both can check several file descriptors if there's input available. Both receive a timeout parameter - this will prevent busy wait, which may consume 100% CPU. This is very suitable solution for small/medium applications.
Another approach is to make polling in separate thread.
If you're going to develop a big application it's worth to look towards ACE framework or
boost. These frameworks are cross platform solutions, well designed and well tested.
Well, in my opinion and experience, the best way to do that in a portable and simple way is to use sockets. Plus you get the possibility to have the two processes on different machine (if needed). Plus you can extand the communication to handle more than synchro.
If you don't want to poll, use a thread that wait for a synchro message on a socket. You read the socket in a blocking way. When you receive the message you use a standard multithread synchronisation to handle your synchronisation. In your case as B should wait till A ends, you just have to read in a blocking way in your process.
To be portable use a portable socket library like boost or ptypes or whatever.
I have a main process that uses a single thread library and I can only the library functions from the main process. I have a thread spawned by the parent process that puts info it receives from the network into a queue.
I need to able to tell the main process that something is on the queue. Then it can access the queue and process the objects. The thread cannot process those objects because the library can only be called by one process.
I guess I need to use pipes and signals. I also read from various newsgroups that I need to use a 'self-trick' pipe.
How should this scenario be implemented?
A more specific case of the following post:
How can unix pipes be used between main process and thread?
Why not use a simple FIFO (named pipe)? The main process will automatically block until it can read something.
If it shouldn't block, it must be possible to poll instead, but maybe it will suck CPU. There probably exists an efficient library for this purpose.
I wouldn't recommend using signals because they are easy to get wrong. If you want to use them anyway, the easiest way I've found is:
Mask all signals in every thread,
A special thread handles signals with sigwait(). It may have to wake up another thread which will handle the signal, e.g. using condition variables.
The advantage is that you don't have to worry anymore about which function is safe to call from the handler.
The "optimal" solution depends quite a bit on your concrete setup. Do you have one process with a main thread and a child thread or do you have one parent process and a child process? Which OS and which thread library do you use?
The reason for the last question is that the current C++03 standard has no notion of a 'thread'. This means in particular that whatever solution your OS and your thread library offer are platform specific. The most portable solutions will only hide these specifics from you in their implementation.
In particular, C++ has no notion of threads in its memory model, nor does it have a notion of atomic operations, synchronization, ordered memory accesses, race conditions etc.
Chances are, however, that whatever library you are using already provides a solution for your problem on your platform.
I highly suggest you used a thread-safe queue such as this one (article and source code). I have personally used it and it's very simple to use. The API consist in simple methods such as push(), try_pop(), wait_and_pop() and empty().
Note that it is based on Boost.Thread.