*** Welcome to piglix ***

Sentinel node


In computer programming, a sentinel node is a specifically designated node used with linked lists and trees as a traversal path terminator. This type of node does not hold or reference any data managed by the data structure.

Sentinels are used as an alternative over using null as the path terminator in order to get one or more of the following benefits:

However, sentinel nodes rely on shared memory, which requires extra code to avoid data races. This causes sentinel nodes to have poor performance on concurrent systems.

Below are two versions of a subroutine (implemented in the C programming language) for looking up a given search key in a singly linked list. The first one uses the sentinel value NULL, and the second one a (pointer to the) sentinel node Sentinel, as the end-of-list indicator. The declarations of the singly linked list data structure and the outcomes of both subroutines are the same.

The for-loop contains two tests (yellow lines) per iteration:

The globally available pointer sentinel to the deliberately prepared data structure Sentinel is used as end-of-list indicator.

The for-loop contains only one test (yellow line) per iteration:

If the data structure is accessed concurrently then for a sentinel-based implementation, not only the list pointed to by first has to be protected by mutex, but also the Sentinel node has to be protected too; this extra mutex in quite a few use scenarios can cause severe performance degradation . One way to avoid it is to have List structure having both Node* first, and Sentinel node (this way Sentinel won't need to be shared more than original list itself).



...
Wikipedia

...