增加卡萨布兰卡的工作线程数

时间:2019-05-29 11:39:18

标签: c++ casablanca cpprest-sdk

我正在cpprestsdk的帮助下使用threadPool。 以下是使用内部线程池的测试代码。 TaskATaskB是虚拟任务。添加了延迟以模拟任务的长时间处理。

测试代码。

#include "cpprest/asyncrt_utils.h"
#include <map>
using namespace std;
using namespace utility;
void TaskA() {
    sleep(rand()%5);
    cout <<"taskA executing .."<< endl;
}
void TaskB() {
    sleep(rand()%5);
    cout <<"taskB executing .."<< endl;
}
std::map<int,std::function<void(void)>> taskIdMap = {
    {0,     TaskA},
    {1,     TaskB}
};
int main() {
    srand(time(NULL));
    int cnt = 100;  // 100 task to dispatch
    while(cnt--) {
        int taskId = rand()%2;  // simulates an receipt of an event or task
        auto func = taskIdMap[taskId];
        pplx::task<int>([func,taskId]()
        {
            func();
            return taskId;

        })
        .then([](pplx::task<int> prevTask)
        {
            auto id = prevTask.get();
            cout <<(id == 0?"TaskA is done !":"TaskB is done !") <<endl;
        });
    }
    cout<<"No more task to dispatch. Main thread is free now"<<endl;
    getchar();
}


/*
system Ubuntu 18.04 =>
CMakeLists.txt
    cmake_minimum_required (VERSION 3.5)
    set(CMAKE_BUILD_TYPE Debug)
    project(TEST)
    message(STATUS "Compiling TestFile : ${PROJECT_NAME}")
    set (CMAKE_CXX_STANDARD 14)
    message(STATUS "Project Directory: ${PROJECT_SOURCE_DIR}")
    set (CMAKE_BINARY_DIR ${CMAKE_SOURCE_DIR}/build)
    set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
    add_executable(${PROJECT_NAME} test.cpp)
    set(REST_LIBRARIES "-lcpprest")
    set(CMAKE_CXX_FLAGS "-std=c++14 -Wall -g -fsanitize=address")
    target_link_libraries(${PROJECT_NAME} ${REST_LIBRARIES})
*/

如何增加线程数,默认情况下在我的系统(Ubuntu)中它创建40个线程。 有一个函数调用ref,但是我不确定如何使用它。

0 个答案:

没有答案