FileOpen and FILE_SARE_WRITE

  • Thread starter Thread starter A n g l e r
  • Start date Start date
A

A n g l e r

Hello there.



What does happen when two threads write to one file opened with
attribute FILE_SHARE_WRITE? Let's that both threads save an array of 10
elements as follows:
THR1: WriteFile(handle1, array1, 10);
THR2: WriteFile(handle2, array2, 10);

where handle1 and handle2 are handles to the same file opened with the
FILE_SHARE_WRITE attribute.

Will these operation cause two arrays will be saved consecutively, or
perhaps will they become completely mixed in a file? Do I have to use a
synchronization like mutex or critical sections, or is this implemented
on the WriteFile level somehow?



Thanks a lot,
Peter.
 
A n g l e r said:
Hello there.



What does happen when two threads write to one file opened with attribute
FILE_SHARE_WRITE? Let's that both threads save an array of 10 elements as
follows:
THR1: WriteFile(handle1, array1, 10);
THR2: WriteFile(handle2, array2, 10);

where handle1 and handle2 are handles to the same file opened with the
FILE_SHARE_WRITE attribute.

Will these operation cause two arrays will be saved consecutively, or
perhaps will they become completely mixed in a file? Do I have to use a
synchronization like mutex or critical sections, or is this implemented on
the WriteFile level somehow?

Each HANDLE maintains its own file position. Writing to one won't move the
other one forward, so one array will end up written on top of the other. I
think as long as WriteFile succeeds in a single call, you'll get one or the
other array in its entirety. But if you end up needing a sequence of
partial writes you could end up with part of each array.
 
Each HANDLE maintains its own file position. Writing to one won't
move
the other one forward, so one array will end up written on top of the
other. I think as long as WriteFile succeeds in a single call,
you'll
get one or the other array in its entirety. But if you end up
needing
a sequence of partial writes you could end up with part of each
array.


Hi there.

I guess that I should have tested what I wanted - that would be the
quickest way to tease out the answer. Anyway, let me explain that I'm
talking here about accessing HID device as a file. Does it change
anything?


Cheers,
Peter.
 
A n g l e r said:
Hi there.

I guess that I should have tested what I wanted - that would be the
quickest way to tease out the answer. Anyway, let me explain that I'm

That would have a race condition, so you might not actually get the right
answer.
talking here about accessing HID device as a file. Does it change
anything?

Well, you should test whether you can open multiple handles at once. Some
devices can't be opened multiple times regardless of sharing options. After
that, everything depends on the driver.
 
Well, you should test whether you can open multiple handles at once.
Some devices can't be opened multiple times regardless of sharing
options. After that, everything depends on the driver.

Can be opened multiple times coz I designed it this way. Anyway,
methinks this is gonna be races there - it's just a mater whether
amongst order of two (more) arriving packets, or their bytes as well.
Will have to take care of this and use mutexes in a dll designed to
access this.

Cheers,
P.
 
Back
Top