#include #include #include #define MAX_THREADS 10 #define queue_max 10000 void *queue[queue_max]; int counter = 0; pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER; void *thr (void *in) { while (1) { void *f = NULL; pthread_mutex_lock (&m); if (!counter) { pthread_mutex_unlock (&m); continue; } f = queue[--counter]; pthread_mutex_unlock (&m); memset (f, 0, 1024); free (f); } } int main() { pthread_t t[MAX_THREADS]; int i; for (i = 0; i < MAX_THREADS; i++) pthread_create (&t[i], NULL, thr, NULL); for (i = 0; i < queue_max; i++) { pthread_mutex_lock (&m); queue [i] = malloc (1024); counter++; pthread_mutex_unlock (&m); } for (i = 0; i < MAX_THREADS; i++) pthread_join (t[i], NULL); }