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: