Commit 63b3a44f by Loren J. Rittle Committed by Loren J. Rittle

pthread1.cc: Use one condition variable per predicate instead of tricky use of one condition...

        * testsuite/thread/pthread1.cc: Use one condition variable
        per predicate instead of tricky use of one condition variable.

From-SVN: r49239
parent 33c7f925
2002-01-25 Loren Rittle <ljrittle@acm.org>
* testsuite/thread/pthread1.cc: Use one condition variable
per predicate instead of tricky use of one condition variable.
2002-01-25 Benjamin Kosnik <bkoz@redhat.com> 2002-01-25 Benjamin Kosnik <bkoz@redhat.com>
* include/bits/fstream.tcc (filebuf::close()): Fix close for input * include/bits/fstream.tcc (filebuf::close()): Fix close for input
......
...@@ -48,19 +48,19 @@ public: ...@@ -48,19 +48,19 @@ public:
task_queue () task_queue ()
{ {
pthread_mutex_init (&fooLock, NULL); pthread_mutex_init (&fooLock, NULL);
pthread_cond_init (&fooCond, NULL); pthread_cond_init (&fooCond1, NULL);
pthread_cond_init (&fooCond2, NULL);
} }
~task_queue () ~task_queue ()
{ {
pthread_mutex_destroy (&fooLock); pthread_mutex_destroy (&fooLock);
pthread_cond_destroy (&fooCond); pthread_cond_destroy (&fooCond1);
pthread_cond_destroy (&fooCond2);
} }
list<int> foo; list<int> foo;
pthread_mutex_t fooLock; pthread_mutex_t fooLock;
// This code uses a special case that allows us to use just one pthread_cond_t fooCond1;
// condition variable - in general, don't use this idiom unless you pthread_cond_t fooCond2;
// know what you are doing. ;-)
pthread_cond_t fooCond;
}; };
void* void*
...@@ -72,9 +72,9 @@ produce (void* t) ...@@ -72,9 +72,9 @@ produce (void* t)
{ {
pthread_mutex_lock (&tq.fooLock); pthread_mutex_lock (&tq.fooLock);
while (tq.foo.size () >= max_size) while (tq.foo.size () >= max_size)
pthread_cond_wait (&tq.fooCond, &tq.fooLock); pthread_cond_wait (&tq.fooCond1, &tq.fooLock);
tq.foo.push_back (num++); tq.foo.push_back (num++);
pthread_cond_signal (&tq.fooCond); pthread_cond_signal (&tq.fooCond2);
pthread_mutex_unlock (&tq.fooLock); pthread_mutex_unlock (&tq.fooLock);
} }
return 0; return 0;
...@@ -89,11 +89,11 @@ consume (void* t) ...@@ -89,11 +89,11 @@ consume (void* t)
{ {
pthread_mutex_lock (&tq.fooLock); pthread_mutex_lock (&tq.fooLock);
while (tq.foo.size () == 0) while (tq.foo.size () == 0)
pthread_cond_wait (&tq.fooCond, &tq.fooLock); pthread_cond_wait (&tq.fooCond2, &tq.fooLock);
if (tq.foo.front () != num++) if (tq.foo.front () != num++)
abort (); abort ();
tq.foo.pop_front (); tq.foo.pop_front ();
pthread_cond_signal (&tq.fooCond); pthread_cond_signal (&tq.fooCond1);
pthread_mutex_unlock (&tq.fooLock); pthread_mutex_unlock (&tq.fooLock);
} }
return 0; return 0;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment