#include <boost/thread.hpp> #include <boost/bind.hpp> #include <iostream> void hello() { std::cout << "this is thread" << std::endl; } void name(const std::string& name) { std::cout << "this name is:" << name << std::endl; } int main(void) { boost::thread thrd1(&hello); boost::thread thrd2(boost::bind(name, "jone")); thrd1.join(); thrd2.join(); system("pause"); } 互斥体 ------------------------------------------------------------- #include <boost/thread.hpp> #include <boost/bind.hpp> #include <iostream> #include <windows.h> boost::mutex io_mutex; static int g_count = 0; void count1() { for (int i=0; i<10; i++) { boost::mutex::scoped_lock lock(io_mutex); std::cout << boost::this_thread::get_id() << ":" << ++g_count << std::endl; ::Sleep(10); // for test } } void count2() { for (int i=0; i<10; i++) { boost::mutex::scoped_lock lock(io_mutex); std::cout << boost::this_thread::get_id() << ":" << ++g_count << std::endl; ::Sleep(11); // for test } } int main(void) { boost::thread thrd1(&count1); boost::thread thrd2(&count2); thrd1.join(); thrd2.join(); system("pause"); } ----------------------------------------------------------- #include <boost/thread.hpp> #include <boost/bind.hpp> #include <iostream> #include <windows.h> boost::mutex io_mutex; static int g_count = 0; void count(int num) { for (int i=0; i<num; i++) { boost::mutex::scoped_lock lock(io_mutex); std::cout << boost::this_thread::get_id() << ":" << ++g_count << std::endl; ::Sleep(100); // for test } } int main(void) { boost::thread thrd1(boost::bind(count, 10)); boost::thread thrd2(boost::bind(count, 20)); thrd1.join(); thrd2.join(); system("pause"); } 条件变量 ----------------------------------------------------------- #include <boost/circular_buffer.hpp> #include <boost/thread/mutex.hpp> #include <boost/thread/condition.hpp> #include <boost/thread/thread.hpp> #include <boost/progress.hpp> #include <boost/bind.hpp> template<class T> class bounded_buffer { public: typedef boost::circular_buffer<T> container_type; typedef typename container_type::size_type size_type; typedef typename container_type::value_type value_type; explicit bounded_buffer(size_type capacity) : unread_(0), container_(capacity) {} void push_front(const value_type& item) { boost::mutex::scoped_lock lock(mutex_); // is_not_full为真继续往下执行,否则等待 not_full_.wait(lock, boost::bind(&bounded_buffer<value_type>::is_not_full, this)); container_.push_front(item); ++unread_; lock.unlock(); not_empty_.notify_one(); // 注意notify_one不能放在锁里面 } void pop_back(value_type* pItem) { boost::mutex::scoped_lock lock(mutex_); not_empty_.wait(lock, boost::bind(&bounded_buffer<value_type>::is_not_empty, this)); *pItem = container_[--unread_]; lock.unlock(); not_full_.notify_one(); } private: bounded_buffer(const bounded_buffer&); bounded_buffer& operator=(const bounded_buffer&); bool is_not_empty() const { return unread_ > 0; } bool is_not_full() const { return unread_ < container_.capacity(); } size_type unread_; container_type container_; boost::mutex mutex_; boost::condition not_empty_; boost::condition not_full_; }; int main(void) { bounded_buffer<int> buffer(3); buffer.push_front(1); buffer.push_front(2); buffer.push_front(3); int item; buffer.pop_back(&item); std::cout << item << std::endl; system("pause"); }
#include <iostream> #include <boost/thread/thread.hpp> #include <boost/thread/mutex.hpp> #include <boost/thread/condition.hpp> #include <boost/thread/xtime.hpp> #include <boost/bind.hpp> #include <ctime> #include <windows.h> #include <vector> boost::mutex mut; boost::condition cond; bool data_ready; bool is_data_ready() { return (data_ready == true); } void prepare_data() { std::cout << "prepare data" << std::endl; } void process_data() { std::cout << "process data" << std::endl; } void wait_for_data_to_process() { boost::mutex::scoped_lock lock(mut); cond.wait(lock, boost::bind(is_data_ready)); // 或者用下面四行替换 //while (!data_ready) //{ // cond.wait(lock); //} process_data(); } void prepare_data_for_processing() { prepare_data(); { boost::lock_guard<boost::mutex> lock(mut); data_ready = true; } cond.notify_one(); } int main() { boost::thread th1(wait_for_data_to_process); boost::thread th2(prepare_data_for_processing); th1.join(); th2.join(); system("pause"); return 0; } ------------------------------------------------------- #include <boost/circular_buffer.hpp> #include <boost/thread/mutex.hpp> #include <boost/thread/condition.hpp> #include <boost/thread/thread.hpp> #include <boost/progress.hpp> #include <boost/bind.hpp> template<class T> class bounded_buffer { public: typedef boost::circular_buffer<T> container_type; typedef typename container_type::size_type size_type; typedef typename container_type::value_type value_type; explicit bounded_buffer(size_type capacity) : unread_(0), container_(capacity) {} void push_front(const value_type& item) { boost::mutex::scoped_lock lock(mutex_); // is_not_full为真继续往下执行,否则等待 not_full_.wait(lock, boost::bind(&bounded_buffer<value_type>::is_not_full, this)); container_.push_front(item); ++unread_; lock.unlock(); not_empty_.notify_one(); // 注意notify_one不能放在锁里面 } void pop_back(value_type* pItem) { boost::mutex::scoped_lock lock(mutex_); not_empty_.wait(lock, boost::bind(&bounded_buffer<value_type>::is_not_empty, this)); *pItem = container_[--unread_]; lock.unlock(); not_full_.notify_one(); } private: bounded_buffer(const bounded_buffer&); bounded_buffer& operator=(const bounded_buffer&); bool is_not_empty() const { return unread_ > 0; } bool is_not_full() const { return unread_ < container_.capacity(); } size_type unread_; container_type container_; boost::mutex mutex_; boost::condition not_empty_; boost::condition not_full_; }; int main(void) { bounded_buffer<int> buffer(3); buffer.push_front(1); buffer.push_front(2); buffer.push_front(3); int item; buffer.pop_back(&item); std::cout << item << std::endl; system("pause"); }