Re: [gtk-list] Re: Threads with GTK



here is an example, what i do in one of my programs (not gtk related, but
might help) :
i have one worker thread and one "get data" thread. 
to pass data, i have a struct containing head and tail of a linked list,
an int size (for speed), a mutex "lock" and a pthread condition "hasone"
(all commands etc. from memory, so check the manpages before useing this).

put(struct queue* q, struct job *p) {
	lock(&q->lock);
	if (q->tail) 
		q->tail->next = q->tail = p;
	else 
		q->head = q->tail = p;
	p->next = NULL
	q->len++;
	signal(&q->hasone);
	unlock(&q->lock);
}

struct job* get(struct queue* q) {
	struct job *p;

	lock(&q->lock);
	while (q->len == 0)
		// wait for someone to send a "hasone" signal
		pthread_cond_wait(&q->hasone, &q->lock);
	p = q->head;
	q->head = p->next;
	if (! q->head)
		q->tail = NULL;
	p->next = NULL
	unlock(&q->lock);
	return p;
}

you coult add one int "size", and a condition "notmax", if you want
to limit the stuff to <size> elements.

got the basic idea ?

andreas



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]