I run the following program on a 32 cores computer:
#include<iostream>
#include<algorithm>
#include<boost/thread.hpp>
using namespace std;
boost::thread_group g;
boost::mutex _mtx;
class A{
public:
void foo()
{
for(int ix = 0; ix < 10000000; ++ix)
vec.push_back(ix);
sort(vec.rbegin(), vec.rend());
}
private:
vector<int> vec;
};
void thread_fun()
{
A a;
_mtx.lock(); //line 24
a.foo();
_mtx.unlock(); //line 26
}
int main()
{
g.add_thread(new boost::thread(thread_fun));
g.add_thread(new boost::thread(thread_fun)); //line 32
g.join_all();
}
- With lines 24, 26 and 32 commented it takes 9 seconds to complete.
- With only lines 24, 26 commented and 32 uncommented it takes also 9 seconds to complete.
- With no lines commented it takes 18 seconds to complete
I thought the two threads are independent and it doesn't matter whether there is a lock on line a.foo()
or not. But it does, why?