通过strand崩溃的boost :: asio :: io_service同步

时间:2014-05-14 07:30:34

标签: c++ multithreading boost thread-safety boost-asio

我正在尝试使用boost asio和io_service来运行我的任务。

我制作了代码:

#include <boost/asio.hpp>
#include <thread>
#include <functional>
#include <iostream>
#include <vector>

void work(boost::asio::io_service::strand& sync);
void sentCompleted(boost::asio::io_service::strand& sync);
void mySend(boost::asio::io_service::strand& sync);

void work(boost::asio::io_service::strand& sync)
{
    std::cout << "id: " << std::this_thread::get_id() << ", work()" << std::endl;

    mySend(sync);
    sync.post(sync.wrap(std::bind(&work, std::ref(sync))));
}

void sentCompleted(boost::asio::io_service::strand& sync)
{
    std::cout << "id: " << std::this_thread::get_id() << ", sentCompleted()" << std::endl;
}

void mySend(boost::asio::io_service::strand& sync)
{
    std::cout << "id: " << std::this_thread::get_id() << ", mySend()" << std::endl;

    sync.post(sync.wrap(std::bind(&sentCompleted, std::ref(sync))));
}

int main()
{
    boost::asio::io_service ioService;
    boost::asio::io_service::strand sync(ioService);
    sync.post(sync.wrap(std::bind(&work, std::ref(sync))));

    std::vector<std::thread*> threads;
    for(size_t i = 0; i < 4; ++i)
    {
        threads.push_back(new std::thread(std::bind(static_cast<size_t (boost::asio::io_service::*)()>(&boost::asio::io_service::run), &ioService)));
    }

    std::for_each(threads.begin(), threads.end(), [](std::thread* thread) { thread->join(); });

    return 0;
}

但程序崩溃了输出:

  

id:140320676251392,work()id:140320676251392,mySend()id:   140320651073280,sentCompleted()id:140320676251392,work()id:   140320676251392,mySend()id:140320651073280,sentCompleted()id:   140320676251392,work()id:140320676251392,mySend()id:   140320651073280,sentCompleted()id:140320676251392,work()id:   140320676251392,mySend()id:140320651073280,sentCompleted()id:   140320676251392,work()id:140320676251392,mySend()id:   140320651073280,sentCompleted()id:140320676251392,work()id:   140320676251392,mySend()id:140320651073280,sentCompleted()id:   140320676251392,work()id:140320676251392,mySend()id:   140320651073280,sentCompleted()id:140320676251392,work()id:   140320676251392,mySend()id:140320651073280,sentCompleted()id:   140320676251392,work()id:140320676251392,mySend()id:   140320651073280,sentCompleted()id:140320676251392,work()id:   140320676251392,mySend()id:140320651073280,sentCompleted()id:   140320676251392,work()id:140320676251392,mySend()id:   140320651073280,sentCompleted()id:140320676251392,work()id:   140320676251392,mySend()id:140320651073280,sentCompleted()id:   140320676251392,work()id:140320676251392,mySend()id:   140320651073280,sentCompleted()id:140320676251392,work()id:   140320676251392,mySend()id:140320651073280,sentCompleted()id:   140320676251392,work()id:140320676251392,mySend()Aborted(core   倾倒)

以为我错误地使用了strand,所以我尝试通过recursive_mutex同步io_service :: run处理程序,情况也一样......

代码:

#include <boost/asio.hpp>
#include <thread>
#include <functional>
#include <iostream>
#include <mutex>
#include <vector>

class Sender
{
public:
    Sender(boost::asio::io_service& ioService):
        ioService_(ioService) {}

    void work()
    {
        sync_.lock();
        std::cout << "id: " << std::this_thread::get_id() << ", work()" << std::endl;
        mySend();
        sync_.unlock();

        ioService_.post(std::bind(&Sender::work, this));
    }

    void mySend()
    {
        std::cout << "id: " << std::this_thread::get_id() << ", mySend()" << std::endl;
        ioService_.post(std::bind(&Sender::sentCompleted, this));
    }

    void sentCompleted()
    {
        sync_.lock();
        std::cout << "id: " << std::this_thread::get_id() << ", sentCompleted()" << std::endl;
        sync_.unlock();
    }

private:
    boost::asio::io_service &ioService_;
    std::recursive_mutex sync_;
};

int main()
{
    boost::asio::io_service ioService;

    Sender sender(ioService);
    sender.work();

    std::vector<std::thread*> threads;
    for(size_t i = 0; i < 4; ++i)
    {
        threads.push_back(new std::thread(std::bind(static_cast<size_t (boost::asio::io_service::*)()>(&boost::asio::io_service::run), &ioService)));
    }

    std::for_each(threads.begin(), threads.end(), [](std::thread* thread) { thread->join(); });

    return 0;
}

输出:

id: 140231283394336, work() id: 140231283394336, mySend() id:
140231283390208, sentCompleted() id: 140231283390208, work() id:
140231283390208, mySend() id: 140231283390208, work() id:
140231283390208, mySend() id: 140231151974144, sentCompleted() id:
140231283390208, work() id: 140231283390208, mySend() id:
140231283390208, sentCompleted() id: 140231283390208, work() id:
140231283390208, mySend() id: 140231283390208, sentCompleted() id:
140231283390208, work() id: 140231283390208, mySend() id:
140231283390208, sentCompleted() id: 140231283390208, work() id:
140231283390208, mySend() id: 140231151974144, sentCompleted() id:
140231283390208, work() id: 140231283390208, mySend() id:
140231151974144, sentCompleted() id: 140231151974144, sentCompleted()
id: 140231283390208, work() id: 140231283390208, mySend() id:
140231151974144, sentCompleted() Aborted (core dumped)

有人可以解释一下我做错了什么吗?提前致谢!最好的问候。

0 个答案:

没有答案