diff options
Diffstat (limited to 'src/boost/libs/thread/tutorial')
-rw-r--r-- | src/boost/libs/thread/tutorial/bounded_buffer.cpp | 69 | ||||
-rw-r--r-- | src/boost/libs/thread/tutorial/counter.cpp | 28 | ||||
-rw-r--r-- | src/boost/libs/thread/tutorial/factorial.cpp | 32 | ||||
-rw-r--r-- | src/boost/libs/thread/tutorial/factorial2.cpp | 32 | ||||
-rw-r--r-- | src/boost/libs/thread/tutorial/factorial3.cpp | 36 | ||||
-rw-r--r-- | src/boost/libs/thread/tutorial/helloworld.cpp | 19 | ||||
-rw-r--r-- | src/boost/libs/thread/tutorial/helloworld2.cpp | 24 | ||||
-rw-r--r-- | src/boost/libs/thread/tutorial/helloworld3.cpp | 20 | ||||
-rw-r--r-- | src/boost/libs/thread/tutorial/once.cpp | 31 | ||||
-rw-r--r-- | src/boost/libs/thread/tutorial/tss.cpp | 36 |
10 files changed, 327 insertions, 0 deletions
diff --git a/src/boost/libs/thread/tutorial/bounded_buffer.cpp b/src/boost/libs/thread/tutorial/bounded_buffer.cpp new file mode 100644 index 000000000..276aaebf1 --- /dev/null +++ b/src/boost/libs/thread/tutorial/bounded_buffer.cpp @@ -0,0 +1,69 @@ +// Copyright (C) 2001-2003 +// William E. Kempf +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#include <boost/thread/condition.hpp> +#include <boost/thread/mutex.hpp> +#include <boost/thread/thread.hpp> +#include <iostream> +#include <vector> + +class bounded_buffer : private boost::noncopyable +{ +public: + typedef boost::mutex::scoped_lock lock; + bounded_buffer(int n) : begin(0), end(0), buffered(0), circular_buf(n) { } + void send (int m) { + lock lk(monitor); + while (buffered == circular_buf.size()) + buffer_not_full.wait(lk); + circular_buf[end] = m; + end = (end+1) % circular_buf.size(); + ++buffered; + buffer_not_empty.notify_one(); + } + int receive() { + lock lk(monitor); + while (buffered == 0) + buffer_not_empty.wait(lk); + int i = circular_buf[begin]; + begin = (begin+1) % circular_buf.size(); + --buffered; + buffer_not_full.notify_one(); + return i; + } +private: + int begin, end, buffered; + std::vector<int> circular_buf; + boost::condition buffer_not_full, buffer_not_empty; + boost::mutex monitor; +}; +bounded_buffer buf(2); + +void sender() { + int n = 0; + while (n < 100) { + buf.send(n); + std::cout << "sent: " << n << std::endl; + ++n; + } + buf.send(-1); +} + +void receiver() { + int n; + do { + n = buf.receive(); + std::cout << "received: " << n << std::endl; + } while (n != -1); // -1 indicates end of buffer +} + +int main() +{ + boost::thread thrd1(&sender); + boost::thread thrd2(&receiver); + thrd1.join(); + thrd2.join(); +} diff --git a/src/boost/libs/thread/tutorial/counter.cpp b/src/boost/libs/thread/tutorial/counter.cpp new file mode 100644 index 000000000..a5ca0b4d6 --- /dev/null +++ b/src/boost/libs/thread/tutorial/counter.cpp @@ -0,0 +1,28 @@ +// Copyright (C) 2001-2003 +// William E. Kempf +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#include <boost/thread/mutex.hpp> +#include <boost/thread/thread.hpp> +#include <iostream> + +boost::mutex mutex; +int counter=0; + +void change_count() +{ + boost::mutex::scoped_lock lock(mutex); + int i = ++counter; + std::cout << "count == " << i << std::endl; +} + +int main() +{ + const int num_threads = 4; + boost::thread_group thrds; + for (int i=0; i < num_threads; ++i) + thrds.create_thread(&change_count); + thrds.join_all(); +} diff --git a/src/boost/libs/thread/tutorial/factorial.cpp b/src/boost/libs/thread/tutorial/factorial.cpp new file mode 100644 index 000000000..9dd1001d9 --- /dev/null +++ b/src/boost/libs/thread/tutorial/factorial.cpp @@ -0,0 +1,32 @@ +// Copyright (C) 2001-2003 +// William E. Kempf +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#include <boost/thread/thread.hpp> +#include <iostream> + +class factorial +{ +public: + factorial(int x, int& res) : x(x), res(res) { } + void operator()() { res = calculate(x); } + int result() const { return res; } + +private: + int calculate(int x) { return x <= 1 ? 1 : x * calculate(x-1); } + +private: + int x; + int& res; +}; + +int main() +{ + int result; + factorial f(10, result); + boost::thread thrd(f); + thrd.join(); + std::cout << "10! = " << result << std::endl; +} diff --git a/src/boost/libs/thread/tutorial/factorial2.cpp b/src/boost/libs/thread/tutorial/factorial2.cpp new file mode 100644 index 000000000..c30421bc5 --- /dev/null +++ b/src/boost/libs/thread/tutorial/factorial2.cpp @@ -0,0 +1,32 @@ +// Copyright (C) 2001-2003 +// William E. Kempf +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#include <boost/thread/thread.hpp> +#include <boost/ref.hpp> +#include <iostream> + +class factorial +{ +public: + factorial(int x) : x(x), res(0) { } + void operator()() { res = calculate(x); } + int result() const { return res; } + +private: + int calculate(int x) { return x <= 1 ? 1 : x * calculate(x-1); } + +private: + int x; + int res; +}; + +int main() +{ + factorial f(10); + boost::thread thrd(boost::ref(f)); + thrd.join(); + std::cout << "10! = " << f.result() << std::endl; +} diff --git a/src/boost/libs/thread/tutorial/factorial3.cpp b/src/boost/libs/thread/tutorial/factorial3.cpp new file mode 100644 index 000000000..2515bfeab --- /dev/null +++ b/src/boost/libs/thread/tutorial/factorial3.cpp @@ -0,0 +1,36 @@ +// Copyright (C) 2001-2003 +// William E. Kempf +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#include <boost/thread/thread.hpp> +#include <iostream> + +const int NUM_CALCS=5; + +class factorial +{ +public: + factorial(int x, int& res) : x(x), res(res) { } + void operator()() { res = calculate(x); } + int result() const { return res; } + +private: + int calculate(int x) { return x <= 1 ? 1 : x * calculate(x-1); } + +private: + int x; + int& res; +}; + +int main() +{ + int results[NUM_CALCS]; + boost::thread_group thrds; + for (int i=0; i < NUM_CALCS; ++i) + thrds.create_thread(factorial(i*10, results[i])); + thrds.join_all(); + for (int j=0; j < NUM_CALCS; ++j) + std::cout << j*10 << "! = " << results[j] << std::endl; +} diff --git a/src/boost/libs/thread/tutorial/helloworld.cpp b/src/boost/libs/thread/tutorial/helloworld.cpp new file mode 100644 index 000000000..5003108f4 --- /dev/null +++ b/src/boost/libs/thread/tutorial/helloworld.cpp @@ -0,0 +1,19 @@ +// Copyright (C) 2001-2003 +// William E. Kempf +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#include <boost/thread/thread.hpp> +#include <iostream> + +void helloworld() +{ + std::cout << "Hello World!" << std::endl; +} + +int main() +{ + boost::thread thrd(&helloworld); + thrd.join(); +} diff --git a/src/boost/libs/thread/tutorial/helloworld2.cpp b/src/boost/libs/thread/tutorial/helloworld2.cpp new file mode 100644 index 000000000..dc7a698a8 --- /dev/null +++ b/src/boost/libs/thread/tutorial/helloworld2.cpp @@ -0,0 +1,24 @@ +// Copyright (C) 2001-2003 +// William E. Kempf +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#include <boost/thread/thread.hpp> +#include <iostream> + +struct helloworld +{ + helloworld(const char* who) : m_who(who) { } + void operator()() + { + std::cout << m_who << "says, \"Hello World.\"" << std::endl; + } + const char* m_who; +}; + +int main() +{ + boost::thread thrd(helloworld("Bob")); + thrd.join(); +} diff --git a/src/boost/libs/thread/tutorial/helloworld3.cpp b/src/boost/libs/thread/tutorial/helloworld3.cpp new file mode 100644 index 000000000..239ed8fe2 --- /dev/null +++ b/src/boost/libs/thread/tutorial/helloworld3.cpp @@ -0,0 +1,20 @@ +// Copyright (C) 2001-2003 +// William E. Kempf +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#include <boost/thread/thread.hpp> +#include <boost/bind/bind.hpp> +#include <iostream> + +void helloworld(const char* who) +{ + std::cout << who << "says, \"Hello World.\"" << std::endl; +} + +int main() +{ + boost::thread thrd(boost::bind(&helloworld, "Bob")); + thrd.join(); +} diff --git a/src/boost/libs/thread/tutorial/once.cpp b/src/boost/libs/thread/tutorial/once.cpp new file mode 100644 index 000000000..5a5b6f558 --- /dev/null +++ b/src/boost/libs/thread/tutorial/once.cpp @@ -0,0 +1,31 @@ +// Copyright (C) 2001-2003 +// William E. Kempf +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#include <boost/thread/thread.hpp> +#include <boost/thread/once.hpp> +#include <cassert> + +int value=0; +boost::once_flag once = BOOST_ONCE_INIT; + +void init() +{ + ++value; +} + +void thread_proc() +{ + boost::call_once(&init, once); +} + +int main(int argc, char* argv[]) +{ + boost::thread_group threads; + for (int i=0; i<5; ++i) + threads.create_thread(&thread_proc); + threads.join_all(); + assert(value == 1); +} diff --git a/src/boost/libs/thread/tutorial/tss.cpp b/src/boost/libs/thread/tutorial/tss.cpp new file mode 100644 index 000000000..f867a9180 --- /dev/null +++ b/src/boost/libs/thread/tutorial/tss.cpp @@ -0,0 +1,36 @@ +// Copyright (C) 2001-2003 +// William E. Kempf +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#include <boost/thread/thread.hpp> +#include <boost/thread/tss.hpp> +#include <cassert> + +boost::thread_specific_ptr<int> value; + +void increment() +{ + int* p = value.get(); + ++*p; +} + +void thread_proc() +{ + value.reset(new int(0)); // initialize the thread's storage + for (int i=0; i<10; ++i) + { + increment(); + int* p = value.get(); + assert(*p == i+1); + } +} + +int main(int argc, char* argv[]) +{ + boost::thread_group threads; + for (int i=0; i<5; ++i) + threads.create_thread(&thread_proc); + threads.join_all(); +} |