Thread: Producer/Consumer Problem

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    9

    Question Producer/Consumer Problem

    Hello all,

    I want to write a program that uses multiple threads that does the following:

    Opens a simple text file containing AT LEAST 500 KB of data

    a reads a line of the file into a “buffer” (a “circular” array with fixed size = 10 data lines)
    b when “end of file” is detected, puts a “special code” in the buffer
    c continues reading until the buffer is full or waits for an indication that there is space in the buffer, then continues reading.

    I'm very confused on how to create a "buffer." Any help?


    Also, I want to implement this to:

    a get a line from the buffer and writes a line of the file to a file
    b exits when a special code is in the buffer.
    c repeats the previous steps (a, b) as long as there is data in the buffer, waits if the buffer is empty



    Any help will be greatly appreciated. I'm new at C++ programming, and am very interested in seeing this program work!! Thank you all for your help.

  2. #2
    Registered User
    Join Date
    Mar 2010
    Posts
    9
    Also, how do I create threads using C++ on a Windows machine? Thanks for the help.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Which C++ compiler do you have on your windows machine?
    And which OS?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Are you reading and writing in the same thread? Because it makes a big difference on the problem.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    9
    Well, the way I would compile my program would be remotely through a Linux machine running Gcc. I hope this information will aid you in helping me write this program.

    Reading and writing will occur on different threads.


    Please let me know if you need more information. Thank you so much for helping.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    You should just google or read in your textbook on how to write a circular buffer. Then you will need to thing about how to implement a mutex for the buffer.

    It's not a hard problem, but a book or tutorial will explain better than a brief account by a forum member.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 09-14-2008, 06:17 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

Tags for this Thread