当前位置: 首页 > 面试题库 >

互斥锁线程

微生高谊
2023-03-14
问题内容

是多线程/进程编程的新手。所以这是我需要澄清的。

处理代码

pthread_mutex_lock()
    pthread_create(fooAPI(sharedResource)) //fooAPI creates another thread with shared resource that shares across processes.
pthread_mutex_unlock()

使用上述伪代码,sharedResource如果互斥锁未解锁,进程B是否可以访问?

如何从进程B正确访问sharedResource?

有没有清晰的可视化图表说明互斥体,线程和进程之间的关系?


问题答案:

您需要做的是调用pthread_mutex_lock来保护互斥锁,如下所示:

pthread_mutex_lock(&mutex);

一旦执行此操作,pthread_mutex_lock(mutex)在您pthread_mutex_unlock在该线程中进行调用之前,不会再进行任何其他调用。因此,如果尝试调用pthread_create,则将能够创建一个新线程,并且该线程将能够(不正确地)使用共享资源。您应该pthread_mutex_lockfooAPI函数内部进行调用,这将导致函数等待,直到共享资源可用为止。

所以你会有这样的事情:

#include <pthread.h>
#include <stdio.h>

int sharedResource = 0;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

void* fooAPI(void* param)
{
    pthread_mutex_lock(&mutex);
    printf("Changing the shared resource now.\n");
    sharedResource = 42;
    pthread_mutex_unlock(&mutex);
    return 0;
}

int main()
{
    pthread_t thread;

    // Really not locking for any reason other than to make the point.
    pthread_mutex_lock(&mutex);
    pthread_create(&thread, NULL, fooAPI, NULL);
    sleep(1);
    pthread_mutex_unlock(&mutex);

    // Now we need to lock to use the shared resource.
    pthread_mutex_lock(&mutex);
    printf("%d\n", sharedResource);
    pthread_mutex_unlock(&mutex);
}

编辑:跨进程使用资源遵循相同的基本方法,但是您需要将内存映射到其他进程。这是使用shmem的示例:

#include <stdio.h>
#include <unistd.h>
#include <sys/file.h>
#include <sys/mman.h>
#include <sys/wait.h>

struct shared {
    pthread_mutex_t mutex;
    int sharedResource;
};

int main()
{
    int fd = shm_open("/foo", O_CREAT | O_TRUNC | O_RDWR, 0600);
    ftruncate(fd, sizeof(struct shared));

    struct shared *p = (struct shared*)mmap(0, sizeof(struct shared),
        PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);

    p->sharedResource = 0;

    // Make sure it can be shared across processes
    pthread_mutexattr_t shared;
    pthread_mutexattr_init(&shared);
    pthread_mutexattr_setpshared(&shared, PTHREAD_PROCESS_SHARED);

    pthread_mutex_init(&(p->mutex), &shared);

    int i;
    for (i = 0; i < 100; i++) {
        pthread_mutex_lock(&(p->mutex));
        printf("%d\n", p->sharedResource);
        pthread_mutex_unlock(&(p->mutex));
        sleep(1);
    }

    munmap(p, sizeof(struct shared*));
    shm_unlink("/foo");
}

编写程序以更改p-> sharedResource留给读者练习。:-)

顺便说一句,忘了指出互斥对象必须设置PTHREAD_PROCESS_SHARED属性,这样pthread才能跨进程工作。



 类似资料:
  • 问题内容: 帮助客户解决他们遇到的问题。我更多地是sysadmin / DBA的人,所以我在努力帮助他们。他们说这是内核/环境中的错误,在我坚持要在他们的代码中或寻求供应商对OS的支持之前,我试图证明或证明这一点。 发生在Red Hat和Oracle Enterprise Linux 5.7(和5.8)上,应用程序用C ++编写 他们遇到的问题是主线程启动一个单独的线程来执行可能长时间运行的TCP

  • 类型 pthread_mutex_t 互斥锁基本操作 函数 描述 [[pthread_mutex_init pthread_mutex_init]] 初始化互斥锁 [[pthread_mutex_lock pthread_mutex_lock]] 阻塞申请互斥锁 [[pthread_mutex_unlock pthread_mutex_unlock]] 释放互斥锁 [[pthread_mutex_

  • Go语言包中的 sync 包提供了两种锁类型:sync.Mutex 和 sync.RWMutex。 Mutex 是最简单的一种锁类型,同时也比较暴力,当一个 goroutine 获得了 Mutex 后,其他 goroutine 就只能乖乖等到这个 goroutine 释放该 Mutex。 RWMutex 相对友好些,是经典的单写多读模型。在读锁占用的情况下,会阻止写,但不阻止读,也就是多个 gor

  • Introduction This is the fourth part of the chapter which describes synchronization primitives in the Linux kernel and in the previous parts we finished to consider different types spinlocks and semap

  • 本文向大家介绍Python Threading 线程/互斥锁/死锁/GIL锁,包括了Python Threading 线程/互斥锁/死锁/GIL锁的使用技巧和注意事项,需要的朋友参考一下 导入线程包 import threading 准备函数线程,传参数 t1 = threading.Thread(target=func,args=(args,)) 类继承线程,创建线程对象 线程共享全面变量,但在

  • 本文向大家介绍互斥锁死锁,包括了互斥锁死锁的使用技巧和注意事项,需要的朋友参考一下 死锁可以在使用互斥锁的多线程Pthread程序中发生。让我们看看它如何发生。未锁定的互斥锁由pthread_mutex_init()函数初始化。 使用pthread_mutex_lock()和pthread_mutex_unlock()获取并释放互斥锁。如果线程尝试获取锁定的互斥锁,则对pthread_mutex_