8

我之前在 Python 中使用过多线程库,但这是我第一次尝试在 C 中使用线程。我想创建工作池。反过来,这些工作人员应该推送到队列或从队列中弹出。下面的代码还没有,但是我到目前为止所做的就是:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define NUMTHREADS 20 /* number of threads to create */

typedef struct node node;
typedef struct queue queue;

struct node {
    char *name;
    node *next;
};

struct queue {
    node *head;
    node *tail;
};

/* pop: remove and return first name from a queue */
char *pop(queue *q)
{
    if (q->head == NULL)
        return NULL;
    char *name = q->head->name;
    node *tmp = q->head;
    q->head = q->head->next;
    free(tmp);
    return name;
}

/* push: add name to the end of the queue */
int push(queue *q, char *name)
{
    node *new = malloc(sizeof(node));
    if (new == NULL)
        return -1;
    new->name = name;
    new->next = NULL;
    if (q->tail != NULL)
        q->tail->next = new;

    q->tail = new;
    if (q->head == NULL) /* first value */
        q->head = new;
    return 0;
}

/* printname: get a name from the queue, and print it. */
void *printname(void *sharedQ)
{
    queue *q = (queue *) sharedQ;
    char *name = pop(q);
    if (name == NULL)
        pthread_exit(NULL);
    printf("%s\n",name);
    pthread_exit(NULL);
}

int main()
{
    size_t i;
    int rc;
    pthread_t threads[NUMTHREADS];
    char *names[] = {
        "yasar",
        "arabaci",
        "osman",
        "ahmet",
        "mehmet",
        "zeliha"
    };

    queue *q = malloc(sizeof(queue));
    q->head = NULL;
    q->tail = NULL;

    /* number of elements in the array */
    size_t numelems = sizeof(names) / sizeof(char *);

    for (i = 0; i < numelems; i++) /* push each name */
        push(q, names[i]);

    for (i = 0; i < NUMTHREADS; i++) { /* fire up threads */
        rc = pthread_create(&threads[i], NULL, printname,
                (void *)q);
        if (rc) {
            printf("Error, return code from pthread is %d\n", rc);
            exit(-1);
        }
    }

    pthread_exit(NULL);
}

我尝试了上面的代码,它总是将每个名称打印一次。它没有跳过任何名称,或两次打印相同的名称。另一方面,我不确定这个队列实现的线程安全性如何。所以我的问题是,这是一个线程安全队列吗?如果不是,为什么不呢?以及如何使其线程安全?

4

2 回答 2

7

代码不是线程安全的。

push 和 pop 函数不是线程安全的。在代码中,push 只是由单个线程执行的,所以没关系,但是 pop 是由多个线程执行的。

1. char *name = q->head->name;
2. node *tmp = q->head;
3. q->head = q->head->next;
4. free(tmp);

假设线程 A 执行到第 2 行(包括第 2 行)。然后线程 B 执行到第 4 行(包括第 4 行)。线程 A 恢复执行。它发现 q->head 已经被 free()ed 了。

现在,到目前为止,这讨论了逻辑问题。

但是,有一些物理问题需要考虑。

想象一下,我们有一个锁定机制,线程可以同步它们的行为,这样一次只有一个线程可以执行第 1 到第 4 行中的代码,例如互斥锁,它是一个对象,一次只有一个线程可以“持有” ,以及尝试获取互斥锁的位置会阻塞线程,直到持有线程释放。

0. get mutex
1. char *name = q->head->name;
2. node *tmp = q->head;
3. q->head = q->head->next;
4. free(tmp);
5. release mutex

我们仍然会遇到一个问题,因为任何给定 CPU 内核(不是线程)执行的写入仅对该内核上的线程立即可见;不要在其他内核上使用线程。

仅仅同步执行是不够的;同时,我们还必须确保一个核心执行的写入对其他核心可见。

(不)幸运的是,所有现代同步方法也执行此写入刷新(例如,当您获得互斥体时,您还将所有写入刷新到内存)。我说不幸的是,因为您并不总是需要这种行为,而且它对性能有害。

于 2012-05-23T13:58:39.867 回答
3

它不是线程安全的,因为多个线程可能同时修改链表中的指针,从而可能破坏它。

在这里你有一个非常相似的问题的答案: Multiple-writer thread-safe queue in C

在那里你可以看到如何使队列线程安全。

于 2012-05-23T14:00:38.697 回答