66 result *= power->value;
67 free (arg);
68 }
69
70 /*
71 * Thread start routine that issues work queue requests.
72 */
73 void *thread_routine (void *arg)
74 {
75 power_t *element;
76 int count;
77 unsigned int seed = (unsigned int)time (NULL);
78 int status;
79
80 /*
81 * Loop, making requests.
82 */
83 for (count = 0; count < ITERATIONS; count++) {
84 element = (power_t*)malloc (sizeof (power_t));
85 if (element == NULL)
86 errno_abort ("Allocate element");
87 element->value = rand_r (&seed) % 20;
88 element->power = rand_r (&seed) % 7;
89 DPRINTF ((
90 "Request: %d^%d\n",
91 element->value, element->power));
92 status = workq_add (&workq, (void*)element);
93 if (status != 0)
94 err_abort (status, "Add to work queue");
95 sleep (rand_r (&seed) % 5);
96 }
97 return NULL;
98 } 99
100 int main (int argc, char *argv[])
101 {
102 pthread_t thread_id;
103 engine_t *engine;
104 int count = 0, calls = 0;
105 int status;
106
107 status = pthread_key_create (&engine_key, destructor);
108 if (status != 0)
109 err_abort (status, "Create key");
110 status = workq_init (&workq, 4, engine_routine);
111 if (status != 0)
112 err_abort (status, "Init work queue");
113 status = pthread_create (&thread_id, NULL, thread_routine, NULL);
114 if (status != 0)
115 err_abort (status, "Create thread");
116 (void)thread_routine (NULL);
117 status = pthread_join (thread_id, NULL);
118 if (status != 0)
119 err_abort (status, "Join thread");
120 status = workq_destroy (&workq);
121 if (status != 0)
122 err_abort (status, "Destroy work queue"); 123
124 /*
125 * By now, all of the engine_t structures have been placed
126 * on the list (by the engine thread destructors), so we
127 * can count and summarize them.
128 */
129 engine = engine_list_head;
130 while (engine != NULL) {
131 count++;
132 calls += engine->calls;
133 printf ("engine %d: %d calls\n", count, engine->calls);
134 engine = engine->link;
135 }
136 printf ("%d engine threads processed %d calls\n",
137 count, calls);
138 return 0;
139 }
7.3 But what about existing libraries?
"The great art of riding, as I was saying is—
to keep your balance properly. Like this, you know—" He let go the bridle, and stretched out both his arms to
show Alice what he meant, and this time he fell flat on
his back, right under the horse's feet.