【问题标题】:c++ multi threading priority implementation failedc++多线程优先级实现失败
【发布时间】:2014-04-22 10:45:06
【问题描述】:

=)

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

【问题讨论】:

  • 运行 gdb [yourprogram] 并按 r。然后它应该准确地告诉你哪里以及哪里出了问题。您可能必须使用-g 选项编译您的程序。见gdb tutorial。 (从错误消息中我得出结论,您在 Unix 上使用 gcc,如果不是,请说明您使用的是什么。)
  • @nwp 感谢您的帮助!我在 Ubuntu 上的 CodeBlocks 上运行此代码!它可以完成所有工作 =) 但是如果我不使用 CodeBlocks,我会做类似... 编译:gcc -c nchars.c 可执行文件:gcc -o nchars -lpthread nchars.o 它通常是我看到使用的(根据教授的说法…………)

标签: c++ multithreading thread-priority


【解决方案1】:

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

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


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

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

【讨论】:

  • 感谢您的帮助约阿希姆! =) 我想我明白你的意思了。因此我更改了代码顺序,将该部分放在创建线程的下方。但是当您说“更改优先级的最简单方法是在线程本身中执行此操作。”时,这让我意识到我理解其中的 0...lol
  • @user3559945 用一些提示更新了答案。
【解决方案2】:

你应该先调用pthread_create (&amp;thread1_id, NULL, &amp;char_print, &amp;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

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

【讨论】:

  • 感谢您的宝贵时间和回复! =) 我试过了,但结果和以前一样。
  • 谢谢你的朋友。我测试了,但结果总是一样。你认为我做错了什么?
猜你喜欢
  • 2011-06-25
  • 2013-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多