Ask a Question related to UNIX Programming, Design and Development.
-
John Eskie #1
Replacement for Win32 events
Hello,
Beeing a Windows programmer I got very little experience with the unix api.
From what I understood pthreads are needed for multithreaded programming. I
looked at the pthread api and got the following question.
I got a list of threads in my existing windows prog and during shutdown I
got a event which will be signalled to specify the threads should be shut
down.
It looks like this (only a sample of how it looks):
void ThreadFunc()
{
while (1)
{
if (waitforsingleobject(event, <return immediately>) == success) // If we
got a event then we quit the loop and shut the thread
break;
}
}
....
for (i = 0; i < all threads; i++)
{
setevent(threadlist[i].event); // signal that we want to quit the thread
}
for (i = 0; i < all threads; i++)
{
if (waitforsingleobject(threadlist[i].event, <max. 500 millisecond
delay>) == success) // if thread didn't shutdown we wait max. 500 msec and
force it to be killed.
terminate_thread(threadlist[i].event);
}
I assume I need to use some of those semaphore functions but I am not sure.
I checked out some code which had both win32 and unix code in same code base
but it didn't handle delaying like the 500 millisecond I did.
Thanks in advance.
-- John
John Eskie Guest
-
Contribute replacement
I've been having sporadic problems with connection and publishing errors spanning several users and websites. I've also perused this forum regularly... -
Win32::OLE, InternetExplorer, NewWindow, Events --- crashes
I'm working on an application that automates IE and needs to handle IE's NewWindow2 event. The IE documentation for the event gives the following... -
Dave Roth's site (Win32::AdminMisc, Win32::ODBC, etc.) not available.
Does anyone know of an alternate method to contact Dave Roth (other then rothd@roth.net )? It appears that his entire domain is unavailable... -
Win32-PerfMon on Win32
Windows 2000(SP4) ActivePerl 5.8.3 I found this escapade rather confusing, I'm warning you now. I cannot install this Win32-PerfMon module, I... -
Is Win32::OLE qw(EVENTS) ~ to the VBA "Impliments" statement?
Hey, I need to export a client interface into a Win32 COM server, to interact w/ an API. VB apparently does this by saying "Impliments foo" or... -
Sean Burke #2
Re: Replacement for Win32 events
"John Eskie" <cyberheg@l115.langkaer.dk> writes:
I think that this could most easily be done using condition variables.> Hello,
>
> Beeing a Windows programmer I got very little experience with the unix api.
> From what I understood pthreads are needed for multithreaded programming. I
> looked at the pthread api and got the following question.
>
> I got a list of threads in my existing windows prog and during shutdown I
> got a event which will be signalled to specify the threads should be shut
> down.
>
> It looks like this (only a sample of how it looks):
>
> void ThreadFunc()
> {
> while (1)
> {
> if (waitforsingleobject(event, <return immediately>) == success) // If we
> got a event then we quit the loop and shut the thread
> break;
> }
> }
>
>
> ...
> for (i = 0; i < all threads; i++)
> {
> setevent(threadlist[i].event); // signal that we want to quit the thread
> }
>
> for (i = 0; i < all threads; i++)
> {
> if (waitforsingleobject(threadlist[i].event, <max. 500 millisecond
> delay>) == success) // if thread didn't shutdown we wait max. 500 msec and
> force it to be killed.
> terminate_thread(threadlist[i].event);
> }
>
> I assume I need to use some of those semaphore functions but I am not sure.
> I checked out some code which had both win32 and unix code in same code base
> but it didn't handle delaying like the 500 millisecond I did.
>
> Thanks in advance.
>
> -- John
Condition variables are used to signal state changes between threads,
but they are different in that they don't hold any state themselves.
(You can't test if a condition _was_ signalled, you can only wait for
it to _be_ signalled.)
So along with the condition variable, you need more data to represent
the state that is changing, and a mutex to serialize shared access to
that state data.
The following enum defines the protocol between your master thread
and the worker threads:
typedef enum {
ThrInit = 0, // Initial state,
ThrShutdownReq, // Thread requested to shut down
ThrShutdown // Thread has shut down.
} ThrState_t;
Next you need an array of these for all of your threads:
ThrState_t ThrState[MAX_THREADS];
We can use one mutex to protect the whole array:
pthread_mutex_t ThrStateMutex = PTHREAD_MUTEX_INITIALIZER;
And we can use one condition variable too:
pthread_cond_t ThrStateCond = PTHREAD_COND_INITIALIZER;
A thread can wait for the master to signal the ThrShutdownReq
state like so:
pthread_mutex_lock(&ThrStateMutex);
// Wait for the shutdown request
//
while (ThrState[my_thread_num] != ThrShutdownReq)
if ((rc = pthread_cond_wait(&ThrStateCond, &ThrStateMutex)) != 0)
break;
// Indicate that we've shut down.
// Should also catch errors when rc != 0
//
ThrState[my_thread_num] = ThrShutdown
// Broadcast the state change so the master thread will see it.
//
pthread_cond_broadcast(&ThrStateCond);
pthread_mutex_unlock(&ThrStateMutex);
The master thread can shutdown the thread number thread_num
in this way:
pthread_mutex_lock(&ThrStateMutex);
// Indicate that the thread should shut down.
//
ThrState[thread_num] = ThrShutdownReq;
// Broadcast the state change so the thread will see it.
//
pthread_cond_broadcast(&ThrStateCond);
// Wait for the thread shutdown to complete.
//
while (ThrState[thread_num] != ThrShutdown)
if ((rc = pthread_cond_wait(&ThrStateCond, &ThrStateMutex)) != 0)
break;
pthread_mutex_unlock(&ThrStateMutex);
If you want to have a timeout on your wait, then you would
use pthread_cond_timedwait().
Note that we since we are sharing a single mutex-condvar pair
among all threads, we must use pthread_cond_broadcast to wake
all of the threads that are waiting on the ThrStateCond.
This design produces needless wakeups, but it should not be
a performance issue. If it is, you could allocate separate
condvar-mutex pairs for each worker thread.
Note that even if you dedicate separate condvars, you must
still test explicitly for the state change on each return
from pthread_cond_wait(), since POSIX allows it to return
"spuriously" - even when it hasn't been signaled.
Hope this helps. Adding comp.programming.threads to followups.
-SEan
--
Remove the pi to email me.
Sean Burke Guest



Reply With Quote

