Boost logo

Boost Users :

Subject: [Boost-users] Trying to use Boost::thread
From: Allan Nielsen (a_at_[hidden])
Date: 2011-07-22 17:41:19


Hi,

I am trying to use boost::thread to copy data from one stream to another.

For this I have written the following code, but can can not compile it
and I do not under stand why...

Is there an better way to do this?

--
Allan
#include <fstream>
#include <iostream>
#include "Poco/Net/HTTPClientSession.h"
#include "Poco/Net/HTTPRequest.h"
#include "Poco/Net/HTTPResponse.h"
#include "Poco/StreamCopier.h"
#include "Poco/Path.h"
#include "Poco/URI.h"
#include <boost/thread.hpp>
using namespace std;
using namespace Poco;
using namespace Poco::Net;
struct Test {
    Test (const std::string & s_url) :
        uri(s_url),
        path(uri.getPathAndQuery()),
        session(uri.getHost(), uri.getPort())  {
        if (path.empty()) path = "/";
        HTTPRequest req(HTTPRequest::HTTP_GET, path, HTTPMessage::HTTP_1_1);
        session.sendRequest(req);
        // Compiler error
        // boost::thread(boost::ref(*this)); // this compiles
        boost::packaged_task<int> pt(boost::ref(*this));
        boost::unique_future<int> fi=pt.get_future();
        boost::thread task(boost::move(pt));
    }
    int operator()() {
        ofstream null("/dev/null");
        std::istream& rs = session.receiveResponse(res);
        StreamCopier::copyStream(rs, null);
    }
    URI uri;
    std::string path;
    HTTPClientSession session;
    HTTPResponse res;
};
int main(int argc, const char *argv[]) {
    Test test("http://localhost/gmbfs");
    // TODO, sync
    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