mirror of
https://sourceware.org/git/glibc.git
synced 2024-11-30 00:31:08 +00:00
Update.
1998-12-13 Ulrich Drepper <drepper@cygnus.com> * Examples/ex3.c: Wait until all threads are started before searching for the number to avoid race condition on very fast systems.
This commit is contained in:
parent
763babf874
commit
e3743e2ffd
@ -1,3 +1,9 @@
|
|||||||
|
1998-12-13 Ulrich Drepper <drepper@cygnus.com>
|
||||||
|
|
||||||
|
* Examples/ex3.c: Wait until all threads are started before
|
||||||
|
searching for the number to avoid race condition on very fast
|
||||||
|
systems.
|
||||||
|
|
||||||
1998-12-08 Andreas Jaeger <aj@arthur.rhein-neckar.de>
|
1998-12-08 Andreas Jaeger <aj@arthur.rhein-neckar.de>
|
||||||
|
|
||||||
* sysdeps/pthread/pthread.h: Remove __pthread_setcanceltype
|
* sysdeps/pthread/pthread.h: Remove __pthread_setcanceltype
|
||||||
|
@ -19,6 +19,7 @@ void print_it(void *);
|
|||||||
pthread_t threads[NUM_THREADS];
|
pthread_t threads[NUM_THREADS];
|
||||||
pthread_mutex_t lock;
|
pthread_mutex_t lock;
|
||||||
int tries;
|
int tries;
|
||||||
|
volatile int started;
|
||||||
|
|
||||||
int main(int argc, char ** argv)
|
int main(int argc, char ** argv)
|
||||||
{
|
{
|
||||||
@ -33,8 +34,8 @@ int main(int argc, char ** argv)
|
|||||||
pthread_mutex_init(&lock, NULL);
|
pthread_mutex_init(&lock, NULL);
|
||||||
|
|
||||||
/* Create the searching threads */
|
/* Create the searching threads */
|
||||||
for (i=0; i<NUM_THREADS; i++)
|
for (started=0; started<NUM_THREADS; started++)
|
||||||
pthread_create(&threads[i], NULL, search, (void *)pid);
|
pthread_create(&threads[started], NULL, search, (void *)pid);
|
||||||
|
|
||||||
/* Wait for (join) all the searching threads */
|
/* Wait for (join) all the searching threads */
|
||||||
for (i=0; i<NUM_THREADS; i++)
|
for (i=0; i<NUM_THREADS; i++)
|
||||||
@ -74,7 +75,13 @@ void *search(void *arg)
|
|||||||
|
|
||||||
/* use the thread ID to set the seed for the random number generator */
|
/* use the thread ID to set the seed for the random number generator */
|
||||||
/* Since srand and rand are not thread-safe, serialize with lock */
|
/* Since srand and rand are not thread-safe, serialize with lock */
|
||||||
pthread_mutex_lock(&lock);
|
|
||||||
|
/* Try to lock the mutex lock --
|
||||||
|
if locked, check to see if the thread has been cancelled
|
||||||
|
if not locked then continue */
|
||||||
|
while (pthread_mutex_trylock(&lock) == EBUSY)
|
||||||
|
pthread_testcancel();
|
||||||
|
|
||||||
srand((int)tid);
|
srand((int)tid);
|
||||||
i = rand() & 0xFFFFFF;
|
i = rand() & 0xFFFFFF;
|
||||||
pthread_mutex_unlock(&lock);
|
pthread_mutex_unlock(&lock);
|
||||||
@ -87,6 +94,9 @@ void *search(void *arg)
|
|||||||
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
|
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
|
||||||
pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);
|
pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);
|
||||||
|
|
||||||
|
while (started < NUM_THREADS)
|
||||||
|
sched_yield ();
|
||||||
|
|
||||||
/* Push the cleanup routine (print_it) onto the thread
|
/* Push the cleanup routine (print_it) onto the thread
|
||||||
cleanup stack. This routine will be called when the
|
cleanup stack. This routine will be called when the
|
||||||
thread is cancelled. Also note that the pthread_cleanup_push
|
thread is cancelled. Also note that the pthread_cleanup_push
|
||||||
|
Loading…
Reference in New Issue
Block a user