使用CAS实现无锁列队-链表

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <iostream>
#include <sys/time.h>
#include <pthread.h>
 
using namespace std;
 
#define MAXLEN 200000
 
#define NUM_THREADS 8
 
#define CAS __sync_bool_compare_and_swap
#define FAA __sync_fetch_and_add
#define FAS __sync_fetch_and_sub
#define VCAS __sync_val_compare_and_swap
 
struct node{
    int num;
    node * next;
    node(int i, node* n) :
        num(i), next(n)
    {}
};
 
 
struct list{
    node *head;
    node *tail;
    pthread_mutex_t  lock;
    int count;
};
 
void init(list *plist)
{
    node *tmp = new node(0, NULL);
    plist->head = tmp;
    plist->tail = tmp;
    plist->count = 0;
    pthread_mutex_init(&(plist->lock), NULL);
}
 
 
 
int enque_lock(list *plist, int num)
{
    node *tmp = new node(num, NULL);
    pthread_mutex_lock(&(plist->lock));
 
    plist->tail->next = tmp;
    plist->tail = tmp;
    plist->count++;
    pthread_mutex_unlock(&(plist->lock));
    return 0;
}
int enque(list *plist, int num)
{
    node *p = NULL;
    node *tmp = new node(num, NULL);
    do
    {
        p = plist->tail;
    } while (CAS(&(p->next), NULL, tmp) == false);
    CAS(&(plist->tail), p, tmp);
 
    FAA(&(plist->count), 1);
    return 0;
}
 
 
void  deque(list *plist)
{
    node *tmp = NULL;
    do{
        tmp = plist->head;
        if (plist->head == plist->tail)
        {
            printf("err\n");
            return;
        }
    } while (CAS(&(plist->head), tmp, tmp->next) == false);
    FAS(&(plist->count), 1);<br>    delete tmp;
    return;
}
 
void *SendMsg(void* p)
{
    struct timeval tv_begin, tv_end;
    gettimeofday(&tv_begin, NULL);
    int i = 0;
    while (i++ < MAXLEN)
    {
        enque((list*)p, i);
    }
    gettimeofday(&tv_end, NULL);
    long timeinterval = (tv_end.tv_sec - tv_begin.tv_sec) * 1000000 + (tv_end.tv_usec - tv_begin.tv_usec);
    printf("cost %llu us\n", timeinterval);
}
 
 
void *SendMsg2(void* p)
{
    struct timeval tv_begin, tv_end;
    gettimeofday(&tv_begin, NULL);
    int i = 0;
    while (i++ < MAXLEN)
    {
        enque_lock((list*)p, i);
    }
    gettimeofday(&tv_end, NULL);
    long timeinterval = (tv_end.tv_sec - tv_begin.tv_sec) * 1000000 + (tv_end.tv_usec - tv_begin.tv_usec);
    printf("cost %llu us\n", timeinterval);
}
 
 
 
int main(void)
{
    int rc, i;
    pthread_t thread[NUM_THREADS];
    list  ll;
    init(&ll);
    for (i = 0; i < NUM_THREADS; i++)
    {
        printf("Creating thread %i\n", i);
        rc = pthread_create(&thread[i], NULL, SendMsg, (void*)&ll);
        if (rc)
        {
            printf("ERROR; return code is %d\n", rc);
            return -1;
        }
    }
 
    for (i = 0; i < NUM_THREADS; i++)
    {
        pthread_join(thread[i], NULL);
    }
 
    while (ll.count > 0)
    {
        deque(&ll);
    }
 
 
    for (i = 0; i < NUM_THREADS; i++)
    {
        printf("Creating thread %i\n", i);
        rc = pthread_create(&thread[i], NULL, SendMsg2, (void*)&ll);
        if (rc)
        {
            printf("ERROR; return code is %d\n", rc);
            return -1;
        }
    }
 
    for (i = 0; i < NUM_THREADS; i++)
    {
        pthread_join(thread[i], NULL);
    }
    return 0;
}

  测试结果如下: 

Creating thread 0
Creating thread 1
Creating thread 2
Creating thread 3
Creating thread 4
Creating thread 5
Creating thread 6
Creating thread 7
cost 227839 us
cost 228055 us
cost 228034 us
cost 228179 us
cost 228274 us
cost 228328 us
cost 228413 us
cost 228433 us
Creating thread 0
Creating thread 1
Creating thread 2
Creating thread 3
Creating thread 4
Creating thread 5
Creating thread 6
Creating thread 7
cost 486283 us
cost 487499 us
cost 488358 us
cost 488677 us
cost 489118 us
cost 489658 us
cost 489657 us
cost 489735 us

加锁版 耗时 比无锁版耗时多一倍

posted @   myd620  阅读(961)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
点击右上角即可分享
微信分享提示