c++ - 不同类型的输出 XCode 与 G++?
问题描述
以下代码在 XCode 中编译。但我想在没有编译器优化的情况下运行它,所以尝试使用g++ -std=c++14 falsesharing.cpp
.
好吧,我得到了两种不同的输出。XCode 的整数和 G++ 的随机字符。有人知道这是为什么吗?
只是为了确定:G ++没有编译器优化吗?但是 XCode 可以吗?
#include <iostream>
#include <thread>
#include <vector>
#include <stdio.h>
#include <memory>
#include "timer.h"
using namespace std;
const int64_t g_numLoops = 1<<27;
void f(uint8_t *pBuffer, int offset){
for(int64_t i=0; i<g_numLoops; i++){
pBuffer[offset] +=1;
}
}
int main(int argc, const char * argv[]) {
cout << "Start! \n";
vector<thread> multipleThreads(4);
uint8_t buffer[256];
int multiplier[] = {1,2,4,8,16,32,64,128,256};
unique_ptr<Timer[]> timers = make_unique<Timer[]>(256);
for(int i=0; i<multipleThreads.size(); i++){
int offset = multiplier[i] * i;
multipleThreads[i] = thread(f, buffer, offset);
}
for(auto &t: multipleThreads){
t.join();
}
int counter = 0;
for(auto i: buffer){
cout << "Buffer "<< counter << " : " << i << endl;
}
cout << "Stop! \n";
return 0;
}
解决方案
推荐阅读
- opencl - OpenCL 报告一半的预期计算单元
- cdn - 使用 JSDelivr 和 Github 在 AFrame 中托管 GLTF 模型时找不到 404 文件
- angular - 无法绑定到“ngForOf”,因为它不是“ion-item”的已知属性
- discord.py - 使用参数的 dicord py
- python - 导出 TensorFlow 概率的隐马尔可夫模型
- php - 使用 Laravel eloquent 更新或创建数据透视表条目时避免唯一约束异常
- spring-boot - 微服务究竟是如何实现的?
- android - 从服务器 API 错误输出自定义 Rx java 中的 onError() 消息
- java - Spring boot docker容器返回文件而不是jsp视图
- swift - XCode 12/Swift 4 自定义单元格未显示在 UI 表格单元格视图上