标签: boost-thread

boost :: threads执行顺序

我对连续创建的线程的执行顺序有疑问.这是代码.

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

using namespace std;

boost::mutex mutexA;
boost::mutex mutexB;
boost::mutex mutexC;
boost::mutex mutexD;


void SomeWork(char letter, int index)
{
    boost::mutex::scoped_lock lock;
    switch(letter)
    {
    case 'A' : lock = boost::mutex::scoped_lock(mutexA); break;
    case 'B' : lock = boost::mutex::scoped_lock(mutexB); break;
    case 'C' : lock = boost::mutex::scoped_lock(mutexC); break;
    case 'D' : lock = boost::mutex::scoped_lock(mutexD); break;
    }

    cout << letter <<index << " started" << endl;
    Sleep(800);
    cout << letter<<index << " finished" << endl; 
}

int main(int argc , …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading boost mutex boost-thread

5
推荐指数
1
解决办法
917
查看次数

使用Boost :: Asio停止线程服务器循环

我正在为更大的软件开发一个小型服务器循环,但它不能像我希望的那样工作.

当用户键入".quit"时,我希望软件停止此线程服务器循环:

try {

while (true) {
      acceptor.accept(socket);
      const size_t buffersize = 1024;
      char data[buffersize+1] = {0};
      data[socket.read_some(boost::asio::buffer(data,buffersize))] = '\0'; // Write data & place terminator
      boost::thread asyncWriting(boost::bind( &myClass::writeToFile, this ));
      socket.close();
}

} catch(const boost::system::system_error& e) {
    cout << "Boost System Error: " << e.what() << endl;
}
Run Code Online (Sandbox Code Playgroud)

我按以下方式启动线程:

serverThread = boost::shared_ptr<boost::thread>( new boost::thread(boost::bind( &myClass::startServer, this )) );
Run Code Online (Sandbox Code Playgroud)

但是我在停止"服务器"时遇到了问题.无论我是否中断线程,关闭套接字和/或接受器,或者只是破坏程序Boost会抛出错误:

 Bad file descriptor
Run Code Online (Sandbox Code Playgroud)

它不会每次都发生,但经常发生,我想解决这个问题而不是忽略它.

你能帮助我如何关闭这个倒干净

c++ boost-thread boost-asio

5
推荐指数
1
解决办法
2904
查看次数

boost :: thread程序在抛出std :: exception时崩溃

我很困惑为什么这个程序崩溃了.这是整个计划

#include<fstream>
#include<string>
#include<iostream>
#include <exception>
#include <boost/thread/thread.hpp>

    void func( const std::string& filename )
    {
        std::ofstream outFile( filename.c_str(), std::ios::binary);
        if( !outFile.is_open() )
        {
            std::string err("Could not open file ");
            err.append(filename);
            err.append(" for writing");
            throw std::exception(err.c_str());
        }
    }

    int main()
    {
        std::string filename("xX:\\does_not_exist.txt");
        try
        {
            boost::thread thrd(boost::bind(&func, filename ));
            thrd.join();
        //  func( filename ); // calling this does not cause a crash
        }
        catch( const std::exception& ex)
        {
            std::cout << ex.what() << std::endl;
        }
        return 0;
    }
Run Code Online (Sandbox Code Playgroud)

环境
Windows 7
Visual …

c++ boost-thread

5
推荐指数
1
解决办法
1754
查看次数

使用C++ API进行多线程处理

我试图使用OpenMP并行我的程序,有时我觉得我达到了死胡同.

我想在我在类中定义(和初始化)的函数成员中共享变量.如果我理解正确,就不可能做一个类#pragma omp parallel shared(foo)的数据成员(例如int,boost::multi_arraystd::vector).例如:在类中的向量数据成员上使用push_back().更新a的值boost::multi_array.

我的问题是,如果OpenMP是适合它的工具,还是应该使用boost :: thread或tbb?或其他什么...什么支持C++ API

Reagrds

c++ tbb openmp boost-thread boost-mpi

5
推荐指数
1
解决办法
495
查看次数

boost thread破坏了多态性

复制:实现boost :: thread包装器接口时"调用纯虚方法"

我试图使用boost线程创建一个更面向对象的线程版本.

所以我创建了一个Thread类:

class Thread {
public:
    Thread() {}
    virtual ~Thread() { thisThread->join(); }

    void start() { thisThread = new boost::thread(&Thread::run, this); }

    virtual void run() {};

private:
    boost::thread *thisThread;
};
Run Code Online (Sandbox Code Playgroud)

这个类在start()中创建线程,如下所示:

thisThread = new boost::thread(&Thread::run, this);

问题是当我创建一个覆盖run()方法的类时,run()Thread 的方法是由线程调用而不是新run()方法

例如,我有一个扩展Thread的类:

class CmdWorker: public Thread {
public:
    CmdWorker() : Thread() {}
    virtual ~CmdWorker() {}

    void run() { /* deosn't get called by the thread */ }
};
Run Code Online (Sandbox Code Playgroud)

当我做

Thread *thread = new …
Run Code Online (Sandbox Code Playgroud)

c++ boost boost-thread

5
推荐指数
1
解决办法
1598
查看次数

CMake和Boost

我搜索并发现很多人都有同样的问题,但没有解决方案.

我正在使用CMake为MinGW生成Makefile,在编译时我收到错误:

CMakeFiles\boosttest.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x5e): undefined reference to `_imp___ZN5boost6thread4joinEv'
CMakeFiles\boosttest.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x71): undefined reference to `_imp___ZN5boost6threadD1Ev'
CMakeFiles\boosttest.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x88): undefined reference to `_imp___ZN5boost6threadD1Ev'
Run Code Online (Sandbox Code Playgroud)

这似乎是一个链接问题,我明白了.我的CMake配置是:

project(boosttest)
cmake_minimum_required(VERSION 2.6)

include_directories(${boosttest_SOURCE_DIR}/include c:/boost_1_48_0/)
link_directories(c:/boost_1_48_0/lib)

file(GLOB_RECURSE cppFiles src/*.cpp)

add_executable(boosttest ${cppFiles})

target_link_libraries(boosttest libboost_thread-mgw46-mt-1_48.a)
Run Code Online (Sandbox Code Playgroud)

首先我尝试使用find_package(Boost COMPONENTS thread)它,它的工作方式相同,所以我想尝试手动执行此操作,但仍然会遇到相同的错误.

有什么见解吗?

我使用bjam编译它为mingw并作为静态链接.还试过做:

add_library(imp_libboost_thread STATIC IMPORTED)
set_property(TARGET imp_libboost_thread PROPERTY IMPORTED_LOCATION c:/boost_1_48_0/lib/libboost_thread-mgw46-mt-1_48.a)
target_link_libraries(boosttest imp_libboost_thread)
Run Code Online (Sandbox Code Playgroud)

我仍然得到相同的错误消息.

boost cmake boost-thread

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

链接Boost-Python Hello World

我正在尝试编译和链接boost-python hello world示例,我有一些链接问题.

操作系统:Ubuntu

g++ -fPIC -w Test2.cpp -I ../../../Libs/Python/Python-2.7.3/Include -I ../../../Libs/Python/Python-2.7.3 -I ../../../Libs/Boost/boost_1_52_0 -Wl,-rpath,../../../Libs/Python/Python-2.7.3/build/lib.linux-x86_64-2.7 -L -L../../../Libs/Python/Python-2.7.3/build/lib.linux-x86_64-2.7 -lssl -lcrypto -lpthread -lm -lutil -lpython2.7 -Wl,-rpath, -L../../../Libs/Boost/boost_1_52_0/lib -L../../../Libs/Boost/boost_1_52_0/stage/lib -lboost_python
Run Code Online (Sandbox Code Playgroud)

我得到以下错误

../../../Libs/Boost/boost_1_52_0/stage/lib/libboost_python.so: undefined reference to `PyUnicodeUCS2_AsWideChar'
../../../Libs/Boost/boost_1_52_0/stage/lib/libboost_python.so: undefined reference to `PyUnicodeUCS2_FromEncodedObject'
collect2: ld returned 1 exit status
make: *** [Test2] Error 1
Run Code Online (Sandbox Code Playgroud)

我有2个Makefile,一个用于python,一个用于boost.Python Makefile:

PYTHON_VERSION  = 2.7.3
PYTHON_FOLDER   = Python-$(PYTHON_VERSION)
INSTAL_FOLDER   = $(PWD)/Python_Build

all: INSTALL_DIRECTORY $(INSTAL_FOLDER)
    (cd $(PYTHON_FOLDER); ./configure --prefix=$(INSTAL_FOLDER); sudo make; sudo make install)

clean:
    (cd $(PYTHON_FOLDER); sudo make clean;)
    (rm …
Run Code Online (Sandbox Code Playgroud)

python boost boost-thread boost-python python-2.7

5
推荐指数
1
解决办法
2073
查看次数

访问boost绑定时共享库中的C++符号查找错误

我正在尝试将多线程添加到我的库中,所以我正在为我的库创建一个线程执行器.为此我使用boost线程.

这是我在运行链接到库的测试用例时遇到的错误:

symbol lookup error: libmylibexample.so.0: undefined symbol: _ZTVN5boost6detail16thread_data_baseE
Run Code Online (Sandbox Code Playgroud)

这是我的共享库中导致错误的代码行:

MyNameSpace::Producer producer = MyNameSpace::Producer();
threads.create_thread(boost::bind(&MyNameSpace::Producer::run, &producer));
Run Code Online (Sandbox Code Playgroud)

我正在使用autotools和libtool编译库.代码编译得很好.然后我创建一个测试用例,我试图引用该库.以下是编译测试用例的编译顺序:

g++ -I.  -I../include -g -O2 -MT runTest-runTest.o -MD -MP -MF .deps/runTest-runTest.Tpo -c -o runTest-runTest.o `test -f 'runTest.cc' || echo './'`runTest.cc
Run Code Online (Sandbox Code Playgroud)

这是我的链接阶段:

mv -f .deps/runTest-runTest.Tpo     .deps/runTest-runTest.Po

/bin/bash ../libtool --tag=CXX   --mode=link g++  -g -O2 ../libmylibexample/libmylibexample.la   -o runTest runTest-runTest.o -lboost_system -lboost_filesystem -lboost_regex -lboost_thread-mt -lfftw3 -ltiff

libtool: link: g++ -g -O2 -o .libs/runTest runTest-runTest.o  ../libmylibexample/.libs/libmylibexample.so -lboost_system -lboost_filesystem -lboost_regex -lboost_thread-mt -lfftw3 /usr/lib/x86_64-linux-gnu/libtiff.so
Run Code Online (Sandbox Code Playgroud)

我的一位同事建议初始化一些与线程相关的boost模板,以帮助共享库从boost_thread库加载符号.我并不完全确定这样做的最佳方法,如果这是正确的方法来加载东西.

总结一下:错误似乎涉及无法从我的共享库加载libboost_thread中定义的符号.

c++ shared-libraries boost-thread libtool

5
推荐指数
1
解决办法
6774
查看次数

在多线程进程中处理信号的示例

任何人都可以给我以下情况的步骤甚至代码:

包含多个线程的进程以及这些线程负责捕获用户定义的信号SIGUSR1.只有这个线程应该能够接收到这个信号,并且在接收到这个信号后我会做一些事情.

在我的情况下,内核模块将信号发送到我的进程ID.然后我的过程负责将它传递给正确的监听线程,该线程还建立了信号处理程序,即信号处理程序不在主线程中.

我已经为一个单线程进程运行了一些代码,但是在多线程环境中运行它时遇到了问题.

我在Linux Ubuntu 12.04.3上使用内核版本3.8.0-29运行我的代码.为了创建流程,我将在Boost Threads和POSIX threads API之间进行混合.

#include <unistd.h>
#include <signal.h>
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/mman.h>
#include <string.h>

/* Value of the last signal caught */
volatile sig_atomic_t sig_value;

static void sig_handler(const int sig_number, siginfo_t *sig_info, void *context)
{
if (sig_number == SIGSEGV)
{
    error_sys("Error at address 0x%lx", (long)sig_info->si_addr);
    exit(-1);
}
sig_value = sig_number;
}


int init_signal_catcher()
{
struct sigaction sig_action; /* Structure describing the action to be taken when asignal arrives. …
Run Code Online (Sandbox Code Playgroud)

c++ linux multithreading signals boost-thread

5
推荐指数
1
解决办法
9052
查看次数

mciSendString不会暂停从线程播放的声音

最近我已经要求一个类似这个问题的解决方案:

有没有办法暂停/停止播放带有"等待"选项的mcisendstring的mp3文件?

我想在我的音频播放器中实现一个功能,允许人们连续播放声音,而滑块根据曲目运行的当前秒数移动,并且还具有在当前曲目之后转到下一曲目的功能.过度

之后(正如您可以在链接中阅读)尝试使用它

mciSendString("play mp3 wait", NULL, 0, NULL);
Run Code Online (Sandbox Code Playgroud)

由于轨道在完成之前无法暂停或停止的问题而失败,我现在正试图以另一种方式实现它.目前,当我开始播放曲目时,我也会启动另一个线程,即启动计数器.计数器以秒为单位获得轨道的长度,并且正在倒计时,还提供用于暂停/恢复计数器的互斥锁.为了阻止我的MusicCycle简单地循环不受控制,我加入了线程,因此等待终止.

void Music::MusicCycle(std::wstring trackPath)
{
    while (true)
    {
        OpenMP3(trackPath);
        mciSendString("play mp3", NULL, 0, NULL);

        m_counterThread = boost::thread(boost::bind(&Counter::StartCount, m_counter, <length of track in seconds>));
        m_counterThread.join();

        //... Get new track here
    }
}
Run Code Online (Sandbox Code Playgroud)

请注意,整个方法也是在一个线程中创建的:

m_cycleThread = boost::thread(boost::bind(&Music::MusicCycle, this, trackPath));
Run Code Online (Sandbox Code Playgroud)

由MusicCycle函数启动的线程如下所示:

void Counter::StartCount(int seconds)
{
    boost::mutex::scoped_lock lock(m_mutex);

    for (int i = 0; i < seconds; i++)
    {
        while (m_counterLock)
        {
            m_condVar.wait(lock);
        }

        boost::this_thread::sleep(boost::posix_time::seconds(1));
    }
}
Run Code Online (Sandbox Code Playgroud)

此外,我添加了另一个功能来使用我的Pause/Resume方法锁定/解锁互斥锁,这也调用相应的mciSendString函数

mciSendString("resume mp3", NULL, 0, NULL);

mciSendString("pause mp3", …
Run Code Online (Sandbox Code Playgroud)

c++ boost boost-thread mcisendstring

5
推荐指数
1
解决办法
488
查看次数