How do I lock files using fopen()?

前端 未结 7 1927
时光取名叫无心
时光取名叫无心 2020-12-01 06:41

I wonder if there is any way to lock and unlock a file in Linux when I open a file using fopen (not open)?

Based on Stack Overflow question

7条回答
  •  情深已故
    2020-12-01 07:16

    The below code is not letting me lock the file using lockf, flock works fine though

        #include 
        #include 
        #include
        #include 
        #include 
        #include 
        #include 
    
    using namespace std;
    
    void append()
    {
        FILE *fp=fopen("a.txt","a");
        if(fp)
        {
            cout< v;
        //#pragma omp parallel for
        for(int i=0;i<1000;++i)
        {
            v.push_back(new thread(append));
    
            //append();
        }
    
        for(auto y:v)
        {
            y->join();
            delete y;
        }
    
        return 0;
    }
    

提交回复
热议问题