下面是如何使用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;
}发布于 2015-04-09 12:54:26
显然不好。首先,如果两个线程正在等待队列变为空,它们很可能都会发现在完全相同的时间发送==错误,并且都会跳入其中,然后就会出现错误。这正是互斥物存在的目的。所以这不管用。
忙于等待变量也是非常糟糕的形式。如果您有一个四核CPU,那么四个核心很有可能花费100%的可用CPU来等待变量的改变。不太好。
而且,由于发送不是易失性的,您的编译器将看不到生成代码以将其设置为true的最小理由,因此这同样无法工作。
要做到这一点,你需要做所有的事情,一个互斥。你必须把一切都做好。这在很大程度上取决于您所使用的确切处理器。您需要注意缓存的一致性、读写操作的顺序、内存障碍等等,如果您还没有听说过这些,那么您就没有机会正确地完成这些操作。
https://stackoverflow.com/questions/29538984
复制相似问题