54c Subj : Re: Reader Writer Puzzle To : comp.programming.threads From : bonnie.caballero Date : Wed Jun 15 2005 03:25 pm I have some small code here for the Reader and Delete tasks. But I realized that after the Reader increments p to p->next and unlocks the read lock, the Delete task might delete the node pointed to by (Reader's) p and hence, the Reader would fail when it tries to access p in its next loop iteration. I'm thinking of using reference counts to prevent this situation but it's not yet in the code. What do you think? TIA ---- // Note: H is the list head, is always not NULL and its data is not used. void Reader(void) { Node * p = NULL; Node pa; Boolean got1 = FALSE; ASSERT(H != NULL); while (1) { read_lock(lock); if (p == NULL) { p = H->next; } if (p != NULL) // Wrong: p might have been previously deleted. { memcpy(&pa, p, sizeof(Node)); got1 = TRUE; p = p->next; } else got1 = FALSE; read_unlock(lock); if (got1) Access(&pa); } } void Delete(Node * T) { Node * p = H; ASSERT(p != NULL); while (p->next != NULL) { if (p->next == T) { write_lock(lock); p->next = T->next; free(T); write_unlock(lock); break; } } } . 0