예제 #1
0
int main() {
    boost::thread pop_sync1(&popper_sync);
    boost::thread pop_sync2(&popper_sync);
    boost::thread pop_sync3(&popper_sync);

    boost::thread push1(&pusher);
    boost::thread push2(&pusher);
    boost::thread push3(&pusher);

    // Waiting for all the tasks to push
    push1.join();
    push2.join();
    push3.join();
    g_queue.flush(); 

    // Waiting for all the tasks to pop
    pop_sync1.join();
    pop_sync2.join();
    pop_sync3.join();


    // Asserting that no tasks remained,
    // and falling though without blocking
    assert(!g_queue.try_pop_task());

    g_queue.push_task(&do_nothing);
    // Asserting that there is a task,
    // and falling though without blocking
    assert(g_queue.try_pop_task());

}
/**
 * Process request; worker (thread)
 *
 * @param queue
 */
void process_request(work_queue& queue) {
  while (running) {
    request_data::pointer request(queue.get());
    if (request) {

      // some heavy work!
      std::this_thread::sleep_for(std::chrono::seconds(10));

      request->conn->set_status(server::connection::ok);
      request->conn->write("Hello, world!");
    }

    std::this_thread::sleep_for(std::chrono::microseconds(1000));
  }
}
예제 #3
0
/**
 * Process request; worker (thread)
 *
 * @param queue
 */
void process_request(work_queue& queue)
{
  Worker worker;
  while(!boost::this_thread::interruption_requested()) {
    request_data::pointer p_req(queue.get());
    if (p_req) {

      // some heavy work!
      long long result = worker.work();
      std::ostringstream body;
      body << "Current length: " << result << std::endl;

      p_req->conn->set_status(server::connection::ok);
      server::response_header headers[] = { {"Content-Type", "text/plain"}, {"Content-Length", std::to_string(body.tellp())} };
      p_req->conn->set_headers(boost::make_iterator_range(headers, headers+2));
      p_req->conn->write(body.str());
    }

    boost::this_thread::sleep(boost::posix_time::microseconds(1000));
  }
}
void process_request(work_queue& queue)
{
    using namespace std;

    while(!boost::this_thread::interruption_requested()) {
        request_data::pointer p_req(queue.get());
        if (p_req) {
            cout << "Received client p_req from " << p_req->req.source << endl; // source 已经包含port
            cout << "destination = " << p_req->req.destination << endl;  // 去除了URL port
            cout << "method = " << p_req->req.method << endl;
            cout << "http version = " << (uint32_t)(p_req->req.http_version_major) 
                 << "." << (uint32_t)(p_req->req.http_version_minor) << endl;
            cout << "headers:" << endl;
            for (const auto &header : p_req->req.headers)
                cout << header.name << " = " << header.value << endl;

            size_t nRead;
            for (const auto &v : p_req->req.headers) {
                if (boost::iequals(v.name, "Content-Length")) {
                    nRead = boost::lexical_cast<size_t>(v.value);
                } // if
            } // for

            WorkItemPtr pWork( new WorkItem(p_req->req.source, p_req->req.destination, nRead) );
            pWork->readBody(p_req->conn);

            // some heavy work!
            boost::this_thread::sleep(boost::posix_time::seconds(3));

            p_req->conn->set_status(server::connection::ok);
            p_req->conn->write("Hello, world!");
        }

        boost::this_thread::sleep(boost::posix_time::microseconds(1000));
    }
}
예제 #5
0
void popper_sync() {
    for (std::size_t i = 0; i < tests_tasks_count; ++i) {
        g_queue.pop_task() // Getting task
                (); // Executing task
    }
}
예제 #6
0
void pusher() {
    for (std::size_t i = 0; i < tests_tasks_count; ++i) {
        // Adding task to do nothing
        g_queue.push_task(&do_nothing);
    }
}