From 346e0db637f3b0be52e1b7782cffa5ee0f2b09e8 Mon Sep 17 00:00:00 2001 From: rpj Date: Mon, 22 Feb 1999 16:51:52 +0000 Subject: 1999-02-23 Ross Johnson * Template.c: Revamp. * condvar1.c: Add. * condvar2.c: Add. * Makefile: Add condvar1 condvar2 tests. * exit1.c, exit2.c, exit3.c: Cosmetic changes. --- tests/Template.c | 79 ++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 66 insertions(+), 13 deletions(-) (limited to 'tests/Template.c') diff --git a/tests/Template.c b/tests/Template.c index 50a81bf..470003d 100644 --- a/tests/Template.c +++ b/tests/Template.c @@ -38,31 +38,84 @@ * - */ -#include -#include +#include "test.h" -pthread_key_t key; -pthread_once_t key_once = PTHREAD_ONCE_INIT; +/* + * Create NUMTHREADS threads in addition to the Main thread. + */ +static enum { + NUMTHREADS = 2 +}; + +typedef struct bag_t_ bag_t; +struct bag_t_ { + int threadnum; + int washere; + /* Add more pre-thread state variables here */ +}; + +static bag_t threadbag[NUMTHREADS]; void * mythread(void * arg) { + bag_t * bag = (bag_t *) arg; + + assert(bag == &threadbag[bag->threadnum]); + assert(bag->washere == 0); + bag->washere = 1; + + /* ... */ + return 0; } int main() { - int rc; - pthread_t t1, t2; - - rc = pthread_create(&t1, NULL, mythread, (void *) 1); - printf("pthread_create returned %d\n", rc); + int failed = 0; + pthread_t t[NUMTHREADS + 1]; + + assert((t[0] = pthread_self()) != NULL); + + for (i = 1; i <= NUMTHREADS; i++) + { + threadbag[i].washere = 0; + threadbag[i].threadnum = i; + assert(pthread_create(&t[i], NULL, mythread, (void *) &threadbag[i]) == 0); + } + + /* + * Give threads time to run. + */ + Sleep(NUMTHREADS * 1000); + + /* + * Standard check that all threads started. + */ + for (i = 1; i <= NUMTHREADS; i++) + { + if (threadbag[i].washere != 1) + { + failed = 1; + fprintf(stderr, "Thread %d: washere %d\n", i, threadbag[i].washere); + } + } + + assert(failed == 0); + + /* + * Check any results here. Only print ouput and set "failed" on failure. + */ + for (i = 1; i <= NUMTHREADS; i++) + { + /* ... */ + } - rc = pthread_create(&t2, NULL, mythread, (void *) 2); - printf("pthread_create returned %d\n", rc); + assert(failed == 0); - /* Give threads time to run. */ - Sleep(2000); + /* + * Success. + */ return 0; } -- cgit v1.2.3