#include <pthread.h>
#include <malloc.h>
#include <stdio.h>
#define S_LEN 20480
void *consume(void *);
void *produce(void *);
long *work = NULL;
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t full = PTHREAD_COND_INITIALIZER;
pthread_cond_t empty = PTHREAD_COND_INITIALIZER;
int main(void)
{
pthread_t thr1, thr2;
pthread_create(&thr2, NULL, produce, NULL);
pthread_create(&thr1, NULL, consume, NULL);
pthread_join(thr1, NULL);
pthread_join(thr2, NULL);
return 1;
}
void *produce(void *p)
{
long i;
pthread_mutex_lock(&lock);
while (1) {
while (work) {
pthread_cond_wait(&empty, &lock);
}
work = malloc(sizeof(long) * S_LEN);
if (work != NULL) {
for (i = 0; i < S_LEN; i++) {
work[i] = 1;
}
}
pthread_cond_signal(&full);
}
pthread_mutex_unlock(&lock);
}
void *consume(void *p)
{
long sum, i;
pthread_mutex_lock(&lock);
while (1) {
while (work == NULL) {
pthread_cond_wait(&full, &lock);
}
sum = 0;
for (i = 0; i < S_LEN; i++) {
sum += work[i];
}
free(work);
work = NULL;
pthread_cond_signal(&empty);
}
pthread_mutex_unlock(&lock);
}
-- If you think education is expensive, try ignorance. -- Derek Bok, president of Harvard[dirk.nuyens@pandora.be] ---[dirkn@sicom.nl]--- -[dot@sin.khk.be]-
- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.rutgers.edu Please read the FAQ at http://www.tux.org/lkml/