boost::thread线程创建方式总结
2015-07-03 08:45
330 查看
首先看看boost::thread的构造函数吧,boost::thread有两个构造函数:
(1)thread():构造一个表示当前执行线程的线程对象;
(2)explicit thread(const boost::function0<void>& threadfunc):
boost::function0<void>可以简单看为:一个无返回(返回void),无参数的函数。
这里的函数也可以是类重载operator()构成的函数;
该构造函数传入的是函数对象而并非是函数指针,
这样一个具有一般函数特性的类也能作为参数传入,在下面有例子。
第一种方式:最简单方法
#include <boost/thread/thread.hpp>
#include <iostream>
void hello()
{
std::cout <<
"Hello world, I''m a thread!"
<< std::endl;
}
int main(int argc, char* argv[])
{
boost::thread thrd(&hello);
thrd.join();
return 0;
}
第二种方式:复杂类型对象作为参数来创建线程:
#include <boost/thread/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <iostream>
boost::mutex io_mutex;
struct count
{
count(int id) : id(id) { }
void operator()()
{
for (int i = 0; i < 10; ++i)
{
boost::mutex::scoped_lock
lock(io_mutex);
std::cout << id << ": " << i << std::endl;
}
}
int id;
};
int main(int argc, char* argv[])
{
boost::thread thrd1(count(1));
boost::thread thrd2(count(2));
thrd1.join();
thrd2.join();
return 0;
}
第三种方式:在类内部创建线程;
(1)类内部静态方法启动线程
#include <boost/thread/thread.hpp>
#include <iostream>
class HelloWorld
{
public:
static void hello()
{
std::cout << "Hello world, I''m a thread!" << std::endl;
}
static void start()
{
boost::thread thrd( hello );
thrd.join();
}
};
int main(int argc, char* argv[])
{
HelloWorld::start();
return 0;
} //在这里start()和hello()方法都必须是static方法。
(2)如果要求start()和hello()方法不能是静态方法则采用下面的方法创建线程:
#include <boost/thread/thread.hpp>
#include <boost/bind.hpp>
#include <iostream>
class HelloWorld
{
public:
void hello()
{
std::cout << "Hello world, I''m a thread!" << std::endl;
}
void start()
{
boost::function0< void> f = boost::bind(&HelloWorld::hello,this);
boost::thread thrd( f );
thrd.join();
}
};
int main(int argc, char* argv[])
{
HelloWorld hello;
hello.start();
return 0;
}
---------------------------------------------------------------------
线程ID从boost::thread中分离出来,因此一个线程可以获得它自己的ID(使用boost::this_thread::get_id()),
并且这些ID可以用作关联容器的键值。
主线程,子线程 , 线程, 资源的问题,线程同步问题
1.创建线程
2.使资源是线程安全的
保证同一时刻多个线程不会同时修改同一个共享资源,那么这个程序是线程安全的。
可以使用mutex类来控制线程的并发问题。
#include <boost/thread/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <iostream>
#include <string>
template<typename T>
class CQueue
{
public:
CQueue() {}
~CQueue() {}
void enqueue(const T& x)
{
boost::mutex::scoped_lock lock(mutex_);
list_.push_back(x);
std::cout << "enqueue" << std::endl;
}
T dequeue()
{
boost::mutex::scoped_lock lock(mutex_);
if(list_.empty())
throw "empty!";
T tmp = list_.front();
list_.pop_front();
std::cout << "dequeue" << std::endl;
return tmp;
}
private:
std::list<T> list_;
boost::mutex mutex_;
};
CQueue<std::string> queue_string;
void send_something()
{
for(int i=0; i<4; ++i)
queue_string.enqueue("Cyrus");
}
void recv_something()
{
std::string s;
for(int i=0; i<4; ++i)
{
try
{
s = queue_string.dequeue();
}
catch(...){}
}
}
int main(int argc, char* argv[])
{
boost::thread thrd1(send_something);
boost::thread thrd2(recv_something);
thrd1.join();
thrd2.join();
return 0;
}
在某一时刻,只有一个线程可以锁定这个mutex对象。
这就阻止了同一时刻多个线程并发访问共享资源。
(1)thread():构造一个表示当前执行线程的线程对象;
(2)explicit thread(const boost::function0<void>& threadfunc):
boost::function0<void>可以简单看为:一个无返回(返回void),无参数的函数。
这里的函数也可以是类重载operator()构成的函数;
该构造函数传入的是函数对象而并非是函数指针,
这样一个具有一般函数特性的类也能作为参数传入,在下面有例子。
第一种方式:最简单方法
#include <boost/thread/thread.hpp>
#include <iostream>
void hello()
{
std::cout <<
"Hello world, I''m a thread!"
<< std::endl;
}
int main(int argc, char* argv[])
{
boost::thread thrd(&hello);
thrd.join();
return 0;
}
第二种方式:复杂类型对象作为参数来创建线程:
#include <boost/thread/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <iostream>
boost::mutex io_mutex;
struct count
{
count(int id) : id(id) { }
void operator()()
{
for (int i = 0; i < 10; ++i)
{
boost::mutex::scoped_lock
lock(io_mutex);
std::cout << id << ": " << i << std::endl;
}
}
int id;
};
int main(int argc, char* argv[])
{
boost::thread thrd1(count(1));
boost::thread thrd2(count(2));
thrd1.join();
thrd2.join();
return 0;
}
第三种方式:在类内部创建线程;
(1)类内部静态方法启动线程
#include <boost/thread/thread.hpp>
#include <iostream>
class HelloWorld
{
public:
static void hello()
{
std::cout << "Hello world, I''m a thread!" << std::endl;
}
static void start()
{
boost::thread thrd( hello );
thrd.join();
}
};
int main(int argc, char* argv[])
{
HelloWorld::start();
return 0;
} //在这里start()和hello()方法都必须是static方法。
(2)如果要求start()和hello()方法不能是静态方法则采用下面的方法创建线程:
#include <boost/thread/thread.hpp>
#include <boost/bind.hpp>
#include <iostream>
class HelloWorld
{
public:
void hello()
{
std::cout << "Hello world, I''m a thread!" << std::endl;
}
void start()
{
boost::function0< void> f = boost::bind(&HelloWorld::hello,this);
boost::thread thrd( f );
thrd.join();
}
};
int main(int argc, char* argv[])
{
HelloWorld hello;
hello.start();
return 0;
}
---------------------------------------------------------------------
线程ID从boost::thread中分离出来,因此一个线程可以获得它自己的ID(使用boost::this_thread::get_id()),
并且这些ID可以用作关联容器的键值。
主线程,子线程 , 线程, 资源的问题,线程同步问题
1.创建线程
2.使资源是线程安全的
保证同一时刻多个线程不会同时修改同一个共享资源,那么这个程序是线程安全的。
可以使用mutex类来控制线程的并发问题。
#include <boost/thread/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <iostream>
#include <string>
template<typename T>
class CQueue
{
public:
CQueue() {}
~CQueue() {}
void enqueue(const T& x)
{
boost::mutex::scoped_lock lock(mutex_);
list_.push_back(x);
std::cout << "enqueue" << std::endl;
}
T dequeue()
{
boost::mutex::scoped_lock lock(mutex_);
if(list_.empty())
throw "empty!";
T tmp = list_.front();
list_.pop_front();
std::cout << "dequeue" << std::endl;
return tmp;
}
private:
std::list<T> list_;
boost::mutex mutex_;
};
CQueue<std::string> queue_string;
void send_something()
{
for(int i=0; i<4; ++i)
queue_string.enqueue("Cyrus");
}
void recv_something()
{
std::string s;
for(int i=0; i<4; ++i)
{
try
{
s = queue_string.dequeue();
}
catch(...){}
}
}
int main(int argc, char* argv[])
{
boost::thread thrd1(send_something);
boost::thread thrd2(recv_something);
thrd1.join();
thrd2.join();
return 0;
}
在某一时刻,只有一个线程可以锁定这个mutex对象。
这就阻止了同一时刻多个线程并发访问共享资源。
相关文章推荐
- Android开发利用service实现背景音乐播放
- 类中的静态方法如何调用?-------Bosent中同一类下面的两个服务怎么调用
- Java学习笔记_成员内部类,静态内部类,方法内部类
- html2canvas- Take Screenshot of Web Page and Save It to Server (Javascript and PHP)
- 折腾源WRT的AC路无线路由-2
- Functional MRI (second edition) -- 3. Basic Principles of MR Signal Generation(II)
- Mule ESB-Content-Based Routing Tutorial(2)
- StringBuilder is mutable
- 20150616_OC之类别
- JavaScript权威指南_120_第15章_脚本化文档_15.3-文档结构和遍历-作为元素树的文档
- Leetcode|Count and Say
- MFC类的范例:CComboBoxEx
- BZOJ 1497 [NOI2006]最大获利 【最大流】
- bosent---多表查询
- mysql外键(FOREIGN KEY)的简单使用
- Capturing Webpage Screenshot with Html2Canvas.js(使用Html2Canvas生成网页快照)
- 当函数模版遇上函数重载
- 20150616实例变量作用域
- 如果想在main函数之前或main函数之后做一些工作
- 当函数模版遇上函数重载