multithreading - c++ multi threading priority implementation failed -


=)

I'm a new user here, and I'm new to c ++, That's why this is a difficult task for me ... So I'm asking you some questions! =)

I am doing a job for the school, who asks me to apply the threading priority to this:

  #include & lt; pthread.h & gt; # Include & lt; Stdio.h & gt; # Include & lt; schedule.h & gt; Int shade_yilild (zero); // parameter for print_function Struct char_print_parms {char characters; // char int count to print; // time to print); Zero * char_print (zero * parameter) {int i; Struct char_print_parms * p; P = (straight char_print_parms *) parameter; (I = 0; i & lt; p-> count; ++ i) {fputc (p->; character, stderr); Sched_yield (); } Return tap; } Int main () {pthread_t thread1_id, thread2_id; Struct char_print_parms thread1_args, thread2_args; // 200x to print a new thread. Thread 1_args.character = 'x'; Thread 1_args.count = 200; Pthread_create (& thread1_id, NULL, & char_print, and thread1_args); // Create a new thread to print 200 o thread 2_args.character = 'o'; Thread 2_args.count = 200; Pthread_create (& amp; Thread 2_ID, Faucet, and Four_print, and Thread 2_Reg); // waits to complete the pthread_join (thread1_id, NULL) for the main thread; Pthread_join (thread 2_id, NULL); Return 0; }   

This gives "olloxoxoxo ..." and so on.

The aim is to get "o" until its purpose is complete.

What I did:

  #include & lt; pthread.h & gt; # Include & lt; Stdio.h & gt; # Include & lt; schedule.h & gt; Int shade_yilild (zero); // parameter for print_function Struct char_print_parms {char characters; // char int count to print; // time to print); Zero * char_print (zero * parameter) {int i; Struct char_print_parms * p; P = (straight char_print_parms *) parameter; (I = 0; i & lt; p-> gt; ++ i) {fputc (p->; character, stderr); sched_yield (); } Return tap; } Int main () {pthread_t thread1_id, thread2_id; Struct char_print_parms thread1_args, thread2_args; // new code line straight sched_param param; Pthread_attr_t pta; pthread_attr_init (& amp; PTA); Pthread_attr_getschedparam (& amp; pta; and Ultimate); // Create a new thread to print the new code lines // 200X. Thread 1_args.character = 'x'; Thread 1_args.count = 200; // More new code line param.sched_priority = 0; Pthread_attr_setschedparam (& amp; pta; and Ultimate); Pthread_setschedparam (thread 1_id, SCHED_OTHER, & param); // more new code lines end pthread_create (& thread1_id, NULL, & char_print, and thread1_args); // Create a new thread to print 200 o thread 2_args.character = 'o'; Thread 2_args.count = 200; // More new code line 2 param.sched_priority = 10; Pthread_attr_setschedparam (& amp; pta; and Ultimate); Pthread_setschedparam (thread2_id, SCHED_OTHER, and Ultimate); // More new code lines end 2 pthread_create (& thread2_id, Null, & char_print, and thread2_args); // waits to complete the pthread_join (thread1_id, NULL) for the main thread; pthread_join (thread 2_id, NULL); Return 0; }   

Finally, I try to compile and run, but this is an error:

Segmentation failed (core dump)

Once again, I'm new to c ++ and my english is not very good, but I want to try to understand why it does not work. Any help is welcome!

When you call pthread_setschedparam , the thread ID variable is still in progress Have not been done. So you are trying to change the parameters on an indefinite thread.

The easiest way to change the priority is to do it in the thread itself.


Regarding the unspecified local variables, their values ​​are uncertain until they are explicitly launched. Leads to using undetermined local variables.

If you see an example in it that has been called to determine the priority of your thread, you can either use it to add a field to the nearby structure in the thread, which has priority, or a wrapper thread -function that sets the priority and then calls the actual thread function.

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -