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

时间:2014-04-22 10:45:06

标签: c++ multithreading thread-priority

=)

我是这里的新用户,我是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 ++的新手,我的英语不是很好,但我想尝试理解为什么这不起作用。欢迎任何帮助!

2 个答案:

答案 0 :(得分:0)

当您致电pthread_setschedparam时,线程ID变量尚未初始化。因此,您尝试在不确定的线程上更改参数。

更改优先级的最简单方法是在线程中自行完成。


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

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

答案 1 :(得分:0)

您应该首先调用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

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