New to Programming: Need Help/Advice on Topic

This is a discussion on New to Programming: Need Help/Advice on Topic within the C++ Programming forums, part of the General Programming Boards category; I am having trouble starting this program, but I'm sure if I get a little hint on a direction to ...

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

    Question New to Programming: Need Help/Advice on Topic

    I am having trouble starting this program, but I'm sure if I get a little hint on a direction to take, I'll be good to go.

    I want to create a program that has multiple threads that takes lines from a simple text file and puts it into a circular array with a fixed size of 10 data lines.

    While doing this, I'll have another thread that gets a line from the circular array and writes that line to another text file.


    My question is, how to set up this circular array that takes lines from a text file and adds these full lines into the array.

    A simple line would be, "Hello World" or anything like that. So the circular array will have ten lines from the input file, waits til the other thread takes from the array, then keeps adding the lines until end of file.

    Where should I begin? How do I implement this circular array, and how should I set up the input/output streams?

    Thanks in advance for all your help!

    NOTE: I believe this is similar to the producer/consumer program, but I am not sure.

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,203
    Was quite some time since i touched concurrency but I think you would want to read up on semaphores and/or monitors. For a container I would probably use something like the std::deque with the restriction of having max 10 objects in it at once.

    Hope this will get you on your way.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    9
    I'm going to use a Queue because that's more like a circular array.. first in first out kind of deal.

    But I have another question.. I want to have a thread that reads from a file, puts each "line" from the file into the queue, AND, while that is happening, have another thread that reads from the queue and puts these lines into an output file.

    I know how to do threads, but I'm curious as to how to set it up so that while the "producer" thread is taking from the input file and putting in the queue, how do I make the "consumer" thread take from the queue and put it into an output file at the same time?

    Thanks in advance!

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    9
    Also,

    how do I set the size of a queue? Is there a simple way to do this? I know of a way to set the size of an array in one line, is there a similar way of doing this?

    I want to set the size of my queue to, say, 10.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    Why would you want a fixed size? The point of a queue is that it is dynamic. It grows as elements are added.
    To address the threading question, you need to (in order):
    • Lock.
    • Check if there's data in the queue.
    • If there is data in the queue, pop an item from the queue.
    • If there is data in the queue, write it out.
    • Unlock.

    I'd use boost for this, as it has threading facilities with RAII.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    9
    Well, I want to make it a fixed size because I want to simulate the producer/consumer methodology. So that when the queue is "full," the producer thread will stop taking from the input file. It's just a restriction I want on the queue.

    Is there a simple way of doing this?

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    Then you need to check the size, and if it's full, don't add data to it. Nothing really changes from a fixed size queue, because you still have to do the check.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,667
    Well, I want to make it a fixed size because I want to simulate the producer/consumer methodology. So that when the queue is "full," the producer thread will stop taking from the input file. It's just a restriction I want on the queue.
    I don't think this needs to be concurrent, then.

  9. #9
    Registered User
    Join Date
    Mar 2010
    Posts
    9
    There will be two threads... one picking from my input file and putting each line into the queue (up to 10 lines capacity)... then (and while the producer thread is doing that), the consumer thread will take from the queue and put them into an output file. It's a simple concept, I'm just having trouble getting started with the queue with a fixed size.

    You say to make a check to see if it's full... like an if statement?

    if(myQ.size() < 10)
    //do work

    else
    //wait until consumer thread takes a line from the queue


    something like that, or am I totally off?

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    Something like this. But again, it begs the question if you really need concurrency? If it's just 10 lines, then you probably won't benefit from the added complexity.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Mar 2010
    Posts
    9
    What if I create an array of 10 integers... like x[10] for example.

    I'd need two values to keep track of the input location (for the producer) and one for the output position for the consumer).

    But then wouldn't I have to keep track of which positions are full and empty because the buffer wraps around (when you reach x[9], the next will be x[0])?

    How would one do this ?

  12. #12
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,667
    If you have the choice of using an array or a linked list, I would implement your circular array with a linked list; connect the head and tail. You'll know that you've gone around if you keep iterating in one direction to fill the list, you'll know you've gone around the list once you pass a sentinel (the head or tail) node.

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    But what is wrong with reading 10 elements, writing 10 elements, reading 10 elements ... ?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. topic for a presentation
    By dayknight in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 09-21-2004, 08:11 AM
  2. Most common topic on the cboard
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 01-15-2004, 04:42 PM
  3. Topic help!!!
    By Joanna in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 09-30-2003, 01:24 PM
  4. Normal Topic 4: If you had a homosexual boss in a programming job how would you feel?
    By Alphabird32 in forum A Brief History of Cprogramming.com
    Replies: 39
    Last Post: 04-07-2003, 03:49 PM
  5. Choose the Sixiest Contest Topic
    By ygfperson in forum Contests Board
    Replies: 2
    Last Post: 09-03-2002, 02:02 PM

Tags for this Thread


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