为什么此变量的指定值会丢失?

时间:2016-01-15 03:14:02

标签: c++ multithreading variable-assignment

我希望有人可以看到我在哪里出错,因为它正在努力。

我有一些代码可以循环播放许多飞机资产:

std::string UpdateService::getUpdate(std::string clientId) {
    std::lock_guard<std::mutex> lock(m_mutex);
    std::cout << "Getting asset repository for UpdateService\n"; 
    AssetRepository* assetRepository =
        AssetRepositoryFactory::getInstance().getAssetRepository(clientId);
    // get an update only if the asset has moved since last time

    std::vector<Airplane*> airplanes = (*assetRepository).getAllAirplanes();
    std::cout << "UpdateService airplanes repository size = "
              << airplanes.size() << "\n";

    for (unsigned i = 0; i < airplanes.size(); i++) {
        std::cout << "UpdateService airplane is located at memory address 0x"
                  << std::hex << airplanes[i] << "\n";

        Airplane a = *(airplanes[i]);

        double x1 = a.getXSnapshot();
        double y1 = a.getYSnapshot();
        double z1 = a.getZSnapshot();
        std::string heading1 = a.getHeadingSnapshot();

        double x2 = a.updateXSnapshot();
        double y2 = a.updateYSnapshot();
        double z2 = a.updateZSnapshot();
        std::string heading2 = a.updateHeadingSnapshot();

        if (!approximatelyTheSame(x1,x2) ||
            !approximatelyTheSame(y1,y2) ||
            !approximatelyTheSame(z1,z2) ||
            heading1 != heading2 )
        {
            return "x: " + std::to_string(x2) +
                   " y: " + std::to_string(y2) + 
                   " z: " + std::to_string(z2) +
                   " heading: " + heading2;
        }

        return "";
    }

    return "";
}

它在Airplane上调用了几种方法:

double Airplane::getXSnapshot() {
    std::cout << "getXSnapshot()\n";
    std::cout << "m_x_snapshot is located at memory address 0x"
              << std::hex << &m_x_snapshot << "\n";
    std::cout << "m_x_snapshot is " << m_x_snapshot << "\n";

    return m_x_snapshot;
}

double Airplane::updateXSnapshot() {
    std::cout << "updateXSnapshot()\n";
    std::cout << "m_x_snapshot is " << m_x_snapshot << "\n";

    m_x_snapshot = (*m_airplane).getPosition()[0];

    std::cout << "m_x_snapshot is " << m_x_snapshot << "\n";

    return getXSnapshot();
}

分离线程每秒调用getUpdate()方法。

我遇到的问题是我为m_x_snapshotm_x_snapshot = (*m_airplane).getPosition()[0];)分配了一个值,但下次调用此方法时,该值已重置为0

以下是此代码的输出:

Getting asset repository for UpdateService
m_airplanes.size() 1
m_airplanes[i] is located at memory address 0x00000000028A8230
UpdateService airplanes repository size = 1
UpdateService airplane is located at memory address 0x00000000028A8230
getXSnapshot()
m_x_snapshot is located at memory address 0x000000000324F398
m_x_snapshot is 0
updateXSnapshot()
m_x_snapshot is 0
m_x_snapshot is -7035
getXSnapshot()
m_x_snapshot is located at memory address 0x000000000324F398
m_x_snapshot is -7035
Diff 7035
The update is: x: -7035.000000 y: 652.188603 z: 51.730339 heading: 0
Getting asset repository for UpdateService
m_airplanes.size() 1
m_airplanes[i] is located at memory address 0x00000000028A8230
UpdateService airplanes repository size = 1
UpdateService airplane is located at memory address 0x00000000028A8230
getXSnapshot()
m_x_snapshot is located at memory address 0x000000000324F398
m_x_snapshot is 0
updateXSnapshot()
m_x_snapshot is 0
m_x_snapshot is -7035
getXSnapshot()
m_x_snapshot is located at memory address 0x000000000324F398
m_x_snapshot is -7035
Diff 7035
The update is: x: -7035.000000 y: 652.189632 z: 51.730314 heading: 0

... etc

如您所见,m_x_snapshot每次调用方法时都会一直恢复为0。我不明白为什么,特别是因为它在同一个内存地址?

0 个答案:

没有答案