Exemplo n.º 1
0
	void doTest() {
//		boost::thread::id a = boost::this_thread::get_id();
		boost::thread thread0(func0);
		boost::thread thread1(func1);

		//boost::recursive_mutex::scoped_lock autolock(mutex);
		thread0.join();
		thread1.join();
		printf("ThreadTest OK..\n");
	}
      void perform_test_trivial() {

        { // running independent counters sequentially (???)
          typedef Counter<0> counter0;
          typedef typename counter0::uint_type uint_type_0;
          const uint_type_0 count0 = 10000000U;
          {
            const counter0 c0(count0);
            OKLIB_TEST_EQUAL(counter0::counter(), count0);
            c0();
            OKLIB_TEST_EQUAL(counter0::counter(), 0);
          }
          const counter0 c0(count0);
          OKLIB_TEST_EQUAL(counter0::counter(), count0);
          
          typedef Counter<1> counter1;
          typedef typename counter1::uint_type uint_type_1;
          const uint_type_1 count1 = 10000001U;
          const counter1 c1(count1);
          OKLIB_TEST_EQUAL(counter1::counter(), count1);

          //independent counter c0, c1 initialised
          boost::thread thread0(c0);
          boost::thread thread1(c1);
          // now both counters are counting down in parallel threads
          OKLIB_TEST_NOTEQUAL(thread0, thread1);
          {
            boost::thread thread;
            OKLIB_TEST_NOTEQUAL(thread0, thread);
            OKLIB_TEST_NOTEQUAL(thread1, thread);
          }
          
          thread0.join();
          thread1.join();
          
          OKLIB_TEST_EQUAL(counter0::counter(), 0);
          OKLIB_TEST_EQUAL(counter1::counter(), 0);

        }

        {
          boost::mutex mutex;

          typedef CounterWithMutex<0> counter0;
          typedef typename counter0::uint_type uint_type_0;
          const uint_type_0 count0 = 10000000U;
          const counter0 c0(count0, mutex);
          OKLIB_TEST_EQUAL(counter0::counter(), count0);
          
          typedef CounterWithMutex<1> counter1;
          typedef typename counter1::uint_type uint_type_1;
          const uint_type_1 count1 = 10000001U;
          const counter1 c1(count1, mutex);
          OKLIB_TEST_EQUAL(counter1::counter(), count1);

          boost::thread thread0(c0);
          boost::thread thread1(c1);
          OKLIB_TEST_NOTEQUAL(thread0, thread1);

          {
            boost::mutex::scoped_lock lock(mutex);
          }
          OKLIB_TEST_EQUAL(counter0::counter(), 0);
          OKLIB_TEST_EQUAL(counter1::counter(), 0);
        }

        { // NOW parallelism
          boost::mutex mutex0;
          typedef CounterWithMutex<0> counter0;
          typedef typename counter0::uint_type uint_type_0;
          const uint_type_0 count0 = 10000000U;
          const counter0 c0(count0, mutex0);
          
          boost::mutex mutex1;
          typedef CounterWithMutex<1> counter1;
          typedef typename counter1::uint_type uint_type_1;
          const uint_type_1 count1 = 10000001U;
          const counter1 c1(count1, mutex1);
          
          {
            boost::thread thread0(c0);
            boost::thread thread1(c1);
            boost::mutex::scoped_lock lock0(mutex0);
            boost::mutex::scoped_lock lock1(mutex1);
          }
          OKLIB_TEST_EQUAL(counter0::counter(), 0);
          OKLIB_TEST_EQUAL(counter1::counter(), 0);
        }
      }