标签: boost-thread

C++ 0x没有信号量?如何同步线程?

是不是C++ 0x没有信号量?Stack Overflow上已经有一些关于信号量使用的问题.我一直使用它们(posix信号量)让线程等待另一个线程中的某个事件:

void thread0(...)
{
  doSomething0();

  event1.wait();

  ...
}

void thread1(...)
{
  doSomething1();

  event1.post();

  ...
}
Run Code Online (Sandbox Code Playgroud)

如果我用互斥量做到这一点:

void thread0(...)
{
  doSomething0();

  event1.lock(); event1.unlock();

  ...
}

void thread1(...)
{
  event1.lock();

  doSomethingth1();

  event1.unlock();

  ...
}
Run Code Online (Sandbox Code Playgroud)

问题:它很难看并且不能保证thread1首先锁定互斥锁(假设同一个线程应该锁定和解锁互斥锁,你也无法在thread0和thread1启动之前锁定event1).

因此,由于boost也没有信号量,实现上述目标的最简单方法是什么?

c++ multithreading synchronization boost-thread c++11

124
推荐指数
7
解决办法
8万
查看次数

boost shared_mutex的示例(多次读取/一次写入)?

我有一个多线程应用程序,必须经常读取一些数据,偶尔会更新数据.现在,互斥锁可以保持对数据安全的访问,但是它很昂贵,因为我希望多个线程能够同时读取,并且只在需要更新时将其锁定(更新线程可以等待其他线程完成) .

我认为这是boost::shared_mutex应该做的,但我不清楚如何使用它,并没有找到一个明确的例子.

有没有人有一个我可以用来开始的简单例子?

c++ multithreading boost mutex boost-thread

113
推荐指数
4
解决办法
9万
查看次数

如何在C++中使用boost创建线程池?

如何使用C++中的boost创建线程池,如何将任务分配给线程池?

c++ boost boost-thread boost-asio threadpool

51
推荐指数
3
解决办法
6万
查看次数

C++线程池

什么是C++在生产代码中使用的线程池的良好开源实现(类似于boost)?

请提供您自己的示例代码或示例代码使用的链接.

c++ multithreading boost boost-thread threadpool

42
推荐指数
3
解决办法
5万
查看次数

英特尔TBB与升压

我的新应用程序我可以灵活地决定使用库进行多线程.到目前为止我一直在使用pthread.现在想探索跨平台库.我对TBB和Boost不感兴趣.我不明白TBB比Boost有什么好处.我试图找出TBB相对于Boost的优势:TBB摘录wiki"相反,该库通过允许将操作视为"任务"来抽象对多个处理器的访问,这些任务由库的运行分配给各个核心 - 时间引擎,并自动有效地使用缓存.TBB程序根据算法创建,同步和销毁相关任务的图形,"

但是线程库甚至需要担心线程分配到核心.这不是操作系统的工作吗?那么使用TBB而不是Boost的真正优势是什么呢?

boost tbb boost-thread

31
推荐指数
2
解决办法
1万
查看次数

shared_ptr断言px!= 0失败

我有一个相当复杂的多线程应用程序(服务器),由于断言,有时会崩溃:

/usr/include/boost/smart_ptr/shared_ptr.hpp:418: T* boost::shared_ptr< <template-parameter-1-1> >::operator->() const [with T = msg::Player]: Assertion `px != 0' failed.
Run Code Online (Sandbox Code Playgroud)

我一直无法确定原因,并想知道这是一个问题与boost :: shared_ptr或它是我吗?

我尝试了g ++ 4.4.3-4ubuntu5和llvm-g ++(GCC)4.2.1优化而没有优化和libboost1.40-dev(= 1.40.0-4ubuntu4).

boost-thread shared-ptr

30
推荐指数
1
解决办法
4万
查看次数

Boost.Thread链接 - boost_thread与boost_thread-mt

我不清楚Boost.Thread 1.34.1库中存在哪些链接选项.我在Ubuntu 8.04上,我发现在使用boost_threadboost_thread-mt在链接编译和运行期间,但我没有在上面链接中看到关于这些或任何其他链接选项的任何文档.

什么Boost.Thread链接选项可用,它们是什么意思?

ubuntu linker g++ boost-thread

25
推荐指数
2
解决办法
2万
查看次数

C++ 11中的boost :: thread_group?

boost::thread_groupC++ 11中有什么类似的东西吗?

我只是试图将我的程序从使用移植boost:thread到C++ 11线程,并且无法找到任何等效的东西.

c++ boost boost-thread c++11

25
推荐指数
2
解决办法
9366
查看次数

(简单)提升thread_group问题

我正在尝试编写一个相当简单的线程应用程序,但它是boost的线程库的新手.我正在研究的一个简单的测试程序是:

#include <iostream>
#include <boost/thread.hpp>

int result = 0;
boost::mutex result_mutex;

boost::thread_group g;

void threaded_function(int i)
{
    for(; i < 100000; ++i) {}

    {
        boost::mutex::scoped_lock lock(result_mutex);
        result += i;
    }
}

int main(int argc, char* argv[])
{
    using namespace std;

    // launch three threads
    boost::thread t1(threaded_function, 10);
    boost::thread t2(threaded_function, 10);
    boost::thread t3(threaded_function, 10);

    g.add_thread(&t1);
    g.add_thread(&t2);
    g.add_thread(&t3);

    // wait for them
    g.join_all();

    cout << result << endl;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

但是,当我编译并运行该程序时,我得到了一个输出

$ ./test 
300000
test: pthread_mutex_lock.c:87: __pthread_mutex_lock: Assertion `mutex->__data.__owner == 0' …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading boost boost-thread

24
推荐指数
2
解决办法
3万
查看次数

提升线程错误:未定义的引用

#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;
}
Run Code Online (Sandbox Code Playgroud)

我试图编译这个程序,并得到这些错误:

/usr/include/boost/thread/pthread/mutex.hpp:40: undefined reference to
   `boost::thread_resource_error::thread_resource_error()'
/usr/include/boost/thread/pthread/mutex.hpp:40: undefined reference to 
   `boost::thread_resource_error::~thread_resource_error()'
/usr/include/boost/thread/pthread/mutex.hpp:40: undefined reference to 
   `typeinfo for boost::thread_resource_error'
./src/thread.o: In function `condition_variable':
/usr/include/boost/thread/pthread/condition_variable_fwd.hpp:33: 
  undefined reference to `boost::thread_resource_error::thread_resource_error()'
/usr/include/boost/thread/pthread/condition_variable_fwd.hpp:33: 
  undefined reference to `boost::thread_resource_error::~thread_resource_error()'
/usr/include/boost/thread/pthread/condition_variable_fwd.hpp:33: \
  undefined reference to `typeinfo for boost::thread_resource_error'
./src/thread.o: In function `thread_data_base':
/usr/include/boost/thread/pthread/thread_data.hpp:54: 
  undefined reference to `vtable for boost::detail::thread_data_base' …
Run Code Online (Sandbox Code Playgroud)

c++ boost boost-thread

23
推荐指数
3
解决办法
5万
查看次数