From 83ff1cfb6e933cc70a5c3c8f331f92c941325188 Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Fri, 7 Oct 2005 12:57:20 +0000 Subject: [PATCH] Playing with boost threads and unit test --- src/test_boost_threads.cpp | 40 ++++++++++++++++++++++++++++++++++++++-- src/test_filter1.cpp | 26 +++++++++++--------------- 2 files changed, 49 insertions(+), 17 deletions(-) diff --git a/src/test_boost_threads.cpp b/src/test_boost_threads.cpp index 8a26cab..08af856 100644 --- a/src/test_boost_threads.cpp +++ b/src/test_boost_threads.cpp @@ -1,6 +1,7 @@ #include #include +#include #include boost::mutex io_mutex; // The iostreams are not guaranteed to be thread-safe! @@ -20,6 +21,7 @@ class counter int count; }; + counter c; void change_count() @@ -30,6 +32,21 @@ void change_count() } +class worker { +public: + void operator() (void) { + int i = c.increment(); + + i = c.increment(); + + i = c.increment(); + boost::mutex::scoped_lock scoped_lock(io_mutex); + std::cout << "count == " << i << std::endl; + } + virtual ~worker() { std::cout << "destroyed\n"; } +}; + + int main(int, char*[]) { @@ -37,11 +54,30 @@ int main(int, char*[]) { const int num_threads = 4; boost::thread_group thrds; + + std::list thread_list; + for (int i=0; i < num_threads; ++i) - thrds.create_thread(&change_count); + { + // thrds.create_thread(&change_count); + worker *w = new worker; + + boost::thread *thr = new boost::thread(*w); + + thrds.add_thread(thr); + + thread_list.push_back(thr); + } thrds.join_all(); - +#if 0 + std::list::iterator it; + for (it = thread_list.begin(); it != thread_list.end(); it++) + { + delete *it; + *it = 0; + } +#endif } catch (std::exception &e) { diff --git a/src/test_filter1.cpp b/src/test_filter1.cpp index 432cbd3..1d1b02b 100644 --- a/src/test_filter1.cpp +++ b/src/test_filter1.cpp @@ -5,8 +5,8 @@ //#include "router.hpp" //#include "package.hpp" - #define BOOST_AUTO_TEST_MAIN - #include +#define BOOST_AUTO_TEST_MAIN +#include //#include //#include @@ -23,27 +23,23 @@ public: BOOST_AUTO_TEST_CASE( test1 ) { - try{ - TFilter filter; - - filter.name("filter1"); - - BOOST_CHECK (filter.name() == "filter1"); - - filter.name() = "filter1 rename"; - - BOOST_CHECK(filter.name() == "filter1 rename"); + TFilter filter; + + filter.name("filter1"); + + BOOST_CHECK (filter.name() == "filter1"); + + filter.name() = "filter1 rename"; + + BOOST_CHECK(filter.name() == "filter1 rename"); } - catch(std::runtime_error &e ){ BOOST_CHECK (true); } catch ( ...) { BOOST_CHECK (false); } - - } /* -- 1.7.10.4