English 中文(简体)
如何执行线索安全队列
原标题:How to implement thread safe queues

我以前在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
",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
", rc);
            exit(-1);
        }
    }

    pthread_exit(NULL);
}

我尝试了上面的代码, 它总是打印每个名字一次 。 它没有跳过任何名字, 或者打印过两次相同的名字 。 另一方面, 我不确定这个队列执行的线条安全程度 。 因此我的问题是, 这是一个安全队列吗? 如果不是, 为什么不? 如何让它安全?

最佳回答

代码不安全 。

推线和弹出函数不是安全的线条。在代码中,推线只由一条线执行,所以无关紧要,但弹出函数是由多个线条执行的。

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

图像线 A 执行到 并包含 第 2 线 。 线索 B 然后执行到 并包含 第 4 线 。 它发现 q- gt;head 已经免费 。

到目前为止,这里讨论的是逻辑问题。

然而,有一些实际问题需要考虑。

想象一下我们有一个锁定机制, 线索可以同步他们的行为, 这样在时间上只有一条线 能够执行代码的线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核心(不是线条)的写作都可立即看到,只有该核心上的线条,而不是其他核心上的线条。

仅仅使处决同步化是不够的;与此同时,我们还必须确保由核心完成的写作能为其他核心所看到。

不幸地, 所有现代的时序化方法都进行写式冲洗( 例如, 当您得到哑巴时, 您也会将全部冲刷到记忆中 ) 。 我说很不幸, 因为您- 总是需要这种行为, 并且对表现有害 。

问题回答

由于多个线索可能会同时修改链接列表中的指针, 从而有可能损坏它, 因此它不是安全的线条 。

Here you have an answer for a very similar question: Multiple-writer thread-safe queue in C

您可以在那里看到如何使队列安全。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签