Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / August 2007

Tip: Looking for answers? Try searching our database.

FileOpen and FILE_SARE_WRITE

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
A n g l e r - 13 Jul 2007 15:24 GMT
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.
Ben Voigt [C++ MVP] - 26 Jul 2007 14:34 GMT
> Hello there.
>
[quoted text clipped - 11 lines]
> 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.

> Thanks a lot,
> Peter.
A n g l e r - 26 Jul 2007 17:41 GMT
> Each HANDLE maintains its own file position.  Writing to one won't
> move
[quoted text clipped - 5 lines]
> 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.
Ben Voigt [C++ MVP] - 06 Aug 2007 18:16 GMT
>> 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
[quoted text clipped - 6 lines]
> 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.
A n g l e r - 18 Aug 2007 02:39 GMT
> 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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.