• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include "test.h"
2 #include <sys/timeb.h>
3 
4 static pthread_cond_t cv;
5 static pthread_mutex_t mutex;
6 static int shared = 0;
7 
8 enum {
9   NUMTHREADS = 2         /* Including the primary thread. */
10 };
11 
12 void *
mythread(void * arg)13 mythread(void * arg)
14 {
15   int result = 0;
16 
17   assert(pthread_mutex_lock(&mutex) == 0);
18   shared++;
19   assert(pthread_mutex_unlock(&mutex) == 0);
20 
21   if ((result = pthread_cond_signal(&cv)) != 0)
22     {
23       printf("Error = %s\n", error_string[result]);
24     }
25   assert(result == 0);
26 
27 
28   return (void *) 0;
29 }
30 
31 int
main()32 main()
33 {
34   pthread_t t[NUMTHREADS];
35   struct timespec abstime = { 0, 0 };
36   struct _timeb currSysTime;
37   const DWORD NANOSEC_PER_MILLISEC = 1000000;
38 
39   assert((t[0] = pthread_self()) != 0);
40   assert(pthread_gethandle (t[0]) != NULL);
41 
42   assert(pthread_cond_init(&cv, NULL) == 0);
43 
44   assert(pthread_mutex_init(&mutex, NULL) == 0);
45 
46   assert(pthread_mutex_lock(&mutex) == 0);
47 
48   /* get current system time */
49   _ftime(&currSysTime);
50 
51   abstime.tv_sec = currSysTime.time;
52   abstime.tv_nsec = NANOSEC_PER_MILLISEC * currSysTime.millitm;
53 
54   assert(pthread_create(&t[1], NULL, mythread, (void *) 1) == 0);
55 
56   abstime.tv_sec += 5;
57 
58   while (! (shared > 0))
59     assert(pthread_cond_timedwait(&cv, &mutex, &abstime) == 0);
60 
61   assert(shared > 0);
62 
63   assert(pthread_mutex_unlock(&mutex) == 0);
64 
65   assert(pthread_join(t[1], NULL) == 0);
66 
67   assert(pthread_cond_destroy(&cv) == 0);
68 
69   return 0;
70 }
71