cant get threads to behave

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 632 Views - Last Post: 05 October 2012 - 10:26 AM Rate Topic: -----

#16 rfs02  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 26
  • View blog
  • Posts: 70
  • Joined: 30-September 12

Re: cant get threads to behave

Posted 05 October 2012 - 10:26 AM

View Posthealix, on 04 October 2012 - 07:41 PM, said:

ok so I need to store the values of the threads in variables? What variables? A variable for each thread? Then add the variables together? I thought threads worked differently. With variables it seems like a regular array.

By the way. If you haven't guessed. This is my first time using threads.


Not sure if you've been able to solve this or not yet.

You should be able to have a "static" variable in the class that you use from all your threads. The problem you will have to deal with is that thread 3 needs to wait for the two other threads to have the data available before you do the sum - which is where the lock comes in.

Effectively, the way I would structure this (given the limited information about the assignment available here) is:

  • Have two static variables: "odd" and "even"
  • Thread "Odd" locks the odd variable, updates it and then releases it
  • Thread "Even" locks the even variable, updates it and then releases it
  • Common thread locks both variables, prints out the sum, and then releases both variables

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2