@@ -40,38 +40,46 @@ static volatile int thr2_count = 0;
40
40
41
41
static void * thr1_run (opal_object_t * obj )
42
42
{
43
- int i ;
44
43
clock_t c1 , c2 ;
45
- opal_mutex_lock (& mutex );
46
44
c1 = clock ();
47
- for (i = 0 ; i < TEST_COUNT ; i ++ ) {
48
- opal_condition_wait (& thr1_cond , & mutex );
49
- opal_condition_signal (& thr2_cond );
45
+ opal_mutex_lock (& mutex );
46
+ while (TEST_COUNT != thr1_count ) {
50
47
thr1_count ++ ;
48
+ opal_condition_signal (& thr2_cond );
49
+ opal_condition_wait (& thr1_cond , & mutex );
51
50
}
52
- c2 = clock ();
51
+
52
+ // Whoever gets here first needs to alert the other
53
+ // thread for their last iteration.
54
+ opal_condition_signal (& thr2_cond );
53
55
opal_mutex_unlock (& mutex );
54
- fprintf (stderr , "thr1: time per iteration: %ld usec\n" , (long ) ((c2 - c1 ) / TEST_COUNT ));
56
+ c2 = clock ();
57
+ fprintf (stderr , "thr1: time per iteration: %ld uses\n" , (long )((c2 - c1 ) / TEST_COUNT ));
55
58
return NULL ;
56
59
}
57
60
58
61
static void * thr2_run (opal_object_t * obj )
59
62
{
60
- int i ;
61
63
clock_t c1 , c2 ;
62
- opal_mutex_lock (& mutex );
63
64
c1 = clock ();
64
- for (i = 0 ; i < TEST_COUNT ; i ++ ) {
65
+ opal_mutex_lock (& mutex );
66
+ while (TEST_COUNT != thr2_count ) {
67
+ thr2_count ++ ;
65
68
opal_condition_signal (& thr1_cond );
66
69
opal_condition_wait (& thr2_cond , & mutex );
67
- thr2_count ++ ;
68
70
}
69
- c2 = clock ();
71
+
72
+ // Whoever gets here first needs to alert the other
73
+ // thread for the last iteration.
74
+ opal_condition_signal (& thr1_cond );
70
75
opal_mutex_unlock (& mutex );
71
- fprintf (stderr , "thr2: time per iteration: %ld usec\n" , (long ) ((c2 - c1 ) / TEST_COUNT ));
76
+
77
+ c2 = clock ();
78
+ fprintf (stderr , "thr2: time per iteration: %ld usec\n" , (long )((c2 - c1 ) / TEST_COUNT ));
72
79
return NULL ;
73
80
}
74
81
82
+
75
83
int main (int argc , char * * argv )
76
84
{
77
85
int rc ;
0 commit comments