hi eric
Quote:
1. Can you prioritize each thread in your system, i.e. give it a different "thread priority"?
|
Posix threads offers an interface to that, but if the implementation actually does something sensibe is left up to the implementation.
But I have made bad experiences experimenting with that (on Sun Solaris).
Quote:
2. What is the time interval that a given thread gets pre-empted by a higher priority thread (assuming that you can have higher priority threads)?
|
You won't find an OS (besides hard realtime OSes) that lets you make any assumption on this value. The same yields for your question 3.
If I understood your scenario right, the structure you need is a condition variable and a mutex (you always need a mutex for a condvar).
Your thread 2 should wait on the condition variable until thread 1 signals it. Thread 1 then can do its further processing while thread 2 performs the status calculations. When thread 1 needs the data it locks the mutex and checks whether the data is already available. If yes, it processes it and if no, it waits on the condvar. When thread 2 is finished calculating the status it locks the mutex, writes the status to the memory (variable) shared between the processes, unlocks the mutex and signals the condvar. Then the loop can begin from start.
Example 1 (Race-Condition: thread 1 needs the status data before thread 2 is finished calculating it)
1. T2 waits on the condvar (with mutex locked)
2. T1 signals the condvar and continues processing
3. T2 gets woken up, unlocks the mutex and starts calculating the status
4. T1 locks the mutex, checks the data and finds it unfinished yet
5. T1 waits on the condvar (with mutex locked)
6. T2 is finished calculating the status, locks the mutex and writes the status
7. T2 signals the condvar, then unlocks the mutex
8. T1 gets woken up, reads the data and transmits it over network, then unlocks the mutex
Example 2 (Race-Condition: thread 2 is finished calculating the data before thread 1 needs it)
1. T2 waits on the condvar (with mutex locked)
2. T1 signals the condvar and continues processing
3. T2 gets woken up, unlocks the mutex and starts calculating the status
4. T2 is finished calculating the status, locks the mutex and writes the status
5. T2 signals the condvar, then unlocks the mutex
6. T1 locks the mutex, checks the data and finds it finished
7. T1 reads the data and transmits it over network, then unlocks the mutex
As you can see, at the end of each example the mutex is unlocked.
Please do not hesitate to ask if something still is unclear.
So long...
bruce