从C ++中的不同线程写入文件

时间:2018-09-13 13:40:12

标签: c++ synchronization

我正在编写一个程序,该程序从不同的线程写入二进制文件。每个线程将写入文件的不同位置。我不使用同步,程序也可以正常工作。我想问一下您是否应该使用一些同步,以及如何或是否足以让OS同步以某种方式执行此操作。我使用的是Linux,gcc编译器,但有时也可以在其他平台上运行。我使用以下函数从不同的线程写入文件。

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());
    }
}

如果您有任何进一步的建议来改进我的代码,我愿意接受。我使用uint8_t缓冲区的原因是,对我来说,理解该缓冲区代表8位字节比使用本人即使用无符号字符更自然。我知道有些纯粹主义者可能对此表示反对。

谢谢, 伏伊塔。

1 个答案:

答案 0 :(得分:0)

  

如果您有任何其他改进我的代码的建议,我愿意接受。

不是每个人都会同意每一个注释或代码样式建议,但是这些对我有用:

#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);
    }
}