Creating a lock-free task queue in C is more challenging than in C++ due to the lack of built-in atomic types and operations. However, you can use GCC's built-in atomic functions or the C11 <stdatomic.h> header to achieve similar functionality. Here's an example of a lock-free task queue in C using GCC's built-in atomic functions:
#include <stdlib.h>
#include <stdatomic.h>
#include <stdio.h>
typedef struct Node {
int data;
struct Node* next;
} Node;
typedef struct {
_Atomic(Node*) head;
_Atomic(Node*) tail;
} LockFreeTaskQueue;
void push(LockFreeTaskQueue* queue, int data) {
Node* newNode = malloc(sizeof(Node));
newNode->data = data;
newNode->next = NULL;
Node* oldTail;
do {
oldTail = atomic_load(&queue->tail);
newNode->next = oldTail;
} while (!atomic_compare_exchange_weak(&queue->tail, &oldTail, newNode));
atomic_store(&queue->head, newNode);
}
int pop(LockFreeTaskQueue* queue) {
Node* oldHead;
do {
oldHead = atomic_load(&queue->head);
if (oldHead == NULL) {
return -1; // Queue is empty
}
} while (!atomic_compare_exchange_weak(&queue->head, &oldHead, oldHead->next));
int data = oldHead->data;
free(oldHead);
return data;
}
int main() {
LockFreeTaskQueue queue = {.head = NULL, .tail = NULL};
// Producer thread
for (int i = 0; i < 10; ++i) {
push(&queue, i);
}
// Consumer thread
int data;
while ((data = pop(&queue)) != -1) {
printf("Consumed: %d\n", data);
}
return 0;
}
Explanation:
-
Node Structure:
- Holds the task data (
data) and a pointer to the next node (next).
-
push() Method:
- Creates a new node with the given data.
- Uses a
do-while loop with atomic_compare_exchange_weak to atomically update the tail pointer.
- If successful, the new node becomes the new tail, and its
next pointer is set to the old tail.
-
pop() Method:
- Uses a
do-while loop with atomic_compare_exchange_weak to atomically update the head pointer.
- If successful, it retrieves the data from the old head node, deletes the old head, and returns the data.
- If the
head pointer has changed, the loop continues.
Key Points:
- Lock-Free: This implementation avoids using mutexes or other locking mechanisms, making it suitable for concurrent access by multiple threads.
- Atomic Operations: It relies on GCC's built-in atomic functions (
atomic_load, atomic_compare_exchange_weak, atomic_store) to ensure thread safety.
- Memory Management: The queue dynamically allocates memory for nodes. Remember to handle memory deallocation appropriately.
- Memory Ordering: The use of
atomic_compare_exchange_weak ensures proper memory ordering between threads.
Usage Example:
int main() {
LockFreeTaskQueue queue = {.head = NULL, .tail = NULL};
// Producer thread
for (int i = 0; i < 10; ++i) {
push(&queue, i);
}
// Consumer thread
int data;
while ((data = pop(&queue)) != -1) {
printf("Consumed: %d\n", data);
}
return 0;
}
Important Considerations:
- Performance: Lock-free algorithms can be complex and may not always be the most performant solution. Benchmark and profile your code to determine if a lock-free approach is suitable for your specific use case.
- Memory Leaks: Ensure proper memory management to avoid memory leaks. Consider using a memory pool to simplify memory allocation and deallocation.
- Error Handling: This example doesn't include error handling. You may want to add checks for allocation failures or other potential errors.