|
From: <sv...@va...> - 2007-09-12 00:22:58
|
Author: sewardj
Date: 2007-09-12 01:22:57 +0100 (Wed, 12 Sep 2007)
New Revision: 6822
Log:
Try to fix this so it doesn't hang when run on very slow machines.
Modified:
branches/THRCHECK/thrcheck/tests/tc08_hbl2.c
Modified: branches/THRCHECK/thrcheck/tests/tc08_hbl2.c
===================================================================
--- branches/THRCHECK/thrcheck/tests/tc08_hbl2.c 2007-09-11 15:07:20 UTC (rev 6821)
+++ branches/THRCHECK/thrcheck/tests/tc08_hbl2.c 2007-09-12 00:22:57 UTC (rev 6822)
@@ -2,7 +2,7 @@
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
-#include <sched.h>
+#include <unistd.h>
/* Simple test program, no race. Parent writes atomically to a counter
whilst child reads it. When counter reaches a prearranged value,
@@ -55,19 +55,25 @@
#define LIMIT 10
-int x = 0;
+volatile int x = 0;
void* child_fn ( void* arg )
{
int q = 0;
int oldx = 0;
+ int ctr = 0;
while (1) {
- q = x == LIMIT;
+ q = (x >= LIMIT);
if (x != oldx) {
oldx = x;
printf("child: new value %d\n", oldx);
+ fflush(stdout);
}
if (q) break;
+ /* Make sure the parent doesn't starve. Seems to be a problem
+ on very slow machines. */
+ ctr++;
+ if (ctr == 2000000) sleep(1);
}
return NULL;
}
@@ -84,9 +90,7 @@
for (i = 0; i < LIMIT; i++) {
INC(x);
- /* Not really necessary, but just gives the child a chance
- to run too. */
- sched_yield();
+ if (i == 5) sleep(1); /* make sure child doesn't starve */
}
if (pthread_join(child, NULL)) {
@@ -94,5 +98,7 @@
exit(1);
}
+ printf("done, x = %d\n", x);
+
return 0;
}
|