I'm having problems with putting items in, and getting them out, of a single linked list in a multithreaded program.
What happens is, that my program crashes when i try to write to the linked list very fast with more than one thread at the same time and in the same time i try to read from the list.
Here's what i do to read : (i get the first item in the list)
Item = ListPtr;
ListPtr = Item->Next;
and to write to the list :
NewItem = malloc ...
NewItem->Next = NULL;
for (ItemPtr = ListPtr; ItemPtr->Next; ItemPtr = ItemPtr->Next);
ItemPtr->Next = NewItem;
else ListPtr = NewItem;
My guess is : while doing those pointermanipulations, another thread is changing them so the result is an incorrect list.
-> Is there a way to tell the compiler that two or more functions must be preformed after each other, whitout excecuting code from other thread at the same time??
or is there another sollution for my problem?