#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;
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);
}
void *produce(void *p)
{
long i;
while(1){
pthread_mutex_lock(&lock);
if(work == NULL){
work = malloc(sizeof(long)*S_LEN);
if(work != NULL){
for( i = 0; i < S_LEN ; i++){
work[i] = 1;
}
}
}
pthread_mutex_unlock(&lock);
}
}
void *consume(void *p)
{
long sum,i;
while(1){
pthread_mutex_lock(&lock);
sum = 0;
if(work != NULL){
for(i = 0; i < S_LEN ; i++){
sum += work[i];
}
free(work);
work = NULL;
}
pthread_mutex_unlock(&lock);
}
}
-
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/