简体   繁体   中英

Writing file from different threads in C++

I am writing a program that writes to a binary file from different threads. Each thread will be writing into the different position of the file. I use no synchronization and my program just works. I would like to ask you if I should use some synchronization and how or if its sufficient to hope that OS synchronization will do this somehow anyway. I am on Linux, gcc compiler but at some point it might be run on other platforms as well. The following function I use to write to the file from different threads.

void writeBytesFrom(std::string fileName, uint64_t fromPosition, uint8_t* buffer, int numBytes)
{
    if(CHAR_BIT != 8)
    {
        std::stringstream errMsg;
        errMsg << "Can not use this platform since CHAR_BIT size is not 8, namely it is "
               << CHAR_BIT << ".";
        LOGE << errMsg.str();
        throw std::runtime_error(errMsg.str());
    }
    std::ofstream file(fileName,
                       std::ios::binary | std::ios::out
                           | std::ios::in); // Open binary, for output, for input
    if(!file.is_open()) // cannot open file
    {
        std::stringstream errMsg;
        errMsg << "Can not open file " << fileName << ".";
        LOGE << errMsg.str();
        throw std::runtime_error(errMsg.str());
    }
    file.seekp(fromPosition); // put pointer
    std::streampos cur = file.tellp();
    file.write((char*)buffer, numBytes);
    auto pos = file.tellp();
    auto num = pos - cur;
    file.close();
    if(num != numBytes)
    {
        std::stringstream errMsg;
        errMsg << num << " bytes written from number of bytess that should be written "
               << numBytes << " to " << fileName << ".";
        LOGE << errMsg.str();
        throw std::runtime_error(errMsg.str());
    }
}

If you have any further suggestions to improve my code, I am open to that. The reason I am using uint8_t buffer is that its more natural to me to understand that the buffer represents 8bit bytes than if I would have used ie unsigned char. I know some purists might argue against that.

Thanks, Vojta.

If you have any further suggestions to improve my code, I am open to that.

Not everyone will agree with every comment or code style recommendation, but these work for me:

#include <string>
#include <climits>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <vector>

extern std::ostream& LOGE;


// See below for the reasons I write this
struct unwrapper {

    void print_exception(std::ostream&os, const std::exception& e, int level =  0) const
    {
        os << level << " : " << e.what() << '\n';
        try {
            std::rethrow_if_nested(e);
        } catch(const std::exception& e) {
            print_exception(os, e, level+1);
        } catch(...) {
            os << level << " : nonstandard exception";
        }
    }    

    std::exception const& initial_exception;

    friend std::ostream& operator<<(std::ostream& os, unwrapper const& u)
    {
        u.print_exception(os, u.initial_exception);
        return os;
    }
};

unwrapper unwrap(std::exception const& e)
{
    return unwrapper { e };
}

// Comment #3 : separate concerns - opening the outfile is a separate job for which
//              we will want a discrete error. So wrap that logic into a function
// Comment #4 : separate exception handling from code
// Comment #5 : nested exceptions are great for providing a forensic trail
//              which helps us to solve runtime errors
// Comment #2 : an ofstream has no business being open for input 
//              std::ios::out is assumed
// Comment #6 : since we're using exceptions, let the stream object raise them for us
std::ofstream openOutput(std::string const& fileName)
try
{

    std::ofstream file(fileName, std::ios::binary);
    file.exceptions(std::ios::badbit | std::ios::failbit);    
    return file;
}
catch(std::exception&)
{
    auto message = [&]() {
        std::ostringstream ss;
        ss << "openOutput: fileName = " << std::quoted(fileName);
        return std::move(ss).str();
    } ();
    LOGE << message;
    std::throw_with_nested(std::runtime_error(message));
}

void writeBytesFrom(std::string fileName, uint64_t fromPosition, uint8_t* buffer, int numBytes)
try  // function try blocks separate exception handling from logic
{
    // Comment #1 : prefer non-compilation over runtime failures.
    // Comment #7 if too few bytes are written, this will now throw
    //            you don't need to check
    // Comment #8 file.close() is automatic, but harmless to leave in    

    static_assert(CHAR_BIT == 8, "Can not use this platform since CHAR_BIT size is not 8");

    std::ofstream file(fileName, std::ios::binary); // Open binary, for output, for input
    file.seekp(fromPosition); // put pointer
    file.write((char*)buffer, numBytes);
    file.close();
}
catch(std::exception&)
{
    auto message = [&]() {
        std::ostringstream ss;
        ss << "writeBytesFrom: fileName = " << std::quoted(fileName) << ", fromPosition = " << fromPosition;
        return std::move(ss).str();
    } ();
    LOGE << message;
    std::throw_with_nested(std::runtime_error(message));
}


void test()
{
    extern std::string getfile();
    extern std::vector<std::uint8_t>& getbuffer();
    extern uint64_t getpos();

    auto&& file = getfile();
    auto&& buffer = getbuffer();
    auto pos = getpos();

    try
    {
        writeBytesFrom(file, pos, buffer.data(), buffer.size());
    }
    catch(std::exception& e)
    {
        // Comment #9 we can unwrap nested exceptions into one log line to
        // provide a complete history of the error
        LOGE << "test failed to write: " << unwrap(e);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM