fix race condition in test_threads
This commit is contained in:
parent
921627010b
commit
76f32f0659
|
@ -33,32 +33,88 @@ POSSIBILITY OF SUCH DAMAGE.
|
||||||
#include <boost/bind.hpp>
|
#include <boost/bind.hpp>
|
||||||
#include <list>
|
#include <list>
|
||||||
#include "libtorrent/thread.hpp"
|
#include "libtorrent/thread.hpp"
|
||||||
|
#include <boost/smart_ptr/detail/atomic_count.hpp>
|
||||||
#include "test.hpp"
|
#include "test.hpp"
|
||||||
|
|
||||||
|
using boost::detail::atomic_count;
|
||||||
using namespace libtorrent;
|
using namespace libtorrent;
|
||||||
|
|
||||||
void fun(condition_variable* s, libtorrent::mutex* m, int i)
|
void fun(condition_variable* s, libtorrent::mutex* m, int* waiting, int i)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "thread %d waiting\n", i);
|
fprintf(stderr, "thread %d waiting\n", i);
|
||||||
libtorrent::mutex::scoped_lock l(*m);
|
libtorrent::mutex::scoped_lock l(*m);
|
||||||
|
*waiting += 1;
|
||||||
s->wait(l);
|
s->wait(l);
|
||||||
fprintf(stderr, "thread %d done\n", i);
|
fprintf(stderr, "thread %d done\n", i);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void increment(condition_variable* s, libtorrent::mutex* m, int* waiting, atomic_count* c)
|
||||||
|
{
|
||||||
|
libtorrent::mutex::scoped_lock l(*m);
|
||||||
|
*waiting += 1;
|
||||||
|
s->wait(l);
|
||||||
|
l.unlock();
|
||||||
|
for (int i = 0; i < 1000000; ++i)
|
||||||
|
++*c;
|
||||||
|
}
|
||||||
|
|
||||||
|
void decrement(condition_variable* s, libtorrent::mutex* m, int* waiting, atomic_count* c)
|
||||||
|
{
|
||||||
|
libtorrent::mutex::scoped_lock l(*m);
|
||||||
|
*waiting += 1;
|
||||||
|
s->wait(l);
|
||||||
|
l.unlock();
|
||||||
|
for (int i = 0; i < 1000000; ++i)
|
||||||
|
--*c;
|
||||||
|
}
|
||||||
|
|
||||||
int test_main()
|
int test_main()
|
||||||
{
|
{
|
||||||
condition_variable cond;
|
condition_variable cond;
|
||||||
libtorrent::mutex m;
|
libtorrent::mutex m;
|
||||||
std::list<thread*> threads;
|
std::list<thread*> threads;
|
||||||
|
int waiting = 0;
|
||||||
for (int i = 0; i < 20; ++i)
|
for (int i = 0; i < 20; ++i)
|
||||||
{
|
{
|
||||||
threads.push_back(new thread(boost::bind(&fun, &cond, &m, i)));
|
threads.push_back(new thread(boost::bind(&fun, &cond, &m, &waiting, i)));
|
||||||
}
|
}
|
||||||
|
|
||||||
// make sure all threads are waiting on the condition_variable
|
// make sure all threads are waiting on the condition_variable
|
||||||
sleep(10);
|
|
||||||
|
|
||||||
libtorrent::mutex::scoped_lock l(m);
|
libtorrent::mutex::scoped_lock l(m);
|
||||||
|
while (waiting < 20)
|
||||||
|
{
|
||||||
|
l.unlock();
|
||||||
|
sleep(10);
|
||||||
|
l.lock();
|
||||||
|
}
|
||||||
|
|
||||||
|
cond.notify_all();
|
||||||
|
l.unlock();
|
||||||
|
|
||||||
|
for (std::list<thread*>::iterator i = threads.begin(); i != threads.end(); ++i)
|
||||||
|
{
|
||||||
|
(*i)->join();
|
||||||
|
delete *i;
|
||||||
|
}
|
||||||
|
threads.clear();
|
||||||
|
|
||||||
|
waiting = 0;
|
||||||
|
atomic_count c(0);
|
||||||
|
for (int i = 0; i < 3; ++i)
|
||||||
|
{
|
||||||
|
threads.push_back(new thread(boost::bind(&increment, &cond, &m, &waiting, &c)));
|
||||||
|
threads.push_back(new thread(boost::bind(&decrement, &cond, &m, &waiting, &c)));
|
||||||
|
}
|
||||||
|
|
||||||
|
// make sure all threads are waiting on the condition_variable
|
||||||
|
l.lock();
|
||||||
|
while (waiting < 6)
|
||||||
|
{
|
||||||
|
l.unlock();
|
||||||
|
sleep(10);
|
||||||
|
l.lock();
|
||||||
|
}
|
||||||
|
|
||||||
cond.notify_all();
|
cond.notify_all();
|
||||||
l.unlock();
|
l.unlock();
|
||||||
|
|
||||||
|
@ -68,6 +124,8 @@ int test_main()
|
||||||
delete *i;
|
delete *i;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CHECK(c == 0);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue