Boost Thread库如何与java.util.concurrent库进行比较?
性能至关重要,所以我更愿意继续使用C++(尽管Java现在要快得多).鉴于我必须使用C++编写代码,存在哪些库可以使线程更容易且更不容易出错.
我最近听说,从JDK 1.5开始,更改了Java内存模型以修复一些并发问题.C++怎么样?我最后一次用C++进行多线程编程是在3 - 4年前我使用pthreads的时候.虽然,我不想再用于大型项目了.我所知道的唯一另一种选择是Boost Threads.但是,我不确定它是否好.我听说过关于java.util.concurrent的好东西,但还没有关于Boost线程的内容.
以下是安全的吗?
我是线程新手,我想将一个耗时的过程委托给我的C++程序中的一个单独的线程.使用boost库我编写了类似这样的代码:
thrd = new boost :: thread(boost :: bind(&myclass :: mymethod,this,&finished_flag);
其中finished_flag是我班级的布尔成员.线程完成后,它会设置值,程序的主循环会检查该值的变化.我认为这是可以的,因为我只启动一个线程,并且该线程是唯一改变值的东西(除非在我启动线程之前初始化它)所以这没关系,或者我错过了什么,并且需要使用锁和互斥锁等
有些C++对象没有复制构造函数,但有移动构造函数.例如,boost :: promise.如何使用移动构造函数绑定这些对象?
#include <boost/thread.hpp>
void fullfil_1(boost::promise<int>& prom, int x)
{
prom.set_value(x);
}
boost::function<void()> get_functor()
{
// boost::promise is not copyable, but movable
boost::promise<int> pi;
// compilation error
boost::function<void()> f_set_one = boost::bind(&fullfil_1, pi, 1);
// compilation error as well
boost::function<void()> f_set_one = boost::bind(&fullfil_1, std::move(pi), 1);
// PS. I know, it is possible to bind a pointer to the object instead of
// the object itself. But it is weird solution, in this case I will have
// to take cake …Run Code Online (Sandbox Code Playgroud) 我正在使用boost :: thread库(V1.44)来支持我的C++项目中的线程.
用户需要能够暂停执行一个测试循环,该测试循环在自己的线程中运行,无限时间,并且只要他愿意,就可以恢复它.
在Windows下我解决了这个问题
bool ContintueLoop(){
if(testLoopPaused){ //testLoopPaused can be set by the user via GUI elements
try{
boost::this_thread::interruptible_wait( 2147483648 ); //that's very ugly,
// somebody knows the right way to pause it for a unlimited time?
return true;
}
catch( boost::thread_interrupted& e ){ //when the user selects resume the
// the thread is interrupted and continues from here
testLoopPaused = false;
return true;
}
if( ... ) //test for other flags like endTestLoop etc.
....
}
Run Code Online (Sandbox Code Playgroud)
这没有任何问题,即使知道无限制中断的正确值也会很好.
我开始实现我的程序的linux版本,但我遇到了我得到编译器错误的问题 …
在调用boost :: thread运行一些指令后,是否有可能回到主线程?
我的代码基于proactor模式,但是某个函数可能需要一些时间,所以为了不阻塞整个程序,我正在创建一个运行这个函数的线程.当这个函数结束时,我需要调用另一个函数,但它必须在主线程上运行.我有一个连接池,这不是线程安全的,我真的想避免互斥.
有没有一种稳定的方法在主线程上运行一个函数(在另一个线程上调用)?
就像在ObjectiveC performSelectorOnMaintThread中一样
使用Visual Studio 2008和Boost库1.46.1我想用/ CLR标志编译和链接以下内容:
#include <boost/thread/thread.hpp>
void run() {}
int main(int argc, char *argv[])
{
boost::thread t(run);
}
Run Code Online (Sandbox Code Playgroud)
第一个错误是关于boost :: thread中的前向声明的伪结构.这篇文章 通过声明:
namespace boost {
struct thread::dummy {};
}
Run Code Online (Sandbox Code Playgroud)
当然,我现在可以编译,但后来我得到链接器警告
警告1警告LNK4248:'boost.detail.win32._SECURITY_ATTRIBUTES'的未解析的typeref标记(0100001F); 图像可能无法运行
运行应用程序导致
应用程序无法正确启动(0xc000007b).
前面提到的论坛帖子中的所有建议都不适合我.我已经构建了Boost Threads lib的静态版本,它运行正常,没有/ CLR标志.调试/发布没有区别.我在Win7 32位下运行.
任何提示?
从处理程序中发布新的处理程序是否安全?即可以将io_service::run()post new new Handlers 调用到相同的io_service?
谢谢
我有一个程序在一个单独的therad中运行一些动作,然后加入线程,比如这个:
#include <boost/thread.hpp>
#include <iostream>
using namespace std;
void f() {
for (int i = 0; i < 100; ++i) cout << i << endl;
}
int main() {
boost::thread t(f);
t.join();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果我在其上运行Valgrind,它会报告"可能丢失"的内存.如果省略join(),这似乎是合乎逻辑的,因为在这种情况下,当程序退出时线程仍在运行.但如果线程完成,我希望没有警告.
这是回溯:
==8797== 288 bytes in 1 blocks are possibly lost in loss record 2 of 3
==8797== at 0x4A1F8B3: calloc (vg_replace_malloc.c:467)
==8797== by 0x400F289: allocate_dtv (in /lib64/ld-2.4.so)
==8797== by 0x400F34D: _dl_allocate_tls (in /lib64/ld-2.4.so)
==8797== by 0x53EF981: pthread_create@@GLIBC_2.2.5 (in /lib64/libpthread-2.4.so)
==8797== by 0x4B3311D: …Run Code Online (Sandbox Code Playgroud) When the application is shutting down Valgrind reports these 3 issues:
==70== Mismatched free() / delete / delete []
==70== at 0x483997B: free (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==70== by 0x4870C89: check_free (dlerror.c:202)
==70== by 0x4870C89: check_free (dlerror.c:186)
==70== by 0x4870C89: free_key_mem (dlerror.c:221)
==70== by 0x4870C89: __dlerror_main_freeres (dlerror.c:239)
==70== by 0x4B59711: __libc_freeres (in /usr/lib/x86_64-linux-gnu/libc-2.29.so)
==70== by 0x482E19E: _vgnU_freeres (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_core-amd64-linux.so)
==70== by 0x4A0A3A9: __run_exit_handlers (exit.c:132)
==70== by 0x4A0A3D9: exit (exit.c:139)
==70== by …Run Code Online (Sandbox Code Playgroud) 所以我使用a boost::lockfree::spec_queue通过两个boost_threads在我的应用程序中运行两个对象的仿函数进行通信.
一切都很好,除了spec_queue::pop()方法是非阻塞的事实.即使队列中没有任何内容,它也会返回True或False.但是我的队列似乎总是返回True(问题#1).我想这是因为我预先分配了队列.
typedef boost::lockfree::spsc_queue<q_pl, boost::lockfree::capacity<100000> > spsc_queue;
Run Code Online (Sandbox Code Playgroud)
这意味着要有效地使用队列,我必须忙着等待不断使用100%cpu弹出队列.我宁愿不睡任意的时间.我已经在java中使用了其他队列,直到对象可用为止.这可以用std ::或boost :: data结构来完成吗?
boost-thread ×10
c++ ×9
boost ×4
boost-asio ×1
boost-bind ×1
c++-cli ×1
clr ×1
g++ ×1
java ×1
lock-free ×1
pthreads ×1
thread-local ×1
valgrind ×1