Tôi đã sử dụng thư viện đa luồng trước trong Python, nhưng đây là lần đầu tiên tôi thử luồng trong C. Tôi muốn tạo nhóm công nhân. Đổi lại, những người lao động phải đẩy tới hoặc bật từ queue.Following mã không khá có được nêu ra, nhưng là những gì tôi đã làm cho đến nay:Làm thế nào để thực hiện hàng đợi an toàn thread
#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);
}
tôi đã cố gắng trên mã, và nó luôn luôn được in mỗi tên chính xác Một lần. Nó không bỏ qua bất kỳ tên nào, hoặc in cùng một tên hai lần. Mặt khác, tôi không chắc chắn cách luồng an toàn thực hiện hàng đợi này. Vì vậy, câu hỏi của tôi là, Đây có phải là một hàng đợi threadsafe? Nếu không, tai sao không? Và làm thế nào để làm cho nó thread an toàn?
Cấu trúc không cần typedef; họ đã có một loại rồi. –