Multiple Vehicle Information Logging in VEINS

I am trying to log data from multiple vehicles into the same CSV file in VEINS, but it stops logging after the second vehicle spawns, i.e., it only works for a single-vehicle scenario.

void MyVeinsApp::handlePositionUpdate(cObject* obj) {
    DemoBaseApplLayer::handlePositionUpdate(obj);

    simtime_t currentTime = simTime();
    double offsetX = 0.0, offsetY = 0.0, offsetZ = 0.0;
    int isOffsetApplied = 0, isAttacker = attackerVehicles.count(vehicleId) ? 1 : 0;
    double currentSpeed = mobility->getSpeed();

    if (isAttacker && offsetDistribution(generator)) {
        offsetX = offsetValue(generator);
        offsetY = offsetValue(generator);
        offsetZ = offsetValue(generator);
        isOffsetApplied = 1;
        currentSpeed /= 2.0;
    }

    vehicleData[vehicleId] = std::make_tuple(curPosition.x + offsetX, curPosition.y + offsetY,
                                             curPosition.z + offsetZ, isOffsetApplied,
                                             isAttacker, currentSpeed);

    logFile << std::fixed << std::setprecision(2) << currentTime.dbl();
    for (int i = 0; i < 50; ++i) {
        if (vehicleData.count(i)) {
            // Vehicle data exists; log it
            auto tupleData = vehicleData[i];
            double x = std::get<0>(tupleData);
            double y = std::get<1>(tupleData);
            double z = std::get<2>(tupleData);
            int offset = std::get<3>(tupleData);
            int attacker = std::get<4>(tupleData);
            double speed = std::get<5>(tupleData);

            logFile << "," << x << "," << y << "," << z << "," << offset << "," << attacker << "," << speed;
        } else {
            // Vehicle data is missing; log blanks
            logFile << ",,,," << "0,0,";
        }
    }
    logFile << std::endl;
}

Could this be due to a race condition, where multiple instances of the application layer for each vehicle are trying to write to the CSV file simultaneously? If so, how can it be fixed?

Yes. This looks like a race condition where multiple vehicles are trying to write to the same file at the same time, which can lead to file corruption or incomplete writes. Below is a thread-safe solution using mutex to synchronise file access:

#include <mutex>

class MyVeinsApp : public DemoBaseApplLayer {
private:
    // Add mutex as class member
    static std::mutex logMutex;
    
    // Other existing class members...

public:
    void handlePositionUpdate(cObject* obj) {
        DemoBaseApplLayer::handlePositionUpdate(obj);

        simtime_t currentTime = simTime();
        double offsetX = 0.0, offsetY = 0.0, offsetZ = 0.0;
        int isOffsetApplied = 0, isAttacker = attackerVehicles.count(vehicleId) ? 1 : 0;
        double currentSpeed = mobility->getSpeed();

        if (isAttacker && offsetDistribution(generator)) {
            offsetX = offsetValue(generator);
            offsetY = offsetValue(generator);
            offsetZ = offsetValue(generator);
            isOffsetApplied = 1;
            currentSpeed /= 2.0;
        }

        // Update vehicle data
        {
            std::lock_guard<std::mutex> lock(logMutex);  // Scope-based lock for vehicle data
            vehicleData[vehicleId] = std::make_tuple(curPosition.x + offsetX, curPosition.y + offsetY,
                                                   curPosition.z + offsetZ, isOffsetApplied,
                                                   isAttacker, currentSpeed);
        }

        // Write to file with mutex protection
        {
            std::lock_guard<std::mutex> lock(logMutex);
            logFile << std::fixed << std::setprecision(2) << currentTime.dbl();
            
            for (int i = 0; i < 50; ++i) {
                if (vehicleData.count(i)) {
                    // Vehicle data exists; log it
                    auto tupleData = vehicleData[i];
                    double x = std::get<0>(tupleData);
                    double y = std::get<1>(tupleData);
                    double z = std::get<2>(tupleData);
                    int offset = std::get<3>(tupleData);
                    int attacker = std::get<4>(tupleData);
                    double speed = std::get<5>(tupleData);

                    logFile << "," << x << "," << y << "," << z << "," 
                           << offset << "," << attacker << "," << speed;
                } else {
                    // Vehicle data is missing; log blanks
                    logFile << ",,,," << "0,0,";
                }
            }
            logFile << std::endl;
            logFile.flush();  // Ensure data is written to disk
        }
    }
};

// Define the static mutex
std::mutex MyVeinsApp::logMutex;

The changes made above are:

  • Added a static mutex (logMutex) as a class member to synchronize access across all instances

  • Used std::lock_guard to RAII-style locking to ensure the mutex is always released

  • Protected both the vehicleData update and file writing operations

  • Added separate scoping blocks for the locks to minimise the critical section

  • Added logFile.flush() to ensure data is written to disk immediately

The following is also recommended:

Consider using a buffered approach where data is collected in memory and written periodically to reduce I/O overhead

You might want to add error handling for file operations

Consider using a more efficient data structure than checking 50 sequential IDs

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật