首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么Callgrind使原子加载永不结束

为什么Callgrind使原子加载永不结束
EN

Stack Overflow用户
提问于 2019-05-22 01:02:11
回答 1查看 71关注 0票数 0

我写了一个小程序,它工作得很好,直到它被Callgrind动态检测:

代码语言:javascript
复制
$ g++ -std=c++11 -pthread -g -ggdb -o program.exe program.cpp
$ time valgrind --tool=callgrind ./program.exe

代码:

代码语言:javascript
复制
#include <atomic>
#include <thread>
#include <iostream>

constexpr int CST_TARGET = 10*1000;

std::atomic<bool> g_lock = {false};
std::atomic<bool> g_got_work = {true};
int g_passer = 0;
long long g_total = 0;

void producer() {
    while (1) {
        while (g_lock.load(std::memory_order_seq_cst));
        if (g_passer >= CST_TARGET) {
            g_got_work.store(false, std::memory_order_seq_cst);
            return;
        }
        ++g_passer;
        g_lock.store(true, std::memory_order_seq_cst);
    }
}

void consumer() {
    while (g_got_work.load(std::memory_order_seq_cst)) {
        if (g_lock.load(std::memory_order_seq_cst)) {
            g_total += g_passer;
            g_lock.store(false, std::memory_order_seq_cst);
        }
    }
}

int main() {
    std::atomic<int> val(0);
    std::thread t1(producer);
    std::thread t2(consumer);
    t1.join();
    t2.join();
    std::cout << "g_passer = " << g_passer << std::endl;
    std::cout << "g_total = " << g_total << std::endl;
    return 0;
}

检测不会在10分钟后结束,所以我终止了它,并查看了KCachegrind统计数据。有数亿到数十亿次对std::atomic<bool>::load(...)的调用。

你知道Callgrind的哪些部分改变了原子调用的行为并使它们失败了吗?程序本身在没有Callgrind的情况下以毫秒的速度运行。

EN

回答 1

Stack Overflow用户

发布于 2019-05-22 05:30:51

使用--fair-sched=yes应该可以解决这个问题。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56243183

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档