// 忽略警告
#define _SCL_SECURE_NO_WARNINGS
#pragma warning(disable : 4996)
#include <assert.h>
#include <iostream>
#include <boost/thread.hpp>
using namespace boost;
using namespace std;
#include <boost/detail/atomic_count.hpp>
// 自定义实现原子操作的计数器
template<typename T>
class basic_atom :noncopyable
{
private:
T n;
typedef mutex mutex_t;
mutex_t mu;
public:
basic_atom(T x = T()) : n(x){}
T operator++()
{
mutex_t::scoped_lock lock(mu);
return ++n;
}
operator T(){ return n; }
};
typedef basic_atom<int> atom_int;
mutex io_mu;
void print(atom_int& x, const string& str)
{
try
{
for (int i = 0; i < 1000; ++i)
{
//this_thread::sleep(posix_time::seconds(1));
mutex::scoped_lock(io_mu);
cout << str << ":" << ++x << endl; // 锁定io流操作
this_thread::interruption_point(); // 允许中断
}
}
catch (thread_interrupted& )
{
cout << "thread_interrupted" << endl;
}
}
void print1(atom_int& x, const string& str)
{
try
{
for (int i = 0; i < 1000; ++i)
{
this_thread::sleep(posix_time::seconds(1));
mutex::scoped_lock(io_mu);
cout << str << ":" << ++x << endl; // 锁定io流操作
}
}
catch (thread_interrupted&)
{
cout << "thread_interrupted" << endl;
}
}
void to_interrupt(atom_int& x, const string& str)
{
try
{
using namespace this_thread;
assert(interruption_enabled()); // 检测当前线程是否允许中断(此时允许中断)
for (int i = 0; i < 5; ++i)
{
disable_interruption di; // 关闭中断
assert(!interruption_enabled());// 此时中断不可用
mutex::scoped_lock(io_mu);
cout << str << ":" << ++x << endl; // 锁定io流操作
cout << this_thread::interruption_requested() << endl;// 1 检测当前线程是否被要求中断
this_thread::interruption_point(); // 中断被禁用
assert(!interruption_enabled()); // 此时中断依旧不可用
restore_interruption ri(di); // 临时恢复中断
assert(interruption_enabled()); // 此时中断可用
cout << this_thread::interruption_requested() << endl;// 1
this_thread::interruption_point(); // 中断
}
}
catch (thread_interrupted&)
{
cout << "thread_interrupted" << endl;
}
}
int main()
{
// system_time 等同于ptime
// 互斥量
mutex mu;
try
{
mu.lock(); // 锁定互斥量
cout << "do something!!!" << endl; // 临界区操作
mu.unlock(); // 解锁互斥量
}
catch (...)
{
mu.unlock();
}
// 防止使用互斥量期间发生异常等退出作用域,跳过unlock导致未解除锁定
mutex::scoped_lock lock(mu); // 使用RAII型的lock_guard,在构造时锁定,析构时解锁
cout << "do something!!!!" << endl;
// 自定义计数类basic_atom
atom_int x(0);
cout << ++x << endl;
cout <<(int)x << endl;
// boost库中用long作为计数类型
detail::atomic_count ac(10);
cout << ac++ << endl;
cout << --ac << endl;
// 线程对象thread类(缺省情况下线程都允许中断)
//atom_int ai1(0);
//atom_int ai2(0);
//thread t1(print, boost::ref(ai1), "thread1");
//cout << t1.get_id() << endl;
//assert(t1.joinable());
//t1.timed_join(posix_time::seconds(3)); // 最多等待3秒人后返回(由于循环设置了10000,可能会在执行一半就返回)
//cout << t1.get_id() << endl;// 执行完线程体后为{Not - any - thread}\即thread::id()
//thread t2(boost::bind(print, boost::ref(ai2), "thread2"));
//cout << t2.get_id() << endl;
//t2.join();// 一直阻塞等待,直到线程结束(打印10000条)
//cout << t2.get_id() << endl;// 执行完线程体后为{Not - any - thread}\即thread::id()
或者thread.detach();与线程执行体分离,但线程继续运行
//assert(t2.get_id() == thread::id());
//cout << (int)ai1 << endl;
//cout << (int)ai2 << endl;
//cout << thread::hardware_concurrency() << endl; // 4 获得硬件系统可并行的线程数量
//thread::sleep(get_system_time() + posix_time::seconds(5));// 让当前线程睡眠5秒钟
//atom_int ai3(0);
//thread t3(print, boost::ref(ai3), "thread3");
this_thread::sleep(posix_time::seconds(10));
//t3.interrupt(); // 中断执行
//t3.join(); // 线程中断,所以join()立即返回
//atom_int ai4(0);
//thread t4(to_interrupt, boost::ref(ai4), "thread4");
//t4.interrupt();
//t4.join();
// 线程组thread_group
thread_group tg1;
atom_int ai5(0);
tg1.create_thread(boost::bind(print1,boost::ref(ai5), "C++"));// 创建线程对象并运行
tg1.create_thread(boost::bind(print1, boost::ref(ai5), "boost"));// 创建线程对象并运行
this_thread::sleep(posix_time::seconds(10));
// tg1.join_all();// 等待tg1中的线程
tg1.interrupt_all();
return 0;
}