cancel
Showing results for 
Search instead for 
Did you mean: 

Archives Discussions

mihzaha
Journeyman III

using atom_inc to append to the end of an array

Hi there 

I have an array and some threads that want to insert something to the end of the array. The head of the array is a global int variable. How should I do that?

I was thinking something like this:

atom_inc(&head);
array[head]=new_item;

but I heard the head is now head+number of threads that want to append (first all the atom_incs from all threads finish then the head is read), not +1 to the first thread, than +2 to the second as I would have liked. (put the threads that want to inc in a list, take the first thread, inc, than the thread reads the head than the second comes and increases once more...)

Not all threads add something to the list. How could I do that?

Thank you 

0 Likes
2 Replies
genaganna
Journeyman III

Originally posted by: mihzaha Hi there  I have an array and some threads that want to insert something to the end of the array. The head of the array is a global int variable. How should I do that? I was thinking something like this: atom_inc(&head); array[head]=new_item; but I heard the head is now head+number of threads that want to append (first all the atom_incs from all threads finish then the head is read), not +1 to the first thread, than +2 to the second as I would have liked. (put the threads that want to inc in a list, take the first thread, inc, than the thread reads the head than the second comes and increases once more...) Not all threads add something to the list. How could I do that? Thank you 

 



 

Following should work for you. oldValue or oldValue + 1 may be your end of the array for that thread based on your initial value of head.

 

oldValue = atom_inc(&head);

array[oldValue or oldValue + 1] = new_item.

 

 

0 Likes

thank you I'm glad it's something simple

0 Likes