#include
#include
using namespace std;
struct my_thread
{
my_thread (int i_) :i (i_) {}
void operator()()
{
//do_something;
}
private:
int i;
};
//using RAII
class thread_guard
{
public:
explicit thread_guard (thread &t_) :t (t_) {}
~thread_guard ()
{
if (t.joinable())
t.join ();
}
private:
thread_guard (const thread_guard &) = delete;
thread_guard &operator=(const thread_guard&) = delete;
thread &t;
};
int main ()
{
ios::sync_with_stdio (false);
thread t (my_thread(0));
thread_guard g (t);
t.join ();
return 0;
}
C++ 多线程入门1
最新推荐文章于 2014-11-11 22:54:37 发布