how can Multiple threat write one text File

This is a discussion on how can Multiple threat write one text File within the C++ Programming forums, part of the General Programming Boards category; I have multi threaded application, and everythreat logging some error messages and logging process but, I dont know how to ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    18

    how can Multiple threat write one text File

    I have multi threaded application, and everythreat logging some error messages and logging process but, I dont know how to log multiple threat's working in a text file. Could you help me.
    Thanx you in advance...

  2. #2
    chococoder
    Join Date
    Nov 2004
    Posts
    515
    Easiest solution is to have a logging framework that queues writes for handling by a single thread (or synchronises write operations so only one thread at a time can write).

    If you don't do that you're running the very real risk of simultaneous write attempts causing multiple threads to attempt writes at the same filepointer position, leading to (at best) loss of data, deadlock, and/or crashes.

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    18
    Yes its risky task more than just open a file and write in it. Is there any available framework for the logging purpose? or may I achieve success by just using semaphores or something similar.
    Thanx u in advance

  4. #4
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Why not use mutexes?
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  5. #5
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    Use boost::thread and boost::mutex

  6. #6
    Registered User
    Join Date
    Aug 2007
    Posts
    18
    Ok. I will create critical section and monitor resources. Another thing make me confused is print screen. Do multiple threats print something to the screen without conflicting? is there any queue mechanism for display or I have to apply same mechanism to prevent that as well?

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    create a mutex. then every cout/cerr operation should be enclosed like this:

    Code:
    lock stdoutmutex
    cout << ... << endl;
    unlock stdoutmutex
    I do it like this and it prevents terminal output goulash

  8. #8
    Registered User
    Join Date
    Aug 2007
    Posts
    18
    I apreciate your help. Thanks a lot, and pheres It really works I love it. thanx again

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I believe that the threaded library at least on MS Visual studio can cope with multiple threads writing to the same file - at least at the basic level of "not crashing" - your output may be scrambled if you use things like fprintf or fputc - but if you use one fputs or fwrite to output the data, then it should be fine.

    As to whether other compiler/C-libraries cope with this OK or not is a different story.

    --
    Mats

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  3. write a file text
    By mickey0 in forum C++ Programming
    Replies: 6
    Last Post: 07-17-2007, 03:36 AM
  4. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 01:59 PM
  5. checking values in a text file
    By darfader in forum C Programming
    Replies: 2
    Last Post: 09-24-2003, 02:13 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21