Skip to content

Instantly share code, notes, and snippets.

@itsff
Created October 3, 2013 16:23
Show Gist options
  • Save itsff/6812653 to your computer and use it in GitHub Desktop.
Save itsff/6812653 to your computer and use it in GitHub Desktop.
struct my_element
{
int my_element_data;
/* Placement of container-specific node data can be easily modified for locality
optimization */
struct container__slist_node slist_node;
/* A single element can be linked into multiple containers */
struct container__hash_node hash_node;
};
void
InsertElement (...)
{
struct my_element* el;
/* Allocate elements using any mechanism desired */
el = malloc(sizeof(struct my_element));
el->my_element_data = 42;
/* Perform locking only during the insertion, allowing potentially costly allocations and
initializations to occur outside the lock */
pthread_mutex_lock(&my_slist_lock);
Container_AddSListHead(&el->slist_node, &my_slist);
pthread_mutex_unlock(&my_slist_lock);
/* Easily allow external libraries to operate on the container */
ExternalLibUpdate(&el->hash_node, &my_hash);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment