By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,117 Members | 2,178 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,117 IT Pros & Developers. It's quick & easy.

Writing to a file from more than one thread possible?

P: n/a
I need to be able to write to a file simultaneously from approximately 4
different threads. I'm working on a program that will download parts of a
file and combine the parts. Each thread will have an offset from zero and a
length to write. None of the threads will overlap in the file position.

Is it possible to have the file open (shared) by multiple threads? I'm
guessing that i'd use a filestream with some sort of sharing. Looking for
some pointers and/or examples. Thanks!
Jul 25 '07 #1
Share this Question
Share on Google+
10 Replies


P: n/a
I need to be able to write to a file simultaneously from approximately
4 different threads. I'm working on a program that will download parts
of a file and combine the parts. Each thread will have an offset from
zero and a length to write. None of the threads will overlap in the
file position.

Is it possible to have the file open (shared) by multiple threads? I'm
guessing that i'd use a filestream with some sort of sharing. Looking
for some pointers and/or examples. Thanks!
Wouldn't it just be simpler to have the multiple threads write to different
files and combine them at the end by creating the final file at this point,
drawing it's data from these other file in sequence?

--
Rory
Jul 25 '07 #2

P: n/a
>
Wouldn't it just be simpler to have the multiple threads write to
different files and combine them at the end by creating the final file at
this point, drawing it's data from these other file in sequence?
No, I'm using a program that does that right now and it creates thousands of
small files and then combines them when it's all done. This causes constant
hard drive activity and my computer is unusable while the program is doing
this. So i'm trying to duplicate the program and combine the data on the
fly, only writing the one file on the hard drive.

I've been leaning toward having one "file manager" thread that I pass a byte
array & file offset and let it do the writing.
Jul 25 '07 #3

P: n/a
I'm just curious why you chose four threads. I mean, if you want something
optimal, I would think the thread count is based on a buffer size. In
theory, I doubt the question matters if there was two or twenty-two threads.

"Terry Olsen" wrote:
I need to be able to write to a file simultaneously from approximately 4
different threads. I'm working on a program that will download parts of a
file and combine the parts. Each thread will have an offset from zero and a
length to write. None of the threads will overlap in the file position.

Is it possible to have the file open (shared) by multiple threads? I'm
guessing that i'd use a filestream with some sort of sharing. Looking for
some pointers and/or examples. Thanks!
Jul 26 '07 #4

P: n/a
Because I'm downloading yEncoded files from a usenet news group. Nothing
shifty here, perfectly legal. Grabbing Fan-Made Mission files from
alt.binaries.games.thief

My news server allows 4 connections. So i'll be simultaneously downloading 4
parts of the file at a time, yDecoding them on the fly, and writing the
decoded bytes to the file.

Some news servers allow upwards of 10 to 20 connections, so it's possible
that someone else using the program may have that many threads running and
all trying to write to the file.

I've built a FileWriter class that lets me pass the Filename, byte array,
file offset, and length. I used SyncLock to make sure each thread waits its
turn. Thanks to the guy who made that suggestion. I haven't tested it yet
but I have high hopes that it will work well.
"ModelBuilder" <Mo**********@discussions.microsoft.comwrote in message
news:5A**********************************@microsof t.com...
I'm just curious why you chose four threads. I mean, if you want
something
optimal, I would think the thread count is based on a buffer size. In
theory, I doubt the question matters if there was two or twenty-two
threads.

"Terry Olsen" wrote:
>I need to be able to write to a file simultaneously from approximately 4
different threads. I'm working on a program that will download parts of a
file and combine the parts. Each thread will have an offset from zero and
a
length to write. None of the threads will overlap in the file position.

Is it possible to have the file open (shared) by multiple threads? I'm
guessing that i'd use a filestream with some sort of sharing. Looking for
some pointers and/or examples. Thanks!

Jul 26 '07 #5

P: n/a
Terry Olsen wrote:
Because I'm downloading yEncoded files from a usenet news group.
Nothing shifty here, perfectly legal. Grabbing Fan-Made Mission files
from alt.binaries.games.thief

My news server allows 4 connections. So i'll be simultaneously
downloading 4 parts of the file at a time, yDecoding them on the fly,
and writing the decoded bytes to the file.
But surely, using 4 connections won't make it download any faster? Isn't the
download speed limited by the speed of your Internet connection? I suppose
it might even make it slower as the disk drive head on the news server has
to keep moving between four parts of the file.

Andrew
Jul 26 '07 #6

P: n/a
I'm sorry. For some reason I had thought you were talking about four
System.Threading.Threads, not discussion threads.

"Terry Olsen" wrote:
Because I'm downloading yEncoded files from a usenet news group. Nothing
shifty here, perfectly legal. Grabbing Fan-Made Mission files from
alt.binaries.games.thief

My news server allows 4 connections. So i'll be simultaneously downloading 4
parts of the file at a time, yDecoding them on the fly, and writing the
decoded bytes to the file.

Some news servers allow upwards of 10 to 20 connections, so it's possible
that someone else using the program may have that many threads running and
all trying to write to the file.

I've built a FileWriter class that lets me pass the Filename, byte array,
file offset, and length. I used SyncLock to make sure each thread waits its
turn. Thanks to the guy who made that suggestion. I haven't tested it yet
but I have high hopes that it will work well.
"ModelBuilder" <Mo**********@discussions.microsoft.comwrote in message
news:5A**********************************@microsof t.com...
I'm just curious why you chose four threads. I mean, if you want
something
optimal, I would think the thread count is based on a buffer size. In
theory, I doubt the question matters if there was two or twenty-two
threads.

"Terry Olsen" wrote:
I need to be able to write to a file simultaneously from approximately 4
different threads. I'm working on a program that will download parts of a
file and combine the parts. Each thread will have an offset from zero and
a
length to write. None of the threads will overlap in the file position.

Is it possible to have the file open (shared) by multiple threads? I'm
guessing that i'd use a filestream with some sort of sharing. Looking for
some pointers and/or examples. Thanks!


Jul 26 '07 #7

P: n/a
I've been leaning toward having one "file manager" thread that I pass
a byte array & file offset and let it do the writing.
Sounds interesting. Hows about adding each ByteArray or stream to a queue
together with a description of it s destination byterange. Then your filemanager
can be the only thread to write to the file and can deal with the bytearrays/streams
as they are completed and added to said queue.

--
Rory
Jul 26 '07 #8

P: n/a
But surely, using 4 connections won't make it download any faster? Isn't
the download speed limited by the speed of your Internet connection? I
suppose it might even make it slower as the disk drive head on the news
server has to keep moving between four parts of the file.
I have an 8MB cable connection. The news server caps each connection to
40kbs. So 4 connections gives me 160kps effective speed.

And the news server shouldn't have any problem with the disk drive heads as
the ISP has hundreds of thousands of subscribers, many, i'm sure, use the
newsgroups.
Jul 27 '07 #9

P: n/a
"ModelBuilder" <Mo**********@discussions.microsoft.comwrote in message
news:89**********************************@microsof t.com...
I'm sorry. For some reason I had thought you were talking about four
System.Threading.Threads, not discussion threads.
You were correct. I'll have 4 System.Threading.Threads running, each
downloading a part of the file.
Jul 27 '07 #10

P: n/a
Terry Olsen wrote:
>But surely, using 4 connections won't make it download any faster?
Isn't the download speed limited by the speed of your Internet
connection? I suppose it might even make it slower as the disk drive
head on the news server has to keep moving between four parts of the
file.

I have an 8MB cable connection. The news server caps each connection
to 40kbs. So 4 connections gives me 160kps effective speed.
That makes sense then :-)

The only constructive suggestion I have is that it might be helpful to do a
..SetLength on the filestream, which I would hope would throw an exception if
there wasn't enough disk space.

Andrew
Jul 27 '07 #11

This discussion thread is closed

Replies have been disabled for this discussion.