Boost logo

Boost Users :

Subject: [Boost-users] [Fiber] Can a fiber promise be set in a different OS thread on which has no fibers running
From: lin li (sdeber_at_[hidden])
Date: 2017-05-11 06:51:36


I have tested the following example code with Boost 1.61. And it worked (ignore the potential race conditions when accessing the variable 'p'). But I am not sure the fiber promise can be used in this way or I am just being lucky.

#include <cstdlib>
#include "boost/fiber/fiber.hpp"
#include "boost/fiber/future.hpp"
#include "boost/thread.hpp"
#include <set>
#include <iostream>
#include <boost/date_time/posix_time/posix_time_duration.hpp>
#include <boost/thread/pthread/mutex.hpp>
#include <boost/thread/lock_guard.hpp>
#include "boost/thread/pthread/thread_data.hpp"
#include "boost/chrono/duration.hpp"
using namespace std;

boost::fibers::promise<int>* p;

void wait()
{

    p = new boost::fibers::promise<int>;

    auto future = p->get_future();
    auto status = future.wait();
    std::cout << "Value: " << future.get() << std::endl;
    delete p;
}

void f()
{
    boost::fibers::fiber fiberJob(&wait);
    fiberJob.join();

}

void setf()
{
  // Is it safe to do this?
    boost::this_thread::sleep(boost::posix_time::seconds(5));
    p->set_value(20);
}

void f1()
{
    boost::fibers::fiber fiberJob(&setf);
    fiberJob.join();
}

int main(int argc, char** argv)
{

    boost::thread t(&f);
    setf();
    cout << "Here" << endl;
    t.join();
    cout << "Here 2" << endl;
    return 0;
}



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net