Last active
August 29, 2015 14:06
-
-
Save draganglumac/0c269fe07d6c317f7dee to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // If you have two functions that traverse the list in slightly | |
| // different manner | |
| // | |
| // First function: | |
| void traverse_1(list *A) { | |
| list *head = A->head; | |
| while (A->head != NULL) | |
| A->head = A->head->next; | |
| A->head = head; | |
| } | |
| // Second function | |
| void traverse_2(list *A) { | |
| list *head = A->head; | |
| while (head != NULL) | |
| head = head->next; | |
| } | |
| // If you call the first function from two different threads | |
| // then there is a chance that A->head will not actually point | |
| // to the head when called from second thread, because the list | |
| // head has already changed in the first thread. | |
| // | |
| // You don't have that problem with the second traversal function. | |
| // It is effectively thread safe for traversal (not thread safe for | |
| // modificaions of course, i.e. adding or removing nodes). | |
| // | |
| // Also the second one is slightly more efficient as there is one | |
| // less statement to execute, namely resetting A->head |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment