1 Star 1 Fork 0

jdhxyy / nowaitlock

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

nowaitlock

介绍

C语言实现的基于Lamport面包店算法的无等待互斥锁。可用于没有提供锁的系统,比如无RTOS的单片机中。

开源

API

// NWLCreate 创建锁
// total: 使用此锁的最大用户数量
// 返回锁的句柄.如果是0表示创建失败
intptr_t NWLCreate(int total);

// NWLDelete 删除锁
void NWLDelete(intptr_t handle);

// NWLLock 上锁.如果返回false表示上锁失败.上锁成功后必须要解锁
bool NWLLock(intptr_t handle, int id);

// NWLUnlock 解锁.必须上锁成功后才能解锁
void NWLUnlock(intptr_t handle, int id);

测试用例

windows下创建若干线程,按顺序打印数据。

#include <stdio.h>
#include <winsock2.h>
#include <process.h>
#include <windows.h>
#include <time.h>

#include "nowaitlock.h"

#pragma comment(lib,"ws2_32.lib")

#define MAX_THREAD_NUM 5

// 锁及保护的资源
static intptr_t lock;
// 顺序控制字
static int seq = 0;

static void case1(void);

static void case2(void);
static DWORD WINAPI thread(LPVOID lpParam);

int main() {
    case2();
    return 0;
}

static void case1(void) {
    intptr_t lock = NWLCreate(3);

    // 本轮应该0获取锁成功
    printf("---------->0\n");
    printf("0:%d\n", NWLLock(lock, 0));
    printf("1:%d\n", NWLLock(lock, 1));
    printf("2:%d\n", NWLLock(lock, 2));
    NWLUnlock(lock, 0);

    // 本轮应该0获取锁成功
    printf("---------->0\n");
    printf("0:%d\n", NWLLock(lock, 0));
    printf("1:%d\n", NWLLock(lock, 1));
    printf("2:%d\n", NWLLock(lock, 2));
    NWLUnlock(lock, 0);

    // 本轮应该1获取锁成功
    printf("---------->1\n");
    printf("1:%d\n", NWLLock(lock, 1));
    printf("0:%d\n", NWLLock(lock, 0));
    printf("2:%d\n", NWLLock(lock, 2));
    NWLUnlock(lock, 1);

    // 本轮应该2获取锁成功
    printf("---------->2\n");
    printf("2:%d\n", NWLLock(lock, 2));
    printf("0:%d\n", NWLLock(lock, 0));
    printf("1:%d\n", NWLLock(lock, 1));
    NWLUnlock(lock, 2);

    NWLDelete(lock);
}

static void case2(void) {
    lock = NWLCreate(MAX_THREAD_NUM);

    int a[MAX_THREAD_NUM] = {0};
    for (int i = 0; i < MAX_THREAD_NUM; i++) {
        a[i] = i;
        CreateThread(NULL, 0, thread, &a[i], 0, NULL);
    }
    while(1) {}
}

static DWORD WINAPI thread(LPVOID lpParam) {
    int id = *(int*)lpParam;

    printf("id:%d thread run\n", id);
    Sleep(1000);

    while (1) {
        if (NWLLock(lock, id) == false) {
            Sleep(0);
            continue;
        }

        printf("id:%d start\n", id);
        if (seq % MAX_THREAD_NUM == id) {
            seq++;
            printf("----->id:%d seq:%d\n", id, seq);
        }
        if (seq >= 20) {
            printf("id:%d exit\n", id);
            NWLUnlock(lock, id);
            break;
        }
        printf("id:%d stop\n", id);

        NWLUnlock(lock, id);
        Sleep(0);
    }
    return 0;
}

输出:

id:0 thread run
id:1 thread run
id:2 thread run
id:3 thread run
id:4 thread run
id:4 start
id:4 stop
id:0 start
----->id:0 seq:1
id:0 stop
id:2 start
id:2 stop
id:0 start
id:0 stop
id:1 start
----->id:1 seq:2
id:1 stop
id:2 start
----->id:2 seq:3
id:2 stop
id:1 start
id:1 stop
id:3 start
----->id:3 seq:4
id:3 stop
id:2 start
id:2 stop
id:4 start
----->id:4 seq:5
id:4 stop
id:0 start
----->id:0 seq:6
id:0 stop
id:1 start
----->id:1 seq:7
id:1 stop
id:2 start
----->id:2 seq:8
id:2 stop
id:0 start
id:0 stop
id:3 start
----->id:3 seq:9
id:3 stop
id:2 start
id:2 stop
id:3 start
id:3 stop
id:1 start
id:1 stop
id:4 start
----->id:4 seq:10
id:4 stop
id:0 start
----->id:0 seq:11
id:0 stop
id:1 start
----->id:1 seq:12
id:1 stop
id:2 start
----->id:2 seq:13
id:2 stop
id:1 start
id:1 stop
id:2 start
id:2 stop
id:1 start
id:1 stop
id:2 start
id:2 stop
id:4 start
id:4 stop
id:0 start
id:0 stop
id:4 start
id:4 stop
id:2 start
id:2 stop
id:3 start
----->id:3 seq:14
id:3 stop
id:4 start
----->id:4 seq:15
id:4 stop
id:2 start
id:2 stop
id:2 start
id:2 stop
id:4 start
id:4 stop
id:1 start
id:1 stop
id:1 start
id:1 stop
id:2 start
id:2 stop
id:3 start
id:3 stop
id:2 start
id:2 stop
id:3 start
id:3 stop
id:1 start
id:1 stop
id:3 start
id:3 stop
id:3 start
id:3 stop
id:3 start
id:3 stop
id:4 start
id:4 stop
id:1 start
id:1 stop
id:2 start
id:2 stop
id:2 start
id:2 stop
id:4 start
id:4 stop
id:0 start
----->id:0 seq:16
id:0 stop
id:2 start
id:2 stop
id:3 start
id:3 stop
id:0 start
id:0 stop
id:1 start
----->id:1 seq:17
id:1 stop
id:0 start
id:0 stop
id:3 start
id:3 stop
id:2 start
----->id:2 seq:18
id:2 stop
id:0 start
id:0 stop
id:3 start
----->id:3 seq:19
id:3 stop
id:4 start
----->id:4 seq:20
id:4 exit
id:2 start
id:2 exit
id:1 start
id:1 exit
id:0 start
----->id:0 seq:21
id:0 exit
id:3 start
id:3 exit
MIT License Copyright (c) 2021 jdhxyy Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

C语言实现的基于Lamport面包店算法的无等待互斥锁 展开 收起
C 等 2 种语言
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/jdhxyy/nowaitlock.git
git@gitee.com:jdhxyy/nowaitlock.git
jdhxyy
nowaitlock
nowaitlock
master

搜索帮助