1. ## 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. 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. 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. 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)"...

5. Originally Posted by GReaper
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.

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. 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. Originally Posted by iMalc
*cough* *ahem* printf.
Οops

9. 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. 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. 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.