mirror of
https://github.com/Zenithsiz/ftmemsim-valgrind.git
synced 2026-02-03 10:05:29 +00:00
Improve pth_cvsimple test in two ways:
1. Make the output deterministic; different thread interleaving from expected was causing failures for me. 2. Make it actually use the condition variable -- the condvar stupidly wasn't actually being used in the expected case, because the other threads finished all their work before pthread_cond_wait() even got called, and this prevented the condition guarding pthread_cond_wait() from succeeding. git-svn-id: svn://svn.valgrind.org/valgrind/trunk@2952
This commit is contained in:
parent
6e3638e2cf
commit
e367976dde
@ -9,7 +9,7 @@
|
||||
*
|
||||
* cvsimple.c
|
||||
*
|
||||
* Demonstrates pthread cancellation.
|
||||
* Demonstrates pthread condvars.
|
||||
*
|
||||
*/
|
||||
|
||||
@ -20,23 +20,22 @@
|
||||
#define TCOUNT 10
|
||||
#define COUNT_THRES 12
|
||||
|
||||
int condvar_was_hit = 0;
|
||||
int count = 0;
|
||||
int thread_ids[3] = {0,1,2};
|
||||
pthread_mutex_t count_lock=PTHREAD_MUTEX_INITIALIZER;
|
||||
pthread_cond_t count_hit_threshold=PTHREAD_COND_INITIALIZER;
|
||||
|
||||
void *inc_count(void *idp)
|
||||
void *inc_count(void *null)
|
||||
{
|
||||
int i=0;
|
||||
int *my_id = idp;
|
||||
|
||||
for (i=0; i<TCOUNT; i++) {
|
||||
pthread_mutex_lock(&count_lock);
|
||||
count++;
|
||||
printf("inc_counter(): thread %d, count = %d, unlocking mutex\n",
|
||||
*my_id, count);
|
||||
printf("inc_counter(): count = %d, unlocking mutex\n", count);
|
||||
if (count == COUNT_THRES) {
|
||||
printf("inc_count(): Thread %d, count %d\n", *my_id, count);
|
||||
printf("hit threshold!\n");
|
||||
pthread_cond_signal(&count_hit_threshold);
|
||||
}
|
||||
pthread_mutex_unlock(&count_lock);
|
||||
@ -45,17 +44,13 @@ void *inc_count(void *idp)
|
||||
return(NULL);
|
||||
}
|
||||
|
||||
void *watch_count(void *idp)
|
||||
void *watch_count(void *null)
|
||||
{
|
||||
int *my_id = idp;
|
||||
|
||||
printf("watch_count(): thread %d\n", *my_id);
|
||||
fflush(stdout);
|
||||
pthread_mutex_lock(&count_lock);
|
||||
|
||||
while (count < COUNT_THRES) {
|
||||
pthread_cond_wait(&count_hit_threshold, &count_lock);
|
||||
printf("watch_count(): thread %d, count %d\n", *my_id, count);
|
||||
condvar_was_hit = 1;
|
||||
}
|
||||
|
||||
pthread_mutex_unlock(&count_lock);
|
||||
@ -69,14 +64,26 @@ main(void)
|
||||
int i;
|
||||
pthread_t threads[3];
|
||||
|
||||
pthread_create(&threads[0], NULL, inc_count, (void *)&thread_ids[0]);
|
||||
pthread_create(&threads[1], NULL, inc_count, (void *)&thread_ids[1]);
|
||||
pthread_create(&threads[2], NULL, watch_count, (void *)&thread_ids[2]);
|
||||
pthread_create(&threads[0], NULL, watch_count, NULL);
|
||||
pthread_create(&threads[1], NULL, inc_count, NULL);
|
||||
pthread_create(&threads[2], NULL, inc_count, NULL);
|
||||
|
||||
for (i = 0; i < NUM_THREADS; i++) {
|
||||
pthread_join(threads[i], NULL);
|
||||
}
|
||||
|
||||
// Nb: it's not certain that we'll hit here. It's possible that the two
|
||||
// inc_count threads could fully run before watch_count begins, and so
|
||||
// pthread_cond_wait() is never called. Or, we could get a spurious
|
||||
// wake-up in watch_count(). Nonetheless, it's very likely that things
|
||||
// will work out as expected, since we're starting watch_count() first.
|
||||
if (condvar_was_hit == 1)
|
||||
printf("condvar was hit!\n");
|
||||
else if (condvar_was_hit > 1)
|
||||
printf("condvar was multi-hit...\n");
|
||||
else
|
||||
printf("condvar was missed...\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@ -1,22 +1,22 @@
|
||||
inc_counter(): thread 0, count = 1, unlocking mutex
|
||||
inc_counter(): thread 0, count = 2, unlocking mutex
|
||||
inc_counter(): thread 0, count = 3, unlocking mutex
|
||||
inc_counter(): thread 0, count = 4, unlocking mutex
|
||||
inc_counter(): thread 0, count = 5, unlocking mutex
|
||||
inc_counter(): thread 0, count = 6, unlocking mutex
|
||||
inc_counter(): thread 0, count = 7, unlocking mutex
|
||||
inc_counter(): thread 0, count = 8, unlocking mutex
|
||||
inc_counter(): thread 0, count = 9, unlocking mutex
|
||||
inc_counter(): thread 0, count = 10, unlocking mutex
|
||||
inc_counter(): thread 1, count = 11, unlocking mutex
|
||||
inc_counter(): thread 1, count = 12, unlocking mutex
|
||||
inc_count(): Thread 1, count 12
|
||||
inc_counter(): thread 1, count = 13, unlocking mutex
|
||||
inc_counter(): thread 1, count = 14, unlocking mutex
|
||||
inc_counter(): thread 1, count = 15, unlocking mutex
|
||||
inc_counter(): thread 1, count = 16, unlocking mutex
|
||||
inc_counter(): thread 1, count = 17, unlocking mutex
|
||||
inc_counter(): thread 1, count = 18, unlocking mutex
|
||||
inc_counter(): thread 1, count = 19, unlocking mutex
|
||||
inc_counter(): thread 1, count = 20, unlocking mutex
|
||||
watch_count(): thread 2
|
||||
inc_counter(): count = 1, unlocking mutex
|
||||
inc_counter(): count = 2, unlocking mutex
|
||||
inc_counter(): count = 3, unlocking mutex
|
||||
inc_counter(): count = 4, unlocking mutex
|
||||
inc_counter(): count = 5, unlocking mutex
|
||||
inc_counter(): count = 6, unlocking mutex
|
||||
inc_counter(): count = 7, unlocking mutex
|
||||
inc_counter(): count = 8, unlocking mutex
|
||||
inc_counter(): count = 9, unlocking mutex
|
||||
inc_counter(): count = 10, unlocking mutex
|
||||
inc_counter(): count = 11, unlocking mutex
|
||||
inc_counter(): count = 12, unlocking mutex
|
||||
hit threshold!
|
||||
inc_counter(): count = 13, unlocking mutex
|
||||
inc_counter(): count = 14, unlocking mutex
|
||||
inc_counter(): count = 15, unlocking mutex
|
||||
inc_counter(): count = 16, unlocking mutex
|
||||
inc_counter(): count = 17, unlocking mutex
|
||||
inc_counter(): count = 18, unlocking mutex
|
||||
inc_counter(): count = 19, unlocking mutex
|
||||
inc_counter(): count = 20, unlocking mutex
|
||||
condvar was hit!
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user