小编Tim*_*ell的帖子

C/C++编译器是否可以通过pthread库调用合法地在寄存器中缓存变量?

假设我们有以下代码:

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>

void guarantee(bool cond, const char *msg) {
    if (!cond) {
        fprintf(stderr, "%s", msg);
        exit(1);
    }
}

bool do_shutdown = false;   // Not volatile!
pthread_cond_t shutdown_cond = PTHREAD_COND_INITIALIZER;
pthread_mutex_t shutdown_cond_mutex = PTHREAD_MUTEX_INITIALIZER;

/* Called in Thread 1. Intended behavior is to block until
trigger_shutdown() is called. */
void wait_for_shutdown_signal() {

    int res;

    res = pthread_mutex_lock(&shutdown_cond_mutex);
    guarantee(res == 0, "Could not lock shutdown cond mutex");

    while (!do_shutdown) {   // while loop guards against spurious wakeups …
Run Code Online (Sandbox Code Playgroud)

c c++ concurrency standards sequence-points

9
推荐指数
1
解决办法
1282
查看次数

标签 统计

c ×1

c++ ×1

concurrency ×1

sequence-points ×1

standards ×1