简体   繁体   English

在C中的两个函数上使用pthreads

[英]Using pthreads on two functions in C

I'm completely new to pthreads and there seems to be a lot of unnecessary info on all the site I've looked at. 我对pthread完全陌生,在我浏览过的所有网站上似乎都有很多不必要的信息。

I have two functions let's just call them X and Y for now, these both work on blocks in memory. 我有两个函数,现在暂时将它们称为X和Y,它们都在内存中的块上工作。 If one thread is running XI don't want any other threads to be calling X or Y on the same block, how can I make sure this will never happen? 如果一个线程正在运行XI,而又不希望其他任何线程在同一块上调用X或Y,那么如何确保这种情况永远不会发生呢?

Do I need to mutex lock the functions for certain block values? 我是否需要互斥锁定某些块值的功能?

You will need to use mutexes. 您将需要使用互斥锁。

You should not lock code, you should lock data. 您不应锁定代码,而应锁定数据。 Create a mutex for each block, and lock it while a function is operating on the block, then unlock it when finished. 为每个块创建一个互斥锁,并在对该块执行功能时将其锁定,然后在完成时将其解锁。

A mutex is a type defined by pthread.h, pthread_mutex_t. 互斥锁是由pthread.h pthread_mutex_t定义的类型。 Functions are provided to lock and unlock the mutex. 提供了锁定和解锁互斥锁的功能。 These functions ensure that only one thread can gain a lock at a time (if you just used a variable to indicate that your block is being used, you will have concurrency issues with that variable instead of the block). 这些函数确保一次仅一个线程可以获得一个锁(如果您只是使用一个变量来指示正在使用您的块,则该变量而不是块将具有并发性问题)。

There are numerous tutorials available online. 在线上有许多教程。 Google "pthread tutorial" and you should find enough to get yourself started. Google的“ pthread教程”,您应该找到足够的入门知识。

You lock the resource - in this case a memory block - with a mutex. 使用互斥锁锁定资源(在本例中为内存块)。 Alternatively you can lock just the parts of your functions code that read/update that area of memory. 或者,您可以仅锁定功能代码中读取/更新该内存区域的部分。 That is called a critical section, and requires a different approcch to coding. 这被称为关键部分,并且需要不同的编码方式。 It means your threads are free to operate except when they hit that part where they interact with the resource. 这意味着您的线程可以自由操作,除非它们到达与资源交互的那一部分。

The first method is easier to implement - just an all or nothing approach for the whole function X or Y. 第一种方法更易于实现-整个函数X或Y只需采用全有或全无的方法。

Perhaps some demonstration code is in order. 也许一些演示代码是有序的。 Presuming you have a block header like so: 假设您有一个像这样的块头:

struct block {
    void *data;
    size_t len;
};

You would protect the block by adding a mutex variable to this structure: 您可以通过向此结构添加互斥变量来保护该块:

struct block {
    void *data;
    size_t len;
    pthread_mutex_t lock;
};

You then need to update the initialisation function for this structure to initialise the lock: 然后,您需要为此结构更新初始化函数以初始化锁:

struct block *new_block(size_t len)
{
    struct block *b = malloc(sizeof *b);
    b->data = malloc(len);
    b->len = len;

    pthread_mutex_init(&b->lock, NULL);

    return b;
}

The X and Y functions (and any other function that reads or writes to the block) then need to take the lock and release it at exit: 然后,X和Y函数(以及任何其他读取或写入该块的函数)需要获取该锁并在出口处释放它:

int x(struct block *b)
{
    int retval;

    pthread_mutex_lock(&b->lock);

    /* code */

    pthread_mutex_unlock(&b->lock);
    return retval;
}

int y(struct block *b)
{
    int retval;

    pthread_mutex_lock(&b->lock);

    /* code */

    pthread_mutex_unlock(&b->lock);
    return retval;
}

You need to be careful to ensure that you unlock the mutex even in error return paths, too. 您还需要注意即使在错误返回路径中也要解锁互斥锁。

Multi-threaded programming is really better resolved using in higher level languages. 使用高级语言确实可以更好地解决多线程编程问题。 Some things are difficult to understand in C, and, in my opinion, mutli-threading is one of them. 有些事情用C语言很难理解,我认为,多线程是其中之一。 I've found Java gave me a better feel for the issues and problems. 我发现Java使我对这些问题有了更好的了解。 It has easier to understand concepts and easier to read documentation. 它使概念更容易理解,文档也更容易阅读。 A C++ framework such as Poco or Qt would also be better if Java is not your thing. 如果您不喜欢Java,那么Poco或Qt之类的C ++框架也会更好。

As others have said, conceptually you want to lock resources (in your case a section of memory). 正如其他人所说,从概念上讲,您想锁定资源(在您的情况下为一部分内存)。 Semaphores, as a concept, fit this problem much better than mutex does. 作为一个概念,信号量比互斥量更适合解决此问题。 I would research semaphores and just think of mutexes as a building block of semaphores. 我会研究信号量,而只是将互斥锁视为信号量的构建块。 If you ask me, a mutex is a poorly named binary semaphore. 如果您问我,互斥锁是一个名称不正确的二进制信号量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM