并发控制 线程间并发控制机制接口(线程互斥锁) Linux环境 C语言实现

线程互斥锁

只能解决单个共享资源的互斥问题

cpp 复制代码
头文件:#include <pthread.h>
类型:pthread_mutex_t
PTHREAD_MUTEX_INITIALIZER 初始化
初始化:int pthread_mutex_init(pthread_mutex_t * mutex, NULL);
清理:int pthread_mutex_destroy(pthread_mutex_t * mutex);
阻塞P操作: int pthread_mutex_lock(pthread_mutex_t * mutex);
非阻塞P操作: int pthread_mutex_trylock(pthread_mutex_t * mutex);
			检查锁状态,无论是已上锁还是未上锁均立即返回,只是返回的值不同,不会阻塞,循环调用该函数可以实现轮询等待
V操作: int pthread_mutex_unlock(pthread_mutex_t * mutex);

功能:见函数名
返回值:成功0,失败错误码

**问题:**主线程动态创建一个对象,该对象将可能被用于多个分离的新线程,要求确保每个线程用该对象时该对象必须存在,大家都不用该对象时才被销毁


算法:


代码:

myobject.h

cpp 复制代码
#ifndef _MYOBJECT_H
#define _MYOBJECT_H

#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>

typedef int DT;

struct myobject{
    DT data;
    int refcnt;
    pthread_mutex_t lock;
};

struct myobject *create_myobject(); // create

int add_object_refcnt(struct myobject *pobj); // add

int destroy_myobject(struct myobject *pobj); // destroy

#endif

myobject.c

cpp 复制代码
#include "myobject.h"

struct myobject *create_myobject(){
    struct myobject *pobj = (struct myobject *)malloc(sizeof(struct myobject));
    if(pobj == NULL){
        printf("malloc for a myobject failed\n");
        return NULL;
    }
    memset(pobj,0,sizeof(struct myobject)); // 初始化

    pthread_mutex_init(&(pobj->lock), NULL); // init

    return pobj;
}

int add_object_refcnt(struct myobject *pobj){
    int cnt = 0;

    pthread_mutex_lock(&pobj->lock); // lock

    cnt = ++pobj->refcnt; // cnt++
    
    pthread_mutex_unlock(&pobj->lock); // unlock

    return cnt;
}

int destroy_myobject(struct myobject *pobj){
    pthread_mutex_lock(&pobj->lock); // lock

    pobj->refcnt--; // cnt--

    if(pobj->refcnt == 0){
        pthread_mutex_destroy(&pobj->lock); // pthread_mutex_destroy()
        free(pobj);
        pobj = NULL;
        printf("Destroy success\n");
        return 0;
    }

    pthread_mutex_unlock(&pobj->lock); // unlock

    return 0;
}

main.c

cpp 复制代码
#include "myobject.h"

void *pthread_start1(void *parg); // thread1
void *pthread_start2(void *parg); // thread2

int main(){
    pthread_t id[2]; // id
    int ret = 0;
    struct myobject *pobj;

    pobj = create_myobject(); // create
    add_object_refcnt(pobj); // main cnt +1

    add_object_refcnt(pobj); // thread1 cnt +1
    ret = pthread_create(&id[0],NULL,pthread_start1,(void*)pobj); // thread1

    add_object_refcnt(pobj); // thread2 cnt +1
    ret += pthread_create(&id[1],NULL,pthread_start2,(void*)pobj); // thread2

    if(ret){
        printf("pthread_create failed\n");
        return 1;
    }
    pobj->data = 99;
    printf("main thread:%d\n",pobj->data);
    printf("cnt:%d\n",pobj->refcnt); // print

    destroy_myobject(pobj); // destroy

    pthread_exit(NULL); // main exit

    return 0;
}

void *pthread_start1(void* parg){ // thread1
    int ret = 0;

    ret = pthread_detach(pthread_self()); // 线程分离
    struct myobject *pobj;
    pobj = (struct myobject*)parg;

    pobj->data = 111;
    printf("thread1:%d\n",pobj->data); // print
    printf("cnt:%d\n",pobj->refcnt); // print

    ret += destroy_myobject(pobj); // destroy
    if(ret){
        printf("destroy_myobject failed\n");
        return NULL;
    }

    return NULL;
}

void *pthread_start2(void* parg){ // thread2
    int ret = 0;

    ret = pthread_detach(pthread_self()); // 线程分离

    struct myobject *pobj = (struct myobject *)parg;

    pobj->data = 222;
    printf("thread2:%d\n",pobj->data); // print
    printf("cnt:%d\n",pobj->refcnt); // print

    ret += destroy_myobject(pobj); // destroy
    if(ret){
        printf("destroy_myobject failed\n");
        return NULL;
    }

    return NULL;
}

输出:

相关推荐
LONGZETECH4 分钟前
新能源汽车充电设备装配与调试仿真教学软件 技术解析与教学落地
开发语言·系统架构·汽车·汽车教学软件·智能网联汽车软件
User_芊芊君子9 分钟前
2026最新Python+AI入门指南:从零基础到实战落地,避开90%新手坑
开发语言·人工智能·python
篮l球场26 分钟前
Trie(字典树/前缀树)
开发语言·c#
似水明俊德32 分钟前
15-C#
android·开发语言·c#
九皇叔叔44 分钟前
CentOS 7.5/RHEL 7.x 配置 YUM 源(阿里云镜像+本地源双方案)
linux·阿里云·centos
hua872221 小时前
Golang 构建学习
java·开发语言·学习
2301_803554521 小时前
qt信号槽机制以及底层实现原理
开发语言·qt
大傻^1 小时前
LangChain4j RAG 核心:Document、Embedding 与向量存储抽象
开发语言·人工智能·python·embedding·langchain4j
笨笨马甲1 小时前
Qt 音视频编解码
开发语言·qt
Halo_tjn1 小时前
Java 三个修饰符 相关知识点
java·开发语言