RE: [Boost-users] Mutexes (Was: (no subject))
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? Thank you in advance. Pshemek -----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Vladimir Prus Sent: 22 April 2005 13:13 To: boost-users@lists.boost.org Subject: [Boost-users] Mutexes (Was: (no subject)) Hi Przemyslaw, (btw, it's better to give some subject to postings).
I have found a very strange behaviour (for me at least!) in boost.Thread. The same dummy code: boost::mutex M;
void functionA(int j)
{
for(int i=0; i
{
boost::mutex::scoped_lock lock(M);
And it would be good if the code was properly indented.
sqrt(static_cast<double>(i));
void functionB(int j)
{
for(int i=0; i
{
boost::mutex::scoped_lock lock(M); ..... If we do not lock the mutex then both processors are used on 100%. However, in case the line is uncommented (we lock the mutex) only one processor is used.
If you lock the mutex, then only one thread can be inside loop body at the moment. Since the code outside of mutex is very small, most of the time only one thread will be running. So, it's reasonable that OS uses just one processor. - Volodya _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users -------------------------------------------------------- If you are not an intended recipient of this e-mail, please notify the sender, delete it and do not read, act upon, print, disclose, copy, retain or redistribute it. Click here for important additional terms relating to this e-mail. http://www.ml.com/email_terms/ --------------------------------------------------------
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.
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?
Ben has already given the answer to your question. I can only mention that you use mail client or NNTP client that does not preserve threading. So, every message from you is shows as a separate thread for me, making reading very inconvenient. Can you consider using some better client? On Windows, Thunderbird is known to work. On Linux, pretty much every mailer works fine, Thunderbird and KMail being two examples. You can also use gmane.org for posting. - Volodya
participants (3)
-
Ben Hutchings
-
Sliwa, Przemyslaw (London)
-
Vladimir Prus