这是创建thread_group并并行执行所有线程的代码:
boost::thread_group group;
for (int i = 0; i < 15; ++i)
group.create_thread(aFunctionToExecute);
group.join_all();
Run Code Online (Sandbox Code Playgroud)
此代码将立即执行所有线程.我想要做的是并行执行除4个以外的所有操作.当on终止时,执行另一个,直到不再执行为止.
这些天我正在阅读pdf Designing MT程序.它解释了在该对象超出范围之前,用户必须在C++ 0x中显式调用detach()类的std::thread对象.如果你不打电话,std::terminate()它将被调用,应用程序将死亡.
我通常boost::thread在C++中使用线程.如果我错了,请纠正我但是boost::thread当一个对象超出范围时会自动分离.
在我看来,提升方法遵循RAII原则而标准没有.
你知道这有什么特别的原因吗?
我目前正在使用Valgrind的"Callgrind"来分析具有性能问题的应用程序.在查看分析数据时,似乎boost::detail::get_tss_data在主要用于物理模拟和可视化的应用程序中花费了25%的处理时间.
get_tss_data 显然被称为 thread_specific_ptr::get
有没有人按预期看到这个?它通常意味着其他具体的东西吗?
编辑:
我的平台是:Linux-2.6.32,x86,GCC 4.4.3,libc6-2.11.1/libpthread-2.11.1
写了一个简单的测试:
#include <iostream>
#include <boost/thread.hpp>
using namespace std;
void myThreadRun() {
cout << "Thread id: " << boost::this_thread::get_id() << "\n";
}
int main() {
for (int i = 0; i < 10000; i++) {
boost::thread t(myThreadRun);
t.join();
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Valgrind Massif显示以下图表:

(已启用堆栈分析.平台:Linux Ubuntu x86).
该程序实际上似乎没有内存泄漏:内存使用情况稳定.
我想知道:这是Valgrind还是boost :: thread的问题?或许我误解了什么?
你会怎么解释?
如何使队列线程安全?我需要按/弹出/前/后清除.是什么类似的提升?
我有一个生产者和一个或多个消费者.
我目前正在开发一个用于boost线程的小包装器类,但我真的不知道睡眠功能如何工作,这是我到目前为止所得到的:
BaseThread::BaseThread(){
thread = boost::thread();
bIsActive = true;
}
BaseThread::~BaseThread(){
join();
}
void BaseThread::join(){
thread.join();
}
void BaseThread::sleep(uint32 _msecs){
if(bIsActive)
boost::this_thread::sleep(boost::posix_time::milliseconds(_msecs));
}
Run Code Online (Sandbox Code Playgroud)
这是我到目前为止实现它的方式,但我真的不明白静态this_thread :: sleep方法如何知道哪个线程要睡眠,例如我的线程包装器的多个实例是活动的.这是实施它的正确方法吗?
首先,我希望我的问题有道理,甚至可能!从我读过的关于TCP套接字和Boost :: ASIO的内容来看,我认为应该如此.
我要做的是设置两台机器,并在它们之间通过TCP实现双向读/写链路.任何一方都应该能够发送一些数据供另一方使用.
关于TCP(/ IP?)的第一个令人困惑的部分是它需要这个客户端/服务器模型.然而,阅读表明任何一方都能够写作或阅读,所以我还没有完全气馁.我不介意建立任意一方作为客户端而另一方作为服务器.在我的申请中,这可以提前谈判,而不是我关心的问题.
不幸的是,我遇到的所有示例似乎都集中在连接到服务器的客户端上,并且服务器立即发回一些数据.但我希望客户端能够写入服务器.
我设想了一种我称之为的循环io_service.poll().如果轮询显示另一方正在等待发送一些数据,它将调用read()并接受该数据.如果队列中没有任何等待,并且它有要发送的数据,那么它将调用write().双方都这样做,他们应该能够互相读写.
我关心的是如何避免两者同时进入某些同步write()操作的情况.他们都有数据发送,然后坐在那里等待双方发送.这是否问题恰恰暗示我应该只做异步write()和read()?在这种情况下,如果连接的两端都试图同时异步写入,事情会爆发吗?
我希望有人可以理想地:
1)提供一种非常高级的结构或最佳实践方法,可以从客户端和服务器的角度完成此任务
或者,理想情况下,
2)说我想做的事情是不可能的,也许建议某种形式的解决方法.
一个简单的程序是:我想使用这个gettid函数获取两个线程的线程ID.我不想直接做sysCall.我想使用这个功能.
#include <iostream>
#include <boost/thread/thread.hpp>
#include <boost/date_time/date.hpp>
#include <unistd.h>
#include <sys/types.h>
using namespace boost;
using namespace std;
boost::thread thread_obj;
boost::thread thread_obj1;
void func(void)
{
char x;
cout << "enter y to interrupt" << endl;
cin >> x;
pid_t tid = gettid();
cout << "tid:" << tid << endl;
if (x == 'y') {
cout << "x = 'y'" << endl;
cout << "thread interrupt" << endl;
}
}
void real_main() {
cout << "real main thread" << endl;
pid_t tid …Run Code Online (Sandbox Code Playgroud) 有些人似乎使用boost :: bind()函数启动boost :: threads,就像在以下问题的接受答案中一样:
而其他人完全没有使用它,就像这个问题最赞成的回答一样:
那么,如果它存在,有什么区别?
我有一个可以并行化的C++程序.我正在使用Visual Studio 2010,32位编译.
简而言之,该计划的结构如下
#define num_iterations 64 //some number
struct result
{
//some stuff
}
result best_result=initial_bad_result;
for(i=0; i<many_times; i++)
{
result *results[num_iterations];
for(j=0; j<num_iterations; j++)
{
some_computations(results+j);
}
// update best_result;
}
Run Code Online (Sandbox Code Playgroud)
由于每个some_computations()都是独立的(读取了一些全局变量,但没有修改全局变量),我并行化了内部for循环.
我的第一次尝试是使用boost :: thread,
thread_group group;
for(j=0; j<num_iterations; j++)
{
group.create_thread(boost::bind(&some_computation, this, result+j));
}
group.join_all();
Run Code Online (Sandbox Code Playgroud)
结果很好,但我决定尝试更多.
我试过OpenMP库
#pragma omp parallel for
for(j=0; j<num_iterations; j++)
{
some_computations(results+j);
}
Run Code Online (Sandbox Code Playgroud)
结果比boost::thread那些更差.
然后我尝试了ppl库并使用parallel_for():
Concurrency::parallel_for(0,num_iterations, [=](int j) { …Run Code Online (Sandbox Code Playgroud) boost-thread ×10
c++ ×9
boost ×5
c++11 ×2
boost-asio ×1
containers ×1
linux ×1
linux-kernel ×1
openmp ×1
ppl ×1
queue ×1
system-calls ×1
valgrind ×1
wrapper ×1