why std::ofstream.flush does not work normally? But work when use gdb attach preference?

Viewed 27
 void Fms::WriteFile(const char* p_buf, const int& p_len){
// printf("p_buf=%ld",p_buf);
/*
    string FileName(_fileDir + FileNamePre + "_" + std::to_string(p_tradingDay) + ".csv");
    ofstream &out = _fileHandle;
    out.open(FileName, std::ios::app|std::ios::out);
*/ 
_fileHandle.write(p_buf, p_len);
cnt_++;
//_fileHandle.flush();
if(cnt_ == 1000){
    _fileHandle.flush();
    cnt_ = 0;
}

This function will be invoked by a thread, which read buf from shared_memory in a loop.But the result is: Even there is still a lot of data in shared_memory, this function never flush ofstream to disk. When I use gdb attach the process(nohup ... &), I find that one time I switch to this thread and input 'c', ofstream will be flushed. How can I find the reason and how to flush ofstream as I want?

0 Answers
Related