下面是我如何通过使用while循环进行简单检查来实现异步消息队列。我认为这比使用互斥锁更好,如果它工作正常。通过在我的机器上运行一些测试似乎可以正常工作,但我真的不确定这是否安全,因为我没有太多为多线程/进程编程异步系统的经验。我的工作是否安全地防止了竞争条件?或者它会因某些较重的载荷或任何其他条件而崩溃?
typedef struct MessageQueueElement {
Message message;
struct MessageQueueElement *next;
} MessageQueueElement;
typedef struct MessageQueue { //singly-linked list as a queue
MessageQueueElement *first;
MessageQueueElement *last;
bool sending;
} MessageQueue;
void createMessageQueue(MessageQueue *this) {
this->first = malloc(sizeof(MessageQueueElement));
this->last = this->first;
this->sending = false;
}
void sendMessage(MessageQueue *this, Message *message) {
while (this->sending);
//do nothing while this function is called from another thread
this->sending = true;
this->last->message = *message;
this->last = this->last->next = malloc(sizeof(MessageQueueElement));
//add a message to the queue
this->sending = false;
}
int waitMessage(MessageQueue *this, int (*readMessage)(unsigned, unsigned, void *)) {
while (this->first == this->last);
//do nothing while the queue is empty
int n = readMessage(this->first->message.type, this->first->message.code, this->first->message.data);
MessageQueueElement *temp = this->first;
this->first = this->first->next;
free(temp);
return n;
}
请参阅下文了解整个上下文和一些测试代码。
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <limits.h>
#include <math.h>
#include <pthread.h>
#define EXIT_MESSAGE 0
#define THREAD_MESSAGE 1
#define EXIT 0
#define CONTINUE 1
int readMessage(size_t type, size_t code, void *data) {
if (type == THREAD_MESSAGE) {
printf("message from thread %d: %s\n", code, (char *)data);
free(data);
} else {
return EXIT;
}
return CONTINUE;
}
MessageQueue mq;
int nThreads;
int counter = 0;
void *worker(void *p) {
double pi = 0.0;
for (int i = 0; i < 10; i += 1) {
for (int j = 0; j < 100000; j += 1) {
double n = i * 100000.0 + j;
pi += (4.0 / (8.0 * n + 1.0) - 2.0 / (8.0 * n + 4.0) - 1.0 / (8.0 * n + 5.0) - 1.0 / (8.0 * n + 6.0)) / pow(16.0, n);
}
char *s = malloc(100);
sprintf(s, "calculating pi... %d percent complete", (i + 1) * 10);
sendMessage(&mq, &(Message){.type = THREAD_MESSAGE, .code = (int)p, .data = s});
}
char *s = malloc(100);
sprintf(s, "pi equals %.8f", pi);
sendMessage(&mq, &(Message){.type = THREAD_MESSAGE, .code = (int)p, .data = s});
counter += 1;
if (counter == nThreads) {
sendMessage(&mq, &(Message){.type = EXIT_MESSAGE});
}
}
int main(int argc, char **argv) {
nThreads = atoi(argv[1]);
createMessageQueue(&mq);
pthread_t threads[nThreads];
for (int i = 0; i < nThreads; i += 1) {
pthread_create(&threads[i], NULL, worker, (void *)i);
}
while (waitMessage(&mq, readMessage));
for (int i = 0; i < nThreads; i += 1) {
pthread_join(threads[i], NULL);
}
return 0;
}
答案 0 :(得分:4)
显然不行。对于初学者来说,如果两个线程正在等待队列变空,那么他们很可能都会在同一时间发现发送== false,并且都会跳入,然后事情以错误的方式出错。那正是互斥体的用途。所以这不起作用。
忙于等待变量也是非常糟糕的形式。如果你有一个四核CPU,那么很有可能四个内核花费100%的可用CPU来等待变量的变化。不好。
由于发送不是易失性的,因此编译器不会看到生成代码将其设置为true的最轻微理由,所以再次发生这种情况并不起作用。
要实现这一点,您需要完成互斥锁所做的所有事情。而且你必须正确地完成完全的所有事情。这在很大程度上取决于您使用的确切处理器。您需要了解缓存一致性,读写操作的顺序,内存障碍等等,如果您还没有听说过这些,那么您就没有机会做到正确。