Back to index

courier  0.68.2
test.c
Go to the documentation of this file.
00001 /*
00002 ** Copyright 2000 Double Precision, Inc.  See COPYING for
00003 ** distribution information.
00004 */
00005 
00006 
00007 #include      "config.h"
00008 #include      <stdio.h>
00009 #include      <stdlib.h>
00010 #include      <math.h>
00011 #include      <errno.h>
00012 #include      <time.h>
00013 #include      "threadlib.h"
00014 
00015 static double buf[100];
00016 static int bufcnt=0;
00017 static unsigned ndelay=0;
00018 
00019 static void workfunc(void *p)
00020 {
00021 double *dp= (double *)p;
00022 time_t t1, t2;
00023 
00024        *dp=sqrt( *dp );
00025 
00026        time(&t1);
00027        while ( time(&t2) < t1 + ndelay)
00028               ;
00029 }
00030 
00031 static void cleanupfunc(void *p)
00032 {
00033 double *dp= (double *)p;
00034 
00035        buf[bufcnt++]= *dp;
00036 }
00037 
00038 static void getwork(void *p, void *q)
00039 {
00040 double *dp= (double *)p;
00041 double *qp= (double *)q;
00042 
00043        *dp= *qp;
00044 }
00045 
00046 static int cmpdbl (const void *a, const void *b)
00047 {
00048 const double *pa=(const double *)a;
00049 const double *pb=(const double *)b;
00050 
00051        return (*pa < *pb ? -1: *pa > *pb ? 1:0);
00052 }
00053 
00054 int main(int argc, char **argv)
00055 {
00056 double n;
00057 struct cthreadinfo *cit;
00058 int    i, j;
00059 
00060        if (argc < 2) return (1);
00061 
00062        j=atoi(argv[1]);
00063        if (argc > 2)
00064               ndelay=atoi(argv[2]);
00065 
00066        if (j <= 0 || j > sizeof(buf)/sizeof(buf[0]))    return (1);
00067 
00068        if ( (cit=cthread_init(2, sizeof(double), workfunc, cleanupfunc)) == 0)
00069        {
00070               perror("cthread_init");
00071               return (1);
00072        }
00073 
00074        for (i=0; i<j; i++)
00075        {
00076               n= i+1;
00077 
00078               if (cthread_go(cit, getwork, &n))
00079               {
00080                      perror("cthread_go");
00081                      return (1);
00082               }
00083               printf ("Started %d\n", i+1);
00084        }
00085        cthread_wait(cit);
00086 
00087        qsort(buf, bufcnt, sizeof(buf[0]), &cmpdbl);
00088 
00089        for (i=0; i<bufcnt; i++)
00090        {
00091               printf("%6.2f\n", buf[i]);
00092        }
00093        return (0);
00094 }