fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <pthread.h>
  4. #include <thread> // std::this_thread::sleep_for
  5. #include <chrono>
  6. #include <iostream>
  7.  
  8. // std::mutex m1;
  9. // std::mutex m2;
  10.  
  11. void* function( void* arg )
  12. {
  13. printf( "This is thread %ld\n", pthread_self() );
  14. return( 0 );
  15. }
  16.  
  17. int main( void )
  18. {
  19. pthread_attr_t attr;
  20.  
  21. pthread_attr_init( &attr );
  22. pthread_attr_setdetachstate(
  23. &attr, PTHREAD_CREATE_DETACHED );
  24. pthread_t thread;
  25. pthread_create( &thread, &attr, &function, NULL );
  26.  
  27. int policy = SCHED_FIFO; ;
  28. int const min_value{ sched_get_priority_min(policy) };
  29. std::cout << "Min prio:" << min_value << '\n';
  30. int const max_value{ sched_get_priority_max(policy) };
  31. std::cout << "Max prio:" << max_value << '\n';
  32.  
  33. sched_param sch;
  34. pthread_getschedparam(thread, &policy, &sch);
  35. sch.sched_priority = 20;
  36. if (pthread_setschedparam(thread, policy, &sch)) {
  37. std::cout << "Failed to setschedparam: " << '\n';
  38. }
  39. std::cout << "Policy: " << policy << '\n';
  40. /* Allow threads to run for 60 seconds. */
  41. //sleep( 60 );
  42. std::chrono::microseconds(200000);
  43. return EXIT_SUCCESS;
  44. }
Success #stdin #stdout 0.01s 5252KB
stdin
Standard input is empty
stdout
Min prio:1
Max prio:99
Failed to setschedparam: 
Policy: 0