473,794 Members | 2,708 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Writing to a file from more than one thread possible?

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
10 1685
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
>
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
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
Because I'm downloading yEncoded files from a usenet news group. Nothing
shifty here, perfectly legal. Grabbing Fan-Made Mission files from
alt.binaries.ga mes.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.
"ModelBuild er" <Mo**********@d iscussions.micr osoft.comwrote in message
news:5A******** *************** ***********@mic rosoft.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
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.ga mes.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
I'm sorry. For some reason I had thought you were talking about four
System.Threadin g.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.ga mes.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.
"ModelBuild er" <Mo**********@d iscussions.micr osoft.comwrote in message
news:5A******** *************** ***********@mic rosoft.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
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
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
"ModelBuild er" <Mo**********@d iscussions.micr osoft.comwrote in message
news:89******** *************** ***********@mic rosoft.com...
I'm sorry. For some reason I had thought you were talking about four
System.Threadin g.Threads, not discussion threads.
You were correct. I'll have 4 System.Threadin g.Threads running, each
downloading a part of the file.
Jul 27 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
10906
by: Ben Dewey | last post by:
Project: ---------------------------- I am creating a HTTPS File Transfer App using ASP.NET and C#. I am utilizing ActiveDirectory and windows security to manage the permissions. Why reinvent the wheel, right? Everything so far is working well with the Active Directory. The problem I am having is with adding File Permissions to a directory. I am currently using some code courtesy of "Willy Denoyette "
0
1064
by: Dante | last post by:
I have moved a heavy calculation inside my application to multiple threads. Previously I was saving the results to a file and after finishing I collected them all in one mdb file. Unfortunately this approach is too slow so I decided to save all in one mdb file; now each thread is writing into its own temporary table inside the MDB to avoid thread collisions and locking. But, is this approach thread safe? Currently I am only guessing, but...
6
5274
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
6
2176
by: bonk | last post by:
I am trying to create a stream that writes text to a file and: - automatically creates a new file once the current file exceeds a certain size - makes it possible to be used by multiple threads and/or processes so that multiple threads/processes can write to the same file (all threads use the same instance of the stream, processes use a different instance but still may point to the same file) Could you point me in the correct...
3
9243
by: patrickdepinguin | last post by:
Hi, I need to write large quantities of data to a file in C. The data comes from statistics that are continuously gathered from a simulator, and in order to not slow the whole thing down I would obviously want the writes to go as fast and efficient as possible. Since I/O operations are rather slow, I was thinking that using a large buffer would be better than writing each data point every time. Each data point calls my function, at which...
118
4703
by: Chuck Cheeze | last post by:
This might be in the wrong group, but... Here is an example of my data: entry_id cat_id 1 20 2 25 3 30 4 25 5 35
30
2706
by: Cramer | last post by:
I've finally gotton board with TDD (test driven development) and wow is it effective! I went from sceptic to True Believer with my first effort. My question: According to the various books and articles I have read about TDD, a good unit test does not rely on the database or other such external/environmental conditions. More generally, a good unit test is atomic and makes as few assumptions about its runtime environment as possible. But...
16
1468
by: AAaron123 | last post by:
I have a timer. At each tick, say 0.1 second, I write a file. If it takes more than 0.1 second to write the file the app will not work correctly. How can I tell in the tick event if the previous file finished writing so I can skip writing at that time? I write using the Bitmap.Save method.
11
12472
by: Krzysztof Retel | last post by:
Hi guys, I am struggling writing fast UDP server. It has to handle around 10000 UDP packets per second. I started building that with non blocking socket and threads. Unfortunately my approach does not work at all. I wrote a simple case test: client and server. The client sends 2200 packets within 0.137447118759 secs. The tcpdump received 2189 packets, which is not bad at all. But the server only handles 700 -- 870 packets, when it is...
0
9671
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9518
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10212
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9035
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5436
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5560
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4112
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2919
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.