我有一些代码,我正在尝试创建一个共享内存段.为此,该段由一个类内部进行管理.共享段将以"公告板"的方式使用.也就是说,这一个进程将写入它,许多其他进程将从中读取.无需再费周折:
#include <string>
#include <sys/types.h>
#include <boost/interprocess/managed_shared_memory.hpp>
static const std::string SHM_NAME("SharedMemory");
static const std::string SHM_STATUS("StatusArray");
static const std::string SHM_INFO1("MfgData");
class Manager {
u_int8_t* _status;
char* _info;
boost::interprocess::managed_shared_memory _shm;
Manager()
: _shm(boost::interprocess::create_only, SHM_NAME.c_str(), 1024)
{
// the process goes out to lunch on this first call, it's like a deadlock
status = _shm.construct<u_int8_t>(SHM_STATUS.c_str()) [128] (0); // array 128 bytes, init to 0
info = _shm.construct<char>(SHM_INFO1.c_str()) [256] (0);
}
public:
~Manager() {
_shm.destroy<u_int8_t>(SHM_STATUS.c_str());
_shm.destroy<char>(SHM_INFO1.c_str());
boost::interprocess::managed_shared_memory_object::remove(SHM_NAME.c_str());
}
Manager* Builder() {
// just in …Run Code Online (Sandbox Code Playgroud) 我正在尝试将 Boost 库编译成通用二进制文件(即包含 i386 和 x86_64 架构的构建的“胖”文件)。
发酸互联网和SO我组装了以下说明。
下载 boost(例如从http://www.boost.org/users/download/)
在下载的文件夹中,输入./bootstrap.sh
(或者,在我的情况下./bootstrap.sh --with-libraries=thread,因为我只需要线程库)
类型 ./b2 install cxxflags="-arch i386 -arch x86"
这些步骤将 Boost 线程库安装到/usr/local/lib/(其标准位置)。生成的静态库是一个通用二进制文件。到现在为止还挺好。
$ lipo -i /usr/local/lib/libboost_thread.a
Architectures in the fat file: /usr/local/lib/libboost_thread.a are: i386 x86_64
Run Code Online (Sandbox Code Playgroud)
然而,动态库似乎只针对 x86_64 进行了编译。
$ lipo -i /usr/local/lib/libboost_thread.dylib
Non-fat file: /usr/local/lib/libboost_thread.dylib is architecture: x86_64
Run Code Online (Sandbox Code Playgroud)
我也希望 .dylib 是通用的。有谁知道我如何为 i386 和 x86_64 编译它?
我有一个使用增强日志记录的应用程序。在关闭期间,它会因空指针访问而发生访问冲突。当我单步执行代码直至失败时,似乎 boost::log dll 正在被解除分配,然后 boost::thread 代码尝试访问曾经被 log dll 占用的内存。
我没有在自己的代码中使用任何 boost 线程,因此假设 boost-thread dll 由 boost log 使用。
为了确保所有接收器在关闭之前被销毁,我调用: core->flush() 和 core->remove_all_sinks()
我正在使用 boost 1.60,并且也尝试过使用 boost 1.63。相同的结果。
有没有办法确保在退出/卸载 dll 之前完全关闭 boost 日志记录核心?
关于Boost::Thread和我有一个新手问题Mutex.
我想启动以下的许多并行实例Worker,并且所有这些实例都写相同std::vector:
struct Worker {
std::vector<double>* vec;
Worker(std::vector<double>* v) : vec(v) {}
void operator() {
// do some long computation and then add results to *vec, e.g.
for(std::size_t i = 0; i < vec->size(); ++i) {
(*vec)[i] += some_value;
}
}
};
Run Code Online (Sandbox Code Playgroud)
我明白工人必须vec在写入之前锁定并在完成时将其解锁(因为所有工人都写入相同的向量).但是我该怎么表达呢?
我在这里阅读这个旧的Boost Thread FAQ,其中有一个指南,用于为具有boost::mutex不可复制对象作为成员的类实现复制构造和赋值运算符.
我对复制构造函数很好,但我对赋值运算符有一些疑问.以下说明仍然有效吗?
// old boost thread
const counter & operator=( const counter& other ){
if (this == &other)
return *this;
boost::mutex::scoped_lock lock1(&m_mutex < &other.m_mutex ?
m_mutex : other.m_mutex);
boost::mutex::scoped_lock lock2(&m_mutex > &other.m_mutex ?
m_mutex : other.m_mutex);
m_value = other.m_value;
return *this;
}
Run Code Online (Sandbox Code Playgroud)
不应该更新为:
// new boost thread
const counter& operator=(const counter& other){
if (this == &other)
return *this;
boost::unique_lock<boost::mutex> l1(m_mutex, boost::defer_lock);
boost::unique_lock<boost::mutex> l2(other.m_mutex, boost::defer_lock);
boost::lock(l1,l2);
m_value = other.m_value;
return *this;
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用OpenCV打开相机.这在我在主线程中打开相机时工作正常,但是当我尝试在Boost线程中打开相机时,它会失败.我没有能够谷歌为什么会这样.我假设它以某种方式与Boost线程的权限相关.
以下工作正常:
#include <cv.h>
#include <boost/thread.hpp>
#include <highgui.h>
using namespace cv;
void openCamera() {
Ptr< VideoCapture > capPtr(new VideoCapture(0)); // open the default camera
}
int main() {
openCamera();
}
Run Code Online (Sandbox Code Playgroud)
然后我的相机短暂打开,之后我收到消息"清理相机",正如人们所期望的那样.
但是当我通过Boost线程尝试相同时,它不会打开相机:
#include <cv.h>
#include <boost/thread.hpp>
#include <highgui.h>
#include <iostream>
using namespace cv;
void openCamera() {
std::cout << "confirming that openCamera() was called" << std::endl;
Ptr< VideoCapture > capPtr(new VideoCapture(0)); // open the default camera
}
int main() {
boost::thread trackerThread( boost::bind(openCamera) );
}
Run Code Online (Sandbox Code Playgroud)
这打印"确认openCamera()被调用",但相机从未打开,并且没有"清理相机"消息.
有什么办法可以解决吗?
谢谢!
我制作了以下示例程序来使用boost线程:
#pragma once
#include "boost\thread\mutex.hpp"
#include <iostream>
class ThreadWorker
{
public:
ThreadWorker() {}
virtual ~ThreadWorker() {}
static void FirstCount(int threadId)
{
boost::mutex::scoped_lock(mutex_);
static int i = 0;
for(i = 1; i <= 30; i++)
{
std::cout << i << ": Hi from thread: " << threadId << std::endl;
}
}
private:
boost::mutex mutex_;
};
Run Code Online (Sandbox Code Playgroud)
主要课程:
// ThreadTest.cpp
#include "stdafx.h"
#include "boost\thread\thread.hpp"
#include "ThreadWorker.h"
int _tmain(int argc, _TCHAR* argv[])
{
boost::thread thread1(&ThreadWorker::FirstCount, 1);
boost::thread thread2(&ThreadWorker::FirstCount, 2);
boost::thread thread3(&ThreadWorker::FirstCount, 3);
thread1.join();
thread2.join(); …Run Code Online (Sandbox Code Playgroud) 我有一个令人尴尬的并行问题,我想在多个处理器上执行.我本以为boost::thread会自动将新线程发送到新处理器,但所有这些线程都在与父进程相同的核心上执行.是否可以让每个线程在不同的处理器上运行,还是需要像MPI这样的东西?
我怀疑这boost::thread不是一个多处理器工具,我要求它做一些不是为它设计的东西.
编辑:我的问题归结为:为什么所有线程都在一个处理器上执行?有没有办法让boost::thread线程发送到不同的处理器?
这是我的代码的相关示例:
size_t lim=1000;
std::deque<int> vals(lim);
std::deque<boost::thread *> threads;
int i=0;
std::deque<int>::iterator it = vals.begin();
for (; it!=sigma.end(); it++, i++) {
threads.push_back(new boost::thread(doWork, it, i));
while (threads.size() >= maxConcurrentThreads) {
threads.front()->join();
delete threads.front();
threads.pop_front();
}
}
while(threads.size()) {
threads.front()->join();
threads.pop_front();
}
Run Code Online (Sandbox Code Playgroud)
应该清楚,doWork使用参数进行一些计算i并将结果存储在中vals.我的想法是设置maxConncurrentThreads为等于可用核心数,然后每个线程将使用空闲的核心.我只需要有人确认boost::thread不能以这种方式工作.
(我猜想有一种更好的方法来限制并发线程的数量而不是使用队列;也可以随意骂我.)
这是doWork功能:
void doWork(std::deque<int>::iterator it, int i) {
int ret=0;
int size = 1000; // originally 1000, later …Run Code Online (Sandbox Code Playgroud) 我正在尝试调整Martinho Fernandes的示例std::future实现,以便它可以在C++ 03下以Boost 1.40运行,作为一种便宜的,间隙测量,直到我可以访问Boost 1.41或C++ 11本身.
我的适应性并不美观,而且肯定不是最佳的,但我更希望它至少能起作用.但是,根据gcc版本4.4.1 20090725(Red Hat 4.4.1-2),它没有.
这是futures.h:
#include <boost/thread.hpp>
#include <boost/shared_ptr.hpp>
#include <stdexcept>
template <typename T>
class future;
template <typename T>
class promise;
namespace detail {
template <typename T>
struct future_shared_state
{
public:
void wait(boost::mutex& m) const
{
boost::mutex::scoped_lock lock(m, boost::adopt_lock);
while (!(state || error))
available.wait(lock);
}
T& get()
{
if (state)
return *state;
if (error)
throw *error;
throw std::runtime_error("WTF");
}
template <typename U>
void set_value(const U& value) …Run Code Online (Sandbox Code Playgroud) 我想在项目中使用boost :: thread,并且将CMake用作构建工具。但是,即使是非常简单的设置也会导致两个编译器错误:
main.cpp
#include <boost/thread.hpp>
int main()
{
boost::thread t;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
CMakeLists.txt
cmake_minimum_required (VERSION 2.6)
project (ThreadTest)
set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
find_package(Boost 1.58.0 COMPONENTS random thread)
set(SOURCE_DIR src)
set(SOURCE_FILES
${SOURCE_DIR}/main.cpp
)
if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
add_executable(test ${SOURCE_FILES})
target_link_libraries(test ${Boost_LIBRARIES})
endif()
Run Code Online (Sandbox Code Playgroud)
我正在使用Boost 1.68.0,CMake可以找到它,并且能够构建正确的Visual Studio项目文件。
我尝试使用boost :: random,它起作用了。
但是,编译上面的程序会导致两个错误消息:
这是error_code.hpp中的引发错误的行
我在boost安装中寻找了文件'libboost_thread-vc14 1 -mt-x64-1_68.lib',但仅找到'boost_1_68_0 \ lib64-msvc-14.0 \ boost_thread-vc14 0 -mt-gd-x64-1_68.lib'
链接器设置包含正确的文件:
所以,我有两个问题: