Sliwa, Przemyslaw (London) wrote:
Volodya,
Thanks for answer. Right I have forgotten to put the subject->it was stupid. But the try_mutex and scoped_tyy_lock cause that both processors are used properly.
Can you explain? <snip>
You don't seem to understand what mutexes are. They do not magically make regions of code thread-safe. They provide _mut_ual _ex_clusion; that is, they allow only one thread at a time to acquire them. Since virtually all the work of the program depends on acquisition of a single mutex, only one thread can run for most of the time, and only one processor will be used. The try_mutex/scoped_try_lock combination allows you to attempt to acquire the mutex without blocking; obviously this can fail. If you don't test for failure, you might as well not use the mutex at all. In your example of calculating square roots there is no shared data and no need for the mutexes. In a real multithreaded program the challenge is to organise shared data and mutexes so that threads rarely contend for the same mutex. Ben.