Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r76296 - trunk/libs/thread/test
From: vicente.botet_at_[hidden]
Date: 2012-01-03 16:26:00


Author: viboes
Date: 2012-01-03 16:25:58 EST (Tue, 03 Jan 2012)
New Revision: 76296
URL: http://svn.boost.org/trac/boost/changeset/76296

Log:
Thread: Added test related to tickets
Added:
   trunk/libs/thread/test/test_2501.cpp (contents, props changed)
   trunk/libs/thread/test/test_4521.cpp (contents, props changed)
   trunk/libs/thread/test/test_4648.cpp (contents, props changed)
   trunk/libs/thread/test/test_4882.cpp (contents, props changed)
   trunk/libs/thread/test/test_5351.cpp (contents, props changed)
   trunk/libs/thread/test/test_5502.cpp (contents, props changed)
   trunk/libs/thread/test/test_5542_1.cpp (contents, props changed)
   trunk/libs/thread/test/test_5542_2.cpp (contents, props changed)
   trunk/libs/thread/test/test_5542_3.cpp (contents, props changed)
   trunk/libs/thread/test/test_6130.cpp (contents, props changed)
   trunk/libs/thread/test/test_6174.cpp (contents, props changed)

Added: trunk/libs/thread/test/test_2501.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/thread/test/test_2501.cpp 2012-01-03 16:25:58 EST (Tue, 03 Jan 2012)
@@ -0,0 +1,10 @@
+#include <boost/thread/shared_mutex.hpp>
+
+int main() {
+
+ boost::shared_mutex mtx; boost::upgrade_lock<boost::shared_mutex> lk(mtx);
+
+ boost::upgrade_to_unique_lock<boost::shared_mutex> lk2(lk);
+
+ return 0;
+}

Added: trunk/libs/thread/test/test_4521.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/thread/test/test_4521.cpp 2012-01-03 16:25:58 EST (Tue, 03 Jan 2012)
@@ -0,0 +1,21 @@
+#include <boost/thread.hpp>
+
+int calculate_the_answer_to_life_the_universe_and_everything()
+{
+ return 42;
+}
+
+int main() {
+boost::packaged_task<int> pt(calculate_the_answer_to_life_the_universe_and_everything);
+boost::unique_future<int> fi=pt.get_future();
+
+boost::thread task(boost::move(pt)); // launch task on a thread
+
+fi.wait(); // wait for it to finish
+
+//assert(fi.is_ready());
+//assert(fi.has_value());
+//assert(!fi.has_exception());
+//assert(fi.get_state()==boost::future_state::ready);
+//assert(fi.get()==42);
+}

Added: trunk/libs/thread/test/test_4648.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/thread/test/test_4648.cpp 2012-01-03 16:25:58 EST (Tue, 03 Jan 2012)
@@ -0,0 +1,42 @@
+#include <iostream>
+#include <boost/thread.hpp>
+#include <boost/current_function.hpp>
+
+class boostThreadLocksTest
+{
+public:
+ boost::shared_mutex myMutex;
+ //boost::upgrade_lock<boost::shared_mutex> myLock;
+ static int firstFunction(boostThreadLocksTest *pBoostThreadLocksTest);
+ static int secondFunction(boostThreadLocksTest *pBoostThreadLocksTest,
+ boost::upgrade_lock<boost::shared_mutex>& upgr);
+ boostThreadLocksTest()
+ :myMutex()
+ //, myLock(myMutex,boost::defer_lock_t())
+ {};
+};
+
+int boostThreadLocksTest::firstFunction(boostThreadLocksTest *pBoostThreadLocksTest)
+{
+ std::cout<<"Entering "<<boost::this_thread::get_id()<<" "<<"firstFunction"<<std::endl;
+ boost::upgrade_lock<boost::shared_mutex> myLock(pBoostThreadLocksTest->myMutex);
+ pBoostThreadLocksTest->secondFunction(pBoostThreadLocksTest, myLock);
+ std::cout<<"Returned From Call "<<boost::this_thread::get_id()<<" "<<"firstFunction"<<std::endl;
+ std::cout<<"Returning from "<<boost::this_thread::get_id()<<" "<<"firstFunction"<<std::endl;
+ return(0);
+}
+int boostThreadLocksTest::secondFunction(boostThreadLocksTest *pBoostThreadLocksTest, boost::upgrade_lock<boost::shared_mutex>& upgr) {
+ std::cout<<"Before Exclusive Locking "<<boost::this_thread::get_id()<<" "<<"secondFunction"<<std::endl;
+ boost::upgrade_to_unique_lock<boost::shared_mutex> localUniqueLock(upgr);
+ std::cout<<"After Exclusive Locking "<<boost::this_thread::get_id()<<" "<<"secondFunction"<<std::endl;
+ return(0);
+}
+int main() {
+ boostThreadLocksTest myObject;
+ boost::thread_group myThreadGroup;
+ myThreadGroup.create_thread(boost::bind(boostThreadLocksTest::firstFunction,&myObject));
+ myThreadGroup.create_thread(boost::bind(boostThreadLocksTest::firstFunction,&myObject));
+ myThreadGroup.create_thread(boost::bind(boostThreadLocksTest::firstFunction,&myObject));
+ myThreadGroup.join_all();
+ return 0;
+}

Added: trunk/libs/thread/test/test_4882.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/thread/test/test_4882.cpp 2012-01-03 16:25:58 EST (Tue, 03 Jan 2012)
@@ -0,0 +1,50 @@
+#include <boost/thread/thread.hpp>
+#include <boost/thread/shared_mutex.hpp>
+
+#include <iostream>
+
+boost::shared_mutex mutex;
+
+void thread()
+{
+ std::cout << __FILE__ << ":" << __LINE__ << std::endl;
+ try
+ {
+ for (int i =0; i<10; ++i)
+ {
+ boost::system_time timeout = boost::get_system_time() + boost::posix_time::milliseconds(50);
+
+ if (mutex.timed_lock(timeout))
+ {
+ std::cout << __FILE__ << ":" << __LINE__ << std::endl;
+ boost::this_thread::sleep(boost::posix_time::milliseconds(10));
+ mutex.unlock();
+ std::cout << __FILE__ << ":" << __LINE__ << std::endl;
+ }
+ }
+ }
+ catch (boost::lock_error& le)
+ {
+ std::cerr << "lock_error exception\n";
+ }
+ std::cout << __FILE__ << ":" << __LINE__ << std::endl;
+}
+
+int main()
+{
+ std::cout << __FILE__ << ":" << __LINE__ << std::endl;
+ const int nrThreads = 20;
+ boost::thread* threads[nrThreads];
+
+ for (int i = 0; i < nrThreads; ++i)
+ threads[i] = new boost::thread(&thread);
+
+ for (int i = 0; i < nrThreads; ++i)
+ {
+ threads[i]->join();
+ std::cout << __FILE__ << ":" << __LINE__ << std::endl;
+ delete threads[i];
+ }
+ std::cout << __FILE__ << ":" << __LINE__ << std::endl;
+ return 0;
+}

Added: trunk/libs/thread/test/test_5351.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/thread/test/test_5351.cpp 2012-01-03 16:25:58 EST (Tue, 03 Jan 2012)
@@ -0,0 +1,45 @@
+#include <iostream>
+#include <boost/thread.hpp>
+#include <boost/date_time/posix_time/posix_time_types.hpp>
+#include <boost/thread/future.hpp>
+
+using namespace boost::posix_time;
+using namespace boost;
+
+int foo()
+{
+ this_thread::sleep(seconds(10));
+ return 0;
+}
+
+
+int main(int argc, char** argv)
+{
+ boost::packaged_task<int> pt(&foo);
+ boost::unique_future<int> fi = pt.get_future();
+ boost::thread task(boost::move(pt)); // launch task on a thread
+
+ task.interrupt();
+
+ try
+ {
+ int v = fi.get();
+ }
+ catch (boost::thread_interrupted& exc)
+ {
+ std::cout << "OK: " << std::endl;
+ return 0;
+ }
+ catch (boost::exception& exc)
+ {
+ std::cout << __LINE__ << " ERROR: " << boost::diagnostic_information(exc) << std::endl;
+ return 1;
+ }
+ catch (...)
+ {
+ std::cout << __LINE__ << " ERROR: " << std::endl;
+ return 2;
+ }
+ std::cout << __LINE__ << " ERROR: " << std::endl;
+ return 3;
+}

Added: trunk/libs/thread/test/test_5502.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/thread/test/test_5502.cpp 2012-01-03 16:25:58 EST (Tue, 03 Jan 2012)
@@ -0,0 +1,86 @@
+// bm.cpp
+
+// g++ test.cpp -lboost_thread-mt && ./a.out
+
+// the ration of XXX and YYY determines
+// if this works or deadlocks
+int XXX = 20;
+int YYY = 10;
+
+#include <boost/thread.hpp>
+#include <boost/thread/shared_mutex.hpp>
+
+#include <unistd.h>
+#include <iostream>
+#include <boost/detail/lightweight_test.hpp>
+
+using namespace std;
+
+void sleepmillis(useconds_t miliis)
+{
+ usleep(miliis * 1000);
+}
+
+void worker1(boost::shared_mutex * lk, int * x)
+{
+ (*x)++; // 1
+ cout << "lock b try " << *x << endl;
+ while (1)
+ {
+ if (lk->timed_lock(boost::posix_time::milliseconds(XXX))) break;
+ sleepmillis(YYY);
+ }
+ cout << "lock b got " << *x << endl;
+ (*x)++; // 2
+ lk->unlock();
+}
+
+void worker2(boost::shared_mutex * lk, int * x)
+{
+ cout << "lock c try" << endl;
+ lk->lock_shared();
+ (*x)++;
+ cout << "lock c got" << endl;
+ lk->unlock_shared();
+ cout << "lock c unlocked" << endl;
+ (*x)++;
+}
+
+int main()
+{
+
+ // create
+ boost::shared_mutex* lk = new boost::shared_mutex();
+
+ // read lock
+ cout << "lock a" << endl;
+ lk->lock_shared();
+
+ int x1 = 0;
+ boost::thread t1(boost::bind(worker1, lk, &x1));
+ while (!x1)
+ ;
+ BOOST_TEST(x1 == 1);
+ sleepmillis(500);
+ BOOST_TEST(x1 == 1);
+
+ int x2 = 0;
+ boost::thread t2(boost::bind(worker2, lk, &x2));
+ t2.join();
+ BOOST_TEST(x2 == 2);
+
+ lk->unlock_shared();
+ cout << "unlock a" << endl;
+
+ for (int i = 0; i < 2000; i++)
+ {
+ if (x1 == 2) break;
+ sleepmillis(10);
+ }
+
+ BOOST_TEST(x1 == 2);
+ t1.join();
+ delete lk;
+
+ return 0;
+}

Added: trunk/libs/thread/test/test_5542_1.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/thread/test/test_5542_1.cpp 2012-01-03 16:25:58 EST (Tue, 03 Jan 2012)
@@ -0,0 +1,62 @@
+#include <iostream>
+#include <boost/thread.hpp>
+
+class Worker
+{
+public:
+
+ Worker()
+ {
+ // the thread is not-a-thread until we call start()
+ }
+
+ void start(int N)
+ {
+ std::cout << "start\n";
+ m_Thread = boost::thread(&Worker::processQueue, this, N);
+ std::cout << "started\n";
+ }
+
+ void join()
+ {
+ m_Thread.join();
+ }
+
+ void processQueue(unsigned N)
+ {
+ float ms = N * 1e3;
+ boost::posix_time::milliseconds workTime(ms);
+
+ std::cout << "Worker: started, will work for "
+ << ms << "ms"
+ << std::endl;
+
+ // We're busy, honest!
+ boost::this_thread::sleep(workTime);
+
+ std::cout << "Worker: completed" << std::endl;
+ }
+
+private:
+
+ boost::thread m_Thread;
+};
+
+int main(int argc, char* argv[])
+{
+ std::cout << "main: startup" << std::endl;
+
+ Worker worker;
+
+ std::cout << "main: create worker" << std::endl;
+
+ worker.start(3);
+
+ std::cout << "main: waiting for thread" << std::endl;
+
+ worker.join();
+
+ std::cout << "main: done" << std::endl;
+
+ return 0;
+}

Added: trunk/libs/thread/test/test_5542_2.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/thread/test/test_5542_2.cpp 2012-01-03 16:25:58 EST (Tue, 03 Jan 2012)
@@ -0,0 +1,11 @@
+#include <boost/thread.hpp>
+
+void run_thread() {
+ return;
+}
+
+int main() {
+ boost::thread t(run_thread);
+ return 0;
+}
+

Added: trunk/libs/thread/test/test_5542_3.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/thread/test/test_5542_3.cpp 2012-01-03 16:25:58 EST (Tue, 03 Jan 2012)
@@ -0,0 +1,30 @@
+#include <iostream>
+#include <boost/thread.hpp>
+#include <boost/date_time.hpp>
+
+void workerFunc()
+{
+ boost::posix_time::seconds workTime(3);
+
+ std::cout << "Worker: running" << std::endl;
+
+ // Pretend to do something useful...
+ boost::this_thread::sleep(workTime);
+
+ std::cout << "Worker: finished" << std::endl;
+}
+
+int main(int argc, char* argv[])
+{
+ std::cout << "main: startup" << std::endl;
+
+ boost::thread workerThread(workerFunc);
+
+ std::cout << "main: waiting for thread" << std::endl;
+
+ workerThread.join();
+
+ std::cout << "main: done" << std::endl;
+
+ return 0;
+}

Added: trunk/libs/thread/test/test_6130.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/thread/test/test_6130.cpp 2012-01-03 16:25:58 EST (Tue, 03 Jan 2012)
@@ -0,0 +1,22 @@
+#include <boost/thread.hpp>
+#include <assert.h>
+#include <iostream>
+#include <stdlib.h>
+#include <unistd.h>
+
+boost::mutex mtx;
+boost::condition_variable cv;
+
+int main()
+{
+ for (int i=0; i<3; ++i) {
+ const time_t wait_time = ::time(0)+1;
+
+ boost::mutex::scoped_lock lk(mtx);
+ const bool res = cv.timed_wait(lk, boost::posix_time::from_time_t(wait_time));
+ const time_t end_time = ::time(0);
+ assert(end_time >= wait_time);
+ std::cerr << end_time - wait_time << " OK\n";
+ }
+ return 0;
+}

Added: trunk/libs/thread/test/test_6174.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/thread/test/test_6174.cpp 2012-01-03 16:25:58 EST (Tue, 03 Jan 2012)
@@ -0,0 +1,35 @@
+
+
+#include <boost/thread.hpp>
+#include <boost/config.hpp>
+
+#ifndef BOOST_NO_RVALUE_REFERENCES
+struct MovableButNonCopyable {
+#ifndef BOOST_NO_DEFAULTED_FUNCTIONS
+ MovableButNonCopyable() = default;
+ MovableButNonCopyable(MovableButNonCopyable const&) = delete;
+ MovableButNonCopyable& operator=(MovableButNonCopyable const&) = delete;
+ MovableButNonCopyable(MovableButNonCopyable&&) = default;
+ MovableButNonCopyable& operator=(MovableButNonCopyable&&) = default;
+#else
+ MovableButNonCopyable() {};
+ MovableButNonCopyable(MovableButNonCopyable&&) {};
+ MovableButNonCopyable& operator=(MovableButNonCopyable&&) {
+ return *this;
+ };
+private:
+ MovableButNonCopyable(MovableButNonCopyable const&);
+ MovableButNonCopyable& operator=(MovableButNonCopyable const&);
+#endif
+};
+int main()
+{
+ boost::packaged_task<MovableButNonCopyable>(MovableButNonCopyable());
+ return 0;
+}
+#else
+int main()
+{
+ return 0;
+}
+#endif


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk