简体   繁体   English

具有c ++ 11的pthread_setschedparam的std :: thread

[英]std::thread with pthread_setschedparam with c++ 11

I wanted to set priority of a custom threadpool developed. 我想设置开发的自定义线程池的优先级。 so I found to set the priority need to use pthred's pthread_setschedparam method. 所以我发现设置优先级需要使用pthred的pthread_setschedparam方法。 Since pthread_t and native_handle_type is different I did something like this - 由于pthread_tnative_handle_type不同,所以我做了这样的事情-

void SetPriority(int id, int priority, int policy) {
            bool ret = true;
            sched_param sch_params;
            sch_params.sched_priority = 20;
            pthread_t p;
            p.p = threads_[id].native_handle();
            if (pthread_setschedparam(p, SCHED_FIFO, &sch_params)) {
                std::cerr << "Failed to set Thread scheduling : " << std::strerror(errno) << std::endl;
            }

        }

I am getting Access violation for pthreadVC2.dll . 我收到pthreadVC2.dll访问冲突。

To test if I am with the right windows version's dll I made did some test like this - 为了测试我是否使用了正确的Windows版本的dll,我做了如下测试-

int main(){
pthread_t thread1, thread2;
    int thr = 1;
    int thr2 = 2;
    // start the threads
    pthread_create(&thread1, NULL, *threadfn, (void *)thr);
    pthread_create(&thread2, NULL, *threadfn, (void *)thr2);

    sched_param sch_params;
    sch_params.sched_priority = 20;

    if (pthread_setschedparam(thread1, SCHED_FIFO, &sch_params)) {
        std::cerr << "Failed to set Thread scheduling : " << std::strerror(errno) << std::endl;
    }

    // wait for threads to finish
    int po1, po2; 
    sched_param sched_param1, sched_param2;
    pthread_getschedparam(thread1,&po1,&sched_param1);
    pthread_getschedparam(thread2, &po2, &sched_param2);
    cout << "thread 1:policy - " << po1 << " priority - " << sched_param1.sched_priority << endl;
    cout << "thread 2:policy - " << po2 << " priority - " << sched_param2.sched_priority << endl;

    pthread_join(thread1, NULL);
    pthread_join(thread2, NULL);
    return 0;
}

I found error no is 1 (priority not set). 我发现错误编号为1(未设置优先级)。 And both thread having policy 0 & priority 0 并且两个线程都具有策略0和优先级0

What is the proper way to set priority of a pthread and how can I use this functionality from std::thread ? 设置pthread优先级的正确方法是什么?如何从std :: thread使用此功能?

Edit: Windows environment setup: Using Visual Studio 2015 with windows 10. Downloaded pthread (pthreads-w32-2-9-1-release.zip) from ftp://sourceware.org/pub/pthreads-win32/ . 编辑:Windows环境设置:在Windows 10中使用Visual Studio2015。从ftp://sourceware.org/pub/pthreads-win32/下载了pthread(pthreads-w32-2-9-1-release.zip)。 project setting- included Additional Include Directories Linker > General and set the location of the pthread library Additional dependecies : pthreadVC2.lib and pthreadVCE2.lib and copied pthreadVC2.dll in the project directory. 项目设置-包括Additional Include Directories Linker> General,并设置pthread库的位置其他依赖项:pthreadVC2.lib和pthreadVCE2.lib以及复制的pthreadVC2.dll在项目目录中。

Here is an example program that shows how to use pthread_getschedparam and pthread_setschedparam with std::thread, taken from here . 这是一个示例程序,该示例程序演示如何将pthread_getschedparam和pthread_setschedparam与std :: thread一起使用,摘自此处

#include <thread>
#include <mutex>
#include <iostream>
#include <chrono>
#include <cstring>
#include <pthread.h>

std::mutex iomutex;
void f(int num)
{
    std::this_thread::sleep_for(std::chrono::seconds(1));

    sched_param sch;
    int policy; 
    pthread_getschedparam(pthread_self(), &policy, &sch);
    std::lock_guard<std::mutex> lk(iomutex);
    std::cout << "Thread " << num << " is executing at priority "
              << sch.sched_priority << '\n';
}

int main()
{
    std::thread t1(f, 1), t2(f, 2);

    sched_param sch;
    int policy; 
    pthread_getschedparam(t1.native_handle(), &policy, &sch);
    sch.sched_priority = 20;
    if (pthread_setschedparam(t1.native_handle(), SCHED_FIFO, &sch)) {
        std::cout << "Failed to setschedparam: " << std::strerror(errno) << '\n';
    }

    t1.join(); t2.join();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM