Thread: using threads to write & read from files

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    16

    using threads to write & read from files

    Hello Guys,

    I am trying to write code in C to copy the contents of two distinct files into one single file using two threads....

    I know how to write the things in general C like, fopen fprintf & fscanf & all but... when it comes to threads Iam dead Meet

    I know it should be like...I call the pthreads in main... functions (two for reading each file & one for writing...)

    I have to maintain locks as well to maintain the concurrecy stuff...

    Anyhelp will be very much appreciated...

    Thanx in advance
    Kishore

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    I just had the exact same problem in a C# program! I had a series of threads that were each generating there own data, and needed to log that data to a file - but the disorganization caused would be aobsolute choas if they program got busy. Unfortunately for you, C does not have a built in mechanism for handling this (a Mutex, is the word I just learned), but here's a nive little imitation you can do:

    - Have your thread tie all the data it needs to write to the file in one place, perhaps a struct.
    - Once a thread is done processing the data, have it put the struct in a queue.
    - Have a separate thread running that loops endlessly (with some way to break the loop when the user exits) taking structs off the queue and writing them to the file.

    Hope that helps! An easier solution (if this works for you) would be to just organize the data into two matrices, and when your two side threads are done executing, write those matrices to the file in order?

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    C does not have a built in mechanism for handling this
    POSIX threads has mutex functions.

    pthread_mutex_init()
    pthread_mutex_lock()
    pthread_mutex_unlock()

    Use those functions to synchronize access to writing the file.

  4. #4
    Registered User
    Join Date
    May 2004
    Posts
    16
    So, File locks in C are same as the mutex or do we have a separate lock kinda thing...!!!

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Just use what bithub showed you - don't try compare POSIX to what I told you - you'll just confused.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. write and read system calls
    By nacho4d in forum C Programming
    Replies: 4
    Last Post: 01-28-2008, 10:59 AM
  2. check for read or write
    By SoFarAway in forum C Programming
    Replies: 6
    Last Post: 04-09-2005, 01:50 AM
  3. trying to read all asci char's in files
    By brane_sail in forum C++ Programming
    Replies: 1
    Last Post: 09-02-2002, 11:33 AM
  4. Replies: 1
    Last Post: 07-24-2002, 06:33 AM
  5. how to read and write files in c++
    By kerick in forum C++ Programming
    Replies: 6
    Last Post: 03-29-2002, 09:41 PM