Roman Morokutti
Anthony Williams wrote:
If you declare variables "extern" at block scope then you need to provide a declaration at global scope too.
The point of declaring them extern in the example is to say that these variables are accessible to, and used by, code elsewhere in the system. In particular, another thread will lock the mutex, set done to true and then notify the condition variable. Thank you for your explanation, Anthony.
You're welcome.
As I am relatively new to threading, I have another question regarding your example:
Are those variables considered to be for thread local storage. And if, would it be preferable then to use the 'extern' specifier in general?
Thread local storage is manifested in boost with boost::thread_specific_ptr. It's use should be limited to globals (or things that are effectively globals) where each thread must have its own copy. Condition variables, mutexes and the "done" variable from this example are used to communicate between threads, so must be accessible to all threads concerned. Thread local storage is thus a bad idea. The "extern" specifier was trying to convey the idea that these variables are accessible from elsewhere. In general there is very little reason to use extern in practice --- just declare you variables in a shared header, for example.
The problem for me (as a novice) on this example is, that it is shown in a very isolated context.
It was trying to demonstrate a specific feature (using timed wait for a condition variable), not provide a full-featured example of condition variable usage.
When will the patches for the Sun Compiler be integrated into the trunk. For me, I use the CC 5.10 version, these patches worked well.
I will look over them today. Anthony -- Author of C++ Concurrency in Action | http://www.manning.com/williams just::thread C++0x thread library | http://www.stdthread.co.uk Just Software Solutions Ltd | http://www.justsoftwaresolutions.co.uk 15 Carrallack Mews, St Just, Cornwall, TR19 7UL, UK. Company No. 5478976