HighTechTalks DotNet Forums  

Writing into a binary file simultaneously from multithreads...

Dotnet Framework microsoft.public.dotnet.framework


Discuss Writing into a binary file simultaneously from multithreads... in the Dotnet Framework forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Steve B.
 
Posts: n/a

Default Writing into a binary file simultaneously from multithreads... - 04-11-2006 , 06:21 AM






Hi,

I'm building an application that use a binary file format with a proprietary
format.
Files can grow up to several GBytes...

The mecanism of building the data is quite complex and I'd like to know if
multiple threads can -simultaneously- write into the data file...

For example, I know in advance thread one will write the first 100MB, thread
two 50 next MB, and so on. Can I write from multiple threads ?
What objects Have I to use ?
One FileStream and seeking as needing ? One FileStream and several
StreamWriters ? etc ...

Thanks in advance...
Steve



Reply With Quote
  #2  
Old   
Jon Skeet [C# MVP]
 
Posts: n/a

Default Re: Writing into a binary file simultaneously from multithreads... - 04-11-2006 , 11:48 AM






Steve B. <steve_beauge (AT) com (DOT) msn_swap_com_and_msn> wrote:
Quote:
I'm building an application that use a binary file format with a proprietary
format.
Files can grow up to several GBytes...

The mecanism of building the data is quite complex and I'd like to know if
multiple threads can -simultaneously- write into the data file...

For example, I know in advance thread one will write the first 100MB, thread
two 50 next MB, and so on. Can I write from multiple threads ?
What objects Have I to use ?
One FileStream and seeking as needing ? One FileStream and several
StreamWriters ? etc ...
I suspect there *are* ways of doing it from multiple threads, but it
feels like the kind of thing which could very easily go wrong. An
alternative which may well be simpler would be to have a single thread
which had a queue of write requests. The other threads would add jobs
to the queue, and then the single thread could process those requests.

--
Jon Skeet - <skeet (AT) pobox (DOT) com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Reply With Quote
  #3  
Old   
Steve B.
 
Posts: n/a

Default Re: Writing into a binary file simultaneously from multithreads... - 04-12-2006 , 06:27 AM



After some tests, I found a solution which seems to work :

lock (outputStream)

{

outputStream.Seek(

dataOffset + numberObBytesWritten

SeekOrigin.Begin

);

outputStream.Write(

buffer,

0,

read

);

I'm not sure this is a very smart solution.
Your solution should look like this :

WriterClass.QueueWriteOperation(Stream s, int offset, byte[] buffer)...
That's it ?

Thanks,
Steve

"Jon Skeet [C# MVP]" <skeet (AT) pobox (DOT) com> a écrit dans le message de news:
MPG.1ea5ee209c38794798d087 (AT) msne... microsoft.com...
Quote:
Steve B. <steve_beauge (AT) com (DOT) msn_swap_com_and_msn> wrote:
I'm building an application that use a binary file format with a
proprietary
format.
Files can grow up to several GBytes...

The mecanism of building the data is quite complex and I'd like to know
if
multiple threads can -simultaneously- write into the data file...

For example, I know in advance thread one will write the first 100MB,
thread
two 50 next MB, and so on. Can I write from multiple threads ?
What objects Have I to use ?
One FileStream and seeking as needing ? One FileStream and several
StreamWriters ? etc ...

I suspect there *are* ways of doing it from multiple threads, but it
feels like the kind of thing which could very easily go wrong. An
alternative which may well be simpler would be to have a single thread
which had a queue of write requests. The other threads would add jobs
to the queue, and then the single thread could process those requests.

--
Jon Skeet - <skeet (AT) pobox (DOT) com
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too



Reply With Quote
  #4  
Old   
Jon Skeet [C# MVP]
 
Posts: n/a

Default Re: Writing into a binary file simultaneously from multithreads... - 04-12-2006 , 12:14 PM



Steve B. <steve_beauge (AT) com (DOT) msn_swap_com_and_msn> wrote:
Quote:
After some tests, I found a solution which seems to work :

lock (outputStream)
{
outputStream.Seek(
dataOffset + numberObBytesWritten
SeekOrigin.Begin
);

outputStream.Write(
buffer,
0,
read
);

I'm not sure this is a very smart solution.
It's not particularly safe, as if you have two streams pointing at the
same file, they could still both update it simultaneously.

Quote:
Your solution should look like this :

WriterClass.QueueWriteOperation(Stream s, int offset, byte[] buffer)...
That's it ?
I wouldn't pass the stream - I'd create one WriterClass (or whatever)
per file I wanted to write to, and have it maintain the stream. Now,
admittedly you'd still need to make sure you didn't create two
WriterClasses for the same file, but that's probably easier to do than
the stream equivalent. (You could easily provide a factory method, in
fact.)

Note that an easier way of seeking is often to use the Position
property:

outputStream.Position = dataOffset+numberOfBytesWritten;

--
Jon Skeet - <skeet (AT) pobox (DOT) com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Reply With Quote
  #5  
Old   
Steve B.
 
Posts: n/a

Default Re: Writing into a binary file simultaneously from multithreads... - 04-13-2006 , 02:10 AM



Thanks,
I'll take a deep look into this mecanism.

Steve


"Jon Skeet [C# MVP]" <skeet (AT) pobox (DOT) com> a écrit dans le message de news:
MPG.1ea745d2a50f720f98d09c (AT) msne... microsoft.com...
Quote:
Steve B. <steve_beauge (AT) com (DOT) msn_swap_com_and_msn> wrote:
After some tests, I found a solution which seems to work :

lock (outputStream)
{
outputStream.Seek(
dataOffset + numberObBytesWritten
SeekOrigin.Begin
);

outputStream.Write(
buffer,
0,
read
);

I'm not sure this is a very smart solution.

It's not particularly safe, as if you have two streams pointing at the
same file, they could still both update it simultaneously.

Your solution should look like this :

WriterClass.QueueWriteOperation(Stream s, int offset, byte[] buffer)...
That's it ?

I wouldn't pass the stream - I'd create one WriterClass (or whatever)
per file I wanted to write to, and have it maintain the stream. Now,
admittedly you'd still need to make sure you didn't create two
WriterClasses for the same file, but that's probably easier to do than
the stream equivalent. (You could easily provide a factory method, in
fact.)

Note that an easier way of seeking is often to use the Position
property:

outputStream.Position = dataOffset+numberOfBytesWritten;

--
Jon Skeet - <skeet (AT) pobox (DOT) com
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too



Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.