并发控制 线程间并发控制机制接口(线程互斥锁) 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;
}

输出:

相关推荐
爱编程— 的小李1 分钟前
有序序列合并(c语言)
c语言·算法
梦想科研社2 分钟前
【无人机设计与控制】红嘴蓝鹊优化器RBMO求解无人机路径规划MATLAB
开发语言·matlab·无人机
混迹网络的权某5 分钟前
每天一道C语言精选编程题之求数字的每⼀位之和
c语言·开发语言·考研·算法·改行学it·1024程序员节
lida20038 分钟前
无人机救援系统简单解读
linux·无人机·openipc·wfb-ng
一只特立独行的猪6111 小时前
Java面试题——微服务篇
java·开发语言·微服务
TensorFlowGAN3 小时前
rhcsa、ce复习(每日编辑,复习完为止)
linux·es
喵手3 小时前
Java 与 Oracle 数据泵实操:数据导入导出的全方位指南
java·开发语言·oracle
硬汉嵌入式4 小时前
H7-TOOL的LUA小程序教程第16期:脉冲测量,4路PWM,多路GPIO和波形打印(2024-10-25, 更新完毕)
开发语言·junit·小程序·lua
Drscq4 小时前
How to migrate a CentOS 8 to Rocky Linux 8.10
linux·运维·centos
修心光4 小时前
CentOS配置iptables规则并使其永久生效
linux·运维·centos