-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.c
48 lines (39 loc) · 1.11 KB
/
test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <stdio.h>
#include <pthread.h>
#include "scspq.h"
#define NUM_MESSAGES 10
typedef struct Message {
alignas(CACHE_LINE_SIZE) int data;
} Message;
SPSCQueue queue;
void* producer_thread(void* arg) {
for (int i = 0; i < NUM_MESSAGES; ++i) {
Message msg = {.data = i};
while (spsc_queue_push(&queue, &msg, sizeof(Message)) == -1) {
// Queue is full, wait and retry
sched_yield();
}
printf("Produced message: %d\n", msg.data);
}
return NULL;
}
void* consumer_thread(void* arg) {
for (int i = 0; i < NUM_MESSAGES; ++i) {
Message msg;
while (spsc_queue_pop(&queue, &msg, sizeof(Message)) == -1) {
// Queue is empty, wait and retry
sched_yield();
}
printf("Consumed message: %d\n", msg.data);
}
return NULL;
}
int main() {
spsc_queue_init(&queue);
pthread_t producer, consumer;
pthread_create(&producer, NULL, producer_thread, NULL);
pthread_create(&consumer, NULL, consumer_thread, NULL);
pthread_join(producer, NULL);
pthread_join(consumer, NULL);
return 0;
}