标签: boost-thread

如何在c ++中使用指向另一个类的线程

我需要在另一个类文件中使用该函数作为线程

int main()
{
master t;

boost::thread t1(boost::bind(t.start, "exampl"));

t1.join();
}
Run Code Online (Sandbox Code Playgroud)

我有一个类master和一个函数start..我需要传递一个值并运行我已经在同一个类中使用它它工作正常...可以任何人告诉我我错在哪里

c++ boost-thread

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

boost::condition_variable.timed_wait 立即返回

我正在开发一个小线程库,但遇到了问题。boost::condition_variable.wait() 工作完美,但是 boost::condition_variable.timed_wait() 立即返回,它不会超时。

文档说它应该只在超时过去或被通知后返回。这是通知前的三秒等待,我已经尝试了 10 秒和 100 秒的超时,所以它应该在 3 秒后返回。

编辑:

boost::condition_variable waitCondition;
boost::mutex mMutex;

Message MessageClient::waitAsync(Message msg, bool waitForReply) {
   unique_lock<boost::mutex> lock(msg->mMutex);
   if(mSendTimeout.sec == 0)
       msg->waitCondition.wait(lock);
   else {
       timeout = msg->waitCondition.timed_wait(lock,  mSendTimeout);
       if(!timeout)
           return 0;

       if(waitForReply) {
          Message reply = receiveMessage();
          return reply;
       }
       else
          return 0;
}
Run Code Online (Sandbox Code Playgroud)

这是在 sendMessage 之后调用的。接收者收到消息,发送回复,然后调用

waitCondition.notify_all();
Run Code Online (Sandbox Code Playgroud)

c++ boost-thread

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

boost条件需要volatile变量

HY,

我已经开始使用C++中的线程(boost threads),我在以下情况下找到了自己:

boost::mutex::scoped_lock lk(monitor);
while (buffered == 0) {
    buffer_not_empty.wait(lk);
}
Run Code Online (Sandbox Code Playgroud)

如果int变量buffered应该是volatile ,我就会徘徊.我会说,为了防止像这样的优化,它应该在编译时:

boost::mutex::scoped_lock lk(monitor);
while (true) {
    buffer_not_empty.wait(lk);
}
Run Code Online (Sandbox Code Playgroud)

但由于这是来自boost文档的一个例子,我可能会遗漏一些东西......

是一个锁/互斥/条件足以阻止这种优化?喜欢在java?

c++ boost-thread

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

请使用boost.thread或C++ 11建议有关多线程的有价值的书

对于您的意见,哪本书值得购买?我通常熟悉多线程概念,OpenMP,基本的WinAPI线程.我现在想学习有效地使用新的std :: thread或boost.thread - 我不熟悉原子,条件变量等等.

c++ multithreading boost-thread c++11

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

安全的多线程计数器增量

例如,我有一些由多个线程同时计算的工作.

出于演示目的,工作在while循环内执行.在单次迭代中,每个线程执行其自己的工作部分,在下一次迭代开始之前,计数器应该递增一次.

我的问题是每个线程都会更新计数器.

由于这似乎是一件相对简单的事情,我认为有一种"最佳实践"或常用方法可以解决这个问题?

下面是一些示例代码来说明问题并帮助讨论.(我使用提升线程)

class someTask {
public:
    int mCounter; //initialized to 0
    int mTotal; //initialized to i.e. 100000
    boost::mutex cntmutex;                
    int getCount()
    {
            boost::mutex::scoped_lock lock( cntmutex );
            return mCount;
    }
    void process( int thread_id, int numThreads )
    {
        while ( getCount() < mTotal )
        {
            // The main task is performed here and is divided 
            // into sub-tasks based on the thread_id and numThreads

                            // Wait for all thread to get to this point

            cntmutex.lock();
            mCounter++;  // < …
Run Code Online (Sandbox Code Playgroud)

c++ algorithm multithreading boost boost-thread

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

从boost线程在主线程上运行函数并将参数传递给该函数

我有一些代码在一个boost线程中运行,它修改了主线程处理的东西,这个东西不起作用,这是有意义的.

在android上我会有Handler一个消息队列,它将在主线程上执行我的代码,我可以将任何我想要的参数传递给这个处理程序.

我想用提升来做同样的事情

所以在我的主线程上我做了以下事情:

boost::thread workerThread(boost::bind(&SomeClass::pollService, this));
Run Code Online (Sandbox Code Playgroud)

我的pollService方法:

SomeClass::pollService()
{
     //get some stuff from a web service
     //parse the json response
     //NEEDED part: call a function to be executed on the main thread and hand it some functions
}
Run Code Online (Sandbox Code Playgroud)

PS我看了很多io_service.post例子,我仍然不知道怎么做,而且我也读了一个说要用的答案,asio strand但我也无法理解.

有人可以为我愚蠢吗?请不要写那么抽象的东西,我不明白,我没有经验.谢谢

boost boost-bind boost-thread boost-asio

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

带有阻塞的c ++工作队列

这个问题应该比我的前几个简单一些.我在我的程序中实现了以下工作队列:

Pool.h:

// tpool class
// It's always closed. :glasses:
#ifndef __POOL_H
#define __POOL_H
class tpool {
    public:
        tpool( std::size_t tpool_size );
        ~tpool();
        template< typename Task >
        void run_task( Task task ){
        boost::unique_lock< boost::mutex > lock( mutex_ );
            if( 0 < available_ ) {
                --available_;
                io_service_.post( boost::bind( &tpool::wrap_task, this, boost::function< void() > ( task ) ) );
            }
        }
    private:
        boost::asio::io_service io_service_;
        boost::asio::io_service::work work_;
        boost::thread_group threads_;
        std::size_t available_;
        boost::mutex mutex_;
        void wrap_task( boost::function< void() > task );
};
extern tpool …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading boost-thread boost-asio threadpool

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

c ++在fork之后初始化全局对象

我有一个程序,它使用工作队列来执行任务,并且应该作为守护进程运行.我使用以下代码实现了这一目标:

bool seedDaemon() {
        using namespace std;
        int childpid = 0;
        pid_t pid = 0;
        if( ( childpid = fork() ) < 0 ) {
                return false;
        }
        else if( childpid > 0 ){
                exit(0);
        }
        setsid();
        umask(0);
        std::cout<< "[OK]\n";
        close( fileno(stderr) );
        close( fileno(stdout) );
        close( STDIN_FILENO );
        return true;
}
Run Code Online (Sandbox Code Playgroud)

这将关闭原始进程并启动另一个进程.但是,这导致了我为执行任务而创建的线程在fork之后没有出现的问题.我的工作队列是全局实例化的,所有其他值和内存地址都正确地复制到子节点,但线程没有.

作为参考,这是池类:

pool.h:

#ifndef __POOL_H
#define __POOL_H
class tpool {
        public:
                tpool( std::size_t tpool_size );
                ~tpool();
                template< typename Task >
                void run_task( Task task ){ // add item …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading boost daemons boost-thread

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

用C++提升 - 好奇的互斥行为

我正在尝试Boost线程,因为据我所知,我可以编写一个多线程Boost应用程序并在Windows或Linux中编译它,而pthreads我更熟悉的是,它严格用于*NIX系统.

我有以下示例应用程序,它是从另一个SO问题中借用的:


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

#define NAP_DURATION    (10000UL)   // 10ms

boost::mutex io_mutex;

void count(int id)
{
    for (int i = 0; i < 1000; ++i)
    {
        boost::mutex::scoped_lock lock(io_mutex);
        std::cout << "Thread ID:" << id << ": " << i << std::endl;
        if (id == 1)
        {
            std::cout << "I'm thread " << id << " and I'm taking a short nap" << std::endl;
            usleep(NAP_DURATION);
        }
        else
        { …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading boost boost-thread boost-mutex

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

boost::thread 与 std::thread 与 pthread

b/w boost::threadstd::thread(C++11) 和pthread基于 Linux 的应用程序的高 CPU 吞吐量(读取:大量浮点运算)的权衡是什么?什么时候应该使用一种实现而不是其他实现?

这里的用例是在连续内存的缓冲区(或指向缓冲区的指针)上调用例程,做一些工作,然后返回——在多线程实现中。

c++ multithreading pthreads boost-thread c++11

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