Accessing a single file with multiple threads

前端 未结 4 619
感情败类
感情败类 2020-12-03 03:29

I need to access a file concurrently with multiple threads. This needs to be done concurrently, without thread serialisation for performance reasons.

The file in par

4条回答
  •  南笙
    南笙 (楼主)
    2020-12-03 04:13

    You can do on that way...

    First thread with read/write access must at first create file:

    FileHandle := CreateFile(
      PChar(FileName),
      GENERIC_READ or GENERIC_WRITE,
      FILE_SHARE_READ,
      nil,
      CREATE_ALWAYS,
      FILE_ATTRIBUTE_NORMAL,
      0);
    

    Sencond thread with only read access then opens the same file:

      FileHandle := CreateFile(
        PCHar(FileName),
        GENERIC_READ,
        FILE_SHARE_READ + FILE_SHARE_WRITE,
        nil,
        OPEN_EXISTING,
        FILE_ATTRIBUTE_NORMAL,
        0);
    

    I didn't test if works with...

    FILE_ATTRIBUTE_TEMPORARY,
    FILE_FLAG_DELETE_ON_CLOSE
    

    attributes...

提交回复
热议问题