|
Boost : |
Subject: Re: [boost] [Boost.utility]
From: Andrew Chinkoff (achinkoff_at_[hidden])
Date: 2010-01-27 18:33:52
Rob, there is a test program I would to be explained to me:
*** BEGIN OF TEST PROGRAM ***
#include <stdio.h>
#include <boost/thread.hpp>
#define USE_SPINLOCKS
//#define USE_MUTEXES
boost::thread th1;
boost::thread th2;
boost::mutex mutex;
int global_int = 0;
bool IsNotEven(int code)
{
boost::mutex::scoped_lock locker(mutex);
bool ret = (code & 1);
return ret;
}
void thread_func()
{
for(int i = 0; i < 1000000; ++i)
{
#ifdef USE_MUTEXES
if(IsNotEven(global_int)) // (1)
#elif defined USE_SPINLOCKS
if(__sync_and_and_fetch(&global_int, 1)) // (2)
#else
ERROR__WRONG_COMPILED;
#endif
global_int+=9;
else
global_int+=1;
}
}
int main()
{
th1 = boost::thread(&thread_func);
th2 = boost::thread(&thread_func);
th1.join();
th2.join();
printf("global_int = %d\n", global_int);
return 0;
}
*** END OF TEST PROGRAM ***
Results of test program:
1) Test program compiled with USE_SPINLOCKS.
Outputs are always:
"global_int = 10"
2) Test program compiled with USE_MUTEXES.
Outputs differ from each other:
"global_int = 10000008"
"global_int = 10000000"
"global_int = 9763210"
Could you explain me why outputs produced with USE_SPINLOCKS compilation
differ from USE_MUTEXES ones?
-- View this message in context: http://old.nabble.com/-Boost.utility--tp27309940p27348736.html Sent from the Boost - Dev mailing list archive at Nabble.com.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk