Thread: Multithreading and File IO

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912

    Multithreading and File IO

    Hit another snag in my server program. Just to remind you of the basic setup, once the user tells the server to start, it is an infinite (sort of) loop of listening to a socket and dispatching each request to a new thread to be handled. Obviously servers need to keep logs, and I had planned to have the log consist of all the user settings (root directory, CGI programs, etc...) followed by a series of entires. Something like:

    Code:
    Timestamp1
    Request1
    	 Headers1
    	 Headers1
    Status1
     
    Timestamp2
    Request2
    	 Headers2
    	 Headers2
    Status2
    I thought this would all work out okay, but the danger of multithreading just hit me. If I have two requests come in very close to eachother, the records might overlap and it would be something like:

    Code:
    Timestamp1
    Request1
    	 Headers1
    Timestamp2
    	 Headers1
    Request2
    Status1
    	 Headers2
    	 Headers2
    Status2
    Which would obviously get confusing with the actual data in there too.

    A couple of ideas, which require your feedback:

    1) Writing the whole record to an array of strings (or hashtable), and sending that to the file in one go. Would there still be the possibility of overlap?

    2) Assigning a unique number to each request and precede each piece of the log with that number.

    3) Maybe set up a queue of requests that need to be logged and having a completely separate thread that just reads from the queue and wrties to the file.

    I know 2 would work, but it's a bit messy. If possible, I'd rather have the output of 1/3.

    As far as 1 goes, I like this idea best, but I don't know how well it would work. I'm still getting everything set up on the computer I plan to test the server on, so it's the not the most convenient time...

    And 3 - I like this idea too, I'm just not sure how to make this 'queue'.

    Thanks a lot for any ideas/advice.
    Last edited by sean; 10-17-2004 at 05:55 PM.

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Having a structure of one log entry, inserting this into a queue and working that queue with a different thread looks like a good idea to me. If it's just for thread safety, there is a Mutex class in .NET.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 04-18-2008, 10:06 AM
  2. multithreading in c
    By thebrighter in forum C Programming
    Replies: 8
    Last Post: 07-10-2007, 01:17 PM
  3. multithreading program help?
    By pthread in forum Linux Programming
    Replies: 7
    Last Post: 12-18-2006, 04:37 AM
  4. Question on Multithreading
    By ronan_40060 in forum C Programming
    Replies: 1
    Last Post: 08-23-2006, 07:58 AM
  5. Multithreading problem
    By Bacardi34 in forum C Programming
    Replies: 5
    Last Post: 09-02-2004, 02:26 PM