lkml.org 
[lkml]   [1999]   [Jul]   [2]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
Date
From
SubjectRe: threaded apps crash in 2.2.10 on Alpha
It wouldn't hurt to rewrite your app as follows with condition variables
instead of infinit running loops (this version is noticable faster if
timed!):

#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/

\
 
 \ /
  Last update: 2005-03-22 13:52    [W:0.069 / U:0.100 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site