尝试我的运气来实现无锁单链表。
typedef _Atomic struct _node
{
void *data;
struct _node *next;
} Node;
这是否也使 _Atomic 结构的所有成员都具有原子性?
void add_head ( Linked_list* list, void* data )
{
if ( debugging )
{
printf ( "%s\n", __func__ );
}
Node *node = ( Node* ) calloc ( 1, sizeof (Node ) );
//init_node_mutex(node);
//lock_node_mutex(node);
atomic_exchange ( &node->next, NULL );
atomic_exchange ( &node->data, data );
if ( list->head == NULL )
{
Node* the_tail = atomic_load ( &list->tail );
// lock_node_mutex ( the_tail );
atomic_exchange ( &node->next, NULL );
atomic_compare_exchange_weak ( &list->tail, the_tail, node );
//unlock_node_mutex ( the_tail );
}
else
{
Node* the_next = atomic_load ( &node->next );
// lock_node_mutex ( the_next );
atomic_compare_exchange_weak ( &node->next, the_next, list->head );
// unlock_node_mutex ( the_next );
}
Node* the_head = atomic_load ( & list->head );
//lock_node_mutex ( the_head );
atomic_store ( &list->head, node );
atomic_store ( &list->current, node );
//unlock_node_mutex ( the_head );
//unlock_node_mutex(node);
atomic_fetch_add ( &list->size, 1 );
}
atomic_load 和 atomic_store 的用法是否正确?