c++多线程优先级实现失败



=)

我是这里的新用户,我是c++的新手,所以对我来说有点难。。。所以我问你们一些问题

我正在为学校做一项工作,要求我实现线程优先级:

#include <pthread.h>
#include <stdio.h>
#include <sched.h>
int sched_yield(void);
// Parameters to print_function.
struct char_print_parms{
    char character; // char to print
    int count; // times to print
};
void* char_print (void* parameters){
    int i;
    struct char_print_parms* p;
    p = (struct char_print_parms*) parameters;
    for (i = 0; i < p->count; ++i){
        fputc (p->character, stderr);
        sched_yield();
    }
    return NULL;
}
int main (){
    pthread_t thread1_id,thread2_id;
    struct char_print_parms thread1_args,thread2_args;
// Create a new thread to print 200 x's.
    thread1_args.character = 'x';
    thread1_args.count = 200;
    pthread_create (&thread1_id, NULL, &char_print, &thread1_args);
// Create a new thread to print 200 o's.
    thread2_args.character = 'o';
    thread2_args.count = 200;
    pthread_create (&thread2_id, NULL,
    &char_print, &thread2_args);
// main waits for the threads to complete
    pthread_join(thread1_id, NULL);
    pthread_join(thread2_id, NULL);
    return 0;
}

这给出的是"oxoxoxo…"等

目标是获得更多的"o",直到它结束。

我所做的是:

#include <pthread.h>
#include <stdio.h>
#include <sched.h>
int sched_yield(void);
// Parameters to print_function.
struct char_print_parms{
    char character; // char to print
    int count; // times to print
};
void* char_print (void* parameters){
    int i;
    struct char_print_parms* p;
    p = (struct char_print_parms*) parameters;
    for (i = 0; i < p->count; ++i){
        fputc (p->character, stderr);
        sched_yield();
    }
    return NULL;
}
int main (){
    pthread_t thread1_id,thread2_id;
    struct char_print_parms thread1_args,thread2_args;

    //new code lines
    struct sched_param param;
    pthread_attr_t pta;
    pthread_attr_init(&pta);
    pthread_attr_getschedparam(&pta, &param);
    //end of new code lines
// Create a new thread to print 200 x's.
    thread1_args.character = 'x';
    thread1_args.count = 200;
    //more new code lines
    param.sched_priority = 0;
    pthread_attr_setschedparam(&pta, &param);
    pthread_setschedparam(thread1_id, SCHED_OTHER, &param);
    //end of more new code lines
    pthread_create (&thread1_id, NULL, &char_print, &thread1_args);

// Create a new thread to print 200 o's.
    thread2_args.character = 'o';
    thread2_args.count = 200;
    //more new code lines 2
    param.sched_priority = 10;
    pthread_attr_setschedparam(&pta, &param);
    pthread_setschedparam(thread2_id, SCHED_OTHER, &param);
    //end of more new code lines 2
    pthread_create (&thread2_id, NULL,
    &char_print, &thread2_args);
// main waits for the threads to complete
    pthread_join(thread1_id, NULL);
    pthread_join(thread2_id, NULL);
    return 0;
}

最后,我编译并尝试运行,但出现了一个错误:

分割失败(堆芯转储)

再一次,我是c++的新手,我的英语不是很好,但我想试着理解为什么这不起作用。欢迎任何帮助!

调用pthread_setschedparam时,线程id变量尚未初始化。因此,您试图更改不确定线程上的参数。

更改优先级的最简单方法是在线程本身中执行。


关于未初始化的局部变量,在显式初始化之前,它们的值是不确定的。使用未初始化的局部变量会导致未定义的行为

如果您在pthread_setschedparam中看到该示例,您会看到它被pthread_self调用以设置自己的线程优先级。您可以使用它在传递给包含优先级的线程的结构中添加一个字段,或者使用一个包装线程函数来设置优先级,然后调用实际的线程函数。

您应该首先调用pthread_create (&thread1_id, NULL, &char_print, &thread1_args);来创建线程thread1_id,然后您可以设置该线程的优先级。我修改了代码,它运行良好。

thread1_args.character = 'x';
thread1_args.count = 200;
pthread_create (&thread1_id, NULL, &char_print, &thread1_args);
//more new code lines
param.sched_priority = 0;
pthread_attr_setschedparam(&pta, &param);
pthread_setschedparam(thread1_id, SCHED_OTHER, &param);
// Create a new thread to print 200 o's.
thread2_args.character = 'o';
thread2_args.count = 200;
pthread_create (&thread2_id, NULL, &char_print, &thread2_args);
//more new code lines 2
param.sched_priority = 10; 
pthread_attr_setschedparam(&pta, &param);
pthread_setschedparam(thread2_id, SCHED_OTHER, &param);

您可以阅读此链接:https://access.redhat.com/site/documentation/en-US/Red_Hat_Enterprise_MRG/2/html/Realtime_Reference_Guide/chap-Realtime_Reference_Guide-Priorities_and_policies.html.

我测试了这段代码,但每次的输出都不一样。

最新更新