Thread: Multiple Threads - computing

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    6

    Lightbulb Multiple Threads - computing

    Hi. I have been assigned a task that has me stumped. I am to create 3 threads, parent and children, using the fibonacci sequence. I have two problems.


    1) The first is getting my functions to work properly.
    My first function successfully generates the number of Fibonacci numbers given by the command line. For some inane reason I can't figure out how to write a function that adds 1 to each number (in a separate thread) and another function that subtracts 1 (in a separate thread). I get all sorts of wacky computations.


    2) The second problem is getting my threads to accept my fibonacci data.
    I was successful in creating 3 threads, but they are generic and as a start I am unsuccessful in making my original fibonacci sequence fit into the first (parent) thread.


    My program currently successfully asks for an input and spits out the number of fibonacci numbers asked for. But then it runs 3 threads showing me a set number and thread number, but with no fibonacci data linked. It is just printing my out statement to the console. I have it set to run 10 sets, for each thread.


    So I need to know how to get my three functions each into the threads successfully. Even if it had the same info every time, it would be great to just see it using my fib data. Every time I add anything to my threads the code breaks.


    I'm happy to paste my code here, but I'm not looking for anyone to do my work and I didn't want to be presumptuous. Any advice is helpful.


    Thanks,
    GigTu

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    I have been assigned a task that has me stumped. I am to create 3 threads, parent and children, using the fibonacci sequence.
    That sounds pretty abmiguous.
    Can you quote the exact problem?

    If all you need is to generate the n th fibonacci number using 3 threads, a simple solution is to to use memoization and a lot of locks.

  3. #3
    Registered User
    Join Date
    May 2013
    Posts
    6
    Hi manasij,

    No, I need to have one thread spit out the fib sequence, another is fib number plus 1, a third thread to calculate fib minus 1.

    They should match up, or be parallel to the original fib sequence as shown:
    original is 0 1 1 2 3
    plus 1 is 1 2 2 3 4
    minus 1 is -1 0 0 1 2

    etc

    I can't figure out how to code that. n+ 1 and n++ (for examples) aren't working.

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Well, if I had an array of fibonacci numbers and I was printing them with "scanf("%d", fib[i])", I'd just do "scanf("%d", fib[i]+1)" and "scanf("%d", fib[i]-1)"...
    Devoted my life to programming...

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by GReaper View Post
    Well, if I had an array of fibonacci numbers and I was printing them with "scanf("%d", fib[i])", I'd just do "scanf("%d", fib[i]+1)" and "scanf("%d", fib[i]-1)"...
    *cough* *ahem* printf.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    May 2013
    Posts
    6
    "%d", fib[i]+1)" and "%d", fib[i]-1)" do not work when i insert them into the threads, but then neither does my original function of first + second = next .....etc etc etc

  7. #7
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Just a sanity check here, is the version of printf that you are using supposed to be able to work with multiple threads?

    It's not clear to me what the 3 threads are supposed to do. Are the 2 other threads supposed to regenerate Fibonacci numbers -1, and +1, or are they supposed to use an existing array of Fibonacci numbers and create new arrays of the existing array - or + 1?

  8. #8
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by iMalc View Post
    *cough* *ahem* printf.
    Οops
    Devoted my life to programming...

  9. #9
    Registered User
    Join Date
    May 2013
    Posts
    6
    Thread 1 generates the number of Fibonacci numbers given by the command line ie: 0 1 1 2 3
    Thread 2 adds +1 to each of the numbers generated by thread 1 ie: 1 2 2 3 4
    Thread 3 subtracts -1 from each of the numbers generated by thread 1 ie: -1 0 0 1 2

  10. #10
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Well one approach to this would be to have thread 1 to send a message to thread 2 and thread 3 for each Fibonacci number added to an array, and in turn thread 2 and thread 3 would append to their arrays using the numbers sent from thread 1 +/- 1. This doesn't seem like a good example for a multi-threaded program, so maybe I'm missing something here.

  11. #11
    Registered User
    Join Date
    May 2013
    Posts
    6
    It's probably just an exercise that squeezes in more than one lesson. I don't think it's meant to be efficient. I have a working code, I just need to get it to call the right set numbers and get my fib -1 to work correctly, No matter what I do it breaks. ugh

    Thanks so much fo all of your help. I'm not there yet, but you all got me thinking.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with multiple threads
    By Cogman in forum Windows Programming
    Replies: 2
    Last Post: 07-05-2009, 09:40 AM
  2. Multiple Threads, One listener.
    By PING in forum Networking/Device Communication
    Replies: 3
    Last Post: 03-27-2009, 12:19 PM
  3. Multiple Threads
    By NuNn in forum C Programming
    Replies: 3
    Last Post: 03-14-2009, 11:29 PM
  4. Multiple Threads ?
    By C++Gamer in forum C++ Programming
    Replies: 11
    Last Post: 07-11-2006, 12:12 PM
  5. a point of threads to create multiple threads
    By v3dant in forum C Programming
    Replies: 3
    Last Post: 10-06-2004, 09:48 AM