fork download
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. #include <numeric>
  5. #include <pthread.h>
  6.  
  7. timespec start, running, joined, sorted, sent, received;
  8. std::vector<int> v(1000, 1);
  9. pthread_mutex_t mx = PTHREAD_MUTEX_INITIALIZER;
  10. pthread_cond_t cv = PTHREAD_COND_INITIALIZER;
  11. int msg, ready;
  12.  
  13. template<typename C>
  14. double avg(const C& v)
  15. {
  16. return std::accumulate(v.begin(), v.end(), 0.0)/v.size();
  17. }
  18.  
  19. // result in us
  20. double diff(timespec x, timespec y)
  21. {
  22. return (y.tv_sec*1000000.0 + y.tv_nsec/1000.0)
  23. - (x.tv_sec*1000000.0 + x.tv_nsec/1000.0);
  24. }
  25.  
  26. extern "C" void* f1(void*)
  27. {
  28. clock_gettime(CLOCK_REALTIME, &running);
  29. return NULL;
  30. }
  31.  
  32. extern "C" void* f2(void*)
  33. {
  34. pthread_mutex_lock(&mx);
  35. ready = 1;
  36. pthread_cond_signal(&cv);
  37.  
  38. while(msg == 0)
  39. pthread_cond_wait(&cv, &mx);
  40. clock_gettime(CLOCK_REALTIME, &received);
  41. pthread_mutex_unlock(&mx);
  42. return NULL;
  43. }
  44.  
  45. int main()
  46. {
  47. std::partial_sum(v.begin(), v.end(), v.begin()); // poor man's std::iota
  48.  
  49. pthread_t hndl;
  50. std::vector<double> starts, joins, sorts, sends;
  51. for(int n = 0; n < 100000; ++n)
  52. {
  53. std::random_shuffle(v.begin(), v.end());
  54.  
  55. clock_gettime(CLOCK_REALTIME, &start);
  56. pthread_create(&hndl, NULL, f1, NULL);
  57. pthread_join(hndl, NULL);
  58. clock_gettime(CLOCK_REALTIME, &joined);
  59. std::sort(v.begin(), v.end());
  60. clock_gettime(CLOCK_REALTIME, &sorted);
  61.  
  62. pthread_create(&hndl, NULL, f2, NULL);
  63. pthread_mutex_lock(&mx);
  64. while(!ready)
  65. pthread_cond_wait(&cv, &mx);
  66. msg=1;
  67. clock_gettime(CLOCK_REALTIME, &sent);
  68. pthread_cond_signal(&cv);
  69. pthread_mutex_unlock(&mx);
  70. pthread_join(hndl, NULL);
  71. msg=ready=0;
  72.  
  73. starts.push_back(diff(start, running));
  74. joins.push_back(diff(running, joined));
  75. sorts.push_back(diff(joined, sorted));
  76. sends.push_back(diff(sent, received));
  77. }
  78. std::cout << "average time to spawn a thread: " << avg(starts) << " us\n"
  79. << "average time to join a thread: " << avg(joins) << " us\n"
  80. << "average time to sort 1000 ints: " << avg(sorts) << " us\n"
  81. << "average time to message a thread: " << avg(sends) << " us\n";
  82. pthread_mutex_destroy(&mx);
  83. pthread_cond_destroy(&cv);
  84. }
  85.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
/home/xpn87C/ccZGRUtG.o: In function `f1':
prog.cpp:(.text+0x13): undefined reference to `clock_gettime'
/home/xpn87C/ccZGRUtG.o: In function `f2':
prog.cpp:(.text+0x7d): undefined reference to `clock_gettime'
/home/xpn87C/ccZGRUtG.o: In function `main':
prog.cpp:(.text.startup+0xe0): undefined reference to `clock_gettime'
prog.cpp:(.text.startup+0x104): undefined reference to `pthread_create'
prog.cpp:(.text.startup+0x118): undefined reference to `pthread_join'
prog.cpp:(.text.startup+0x12c): undefined reference to `clock_gettime'
prog.cpp:(.text.startup+0x1d3): undefined reference to `clock_gettime'
prog.cpp:(.text.startup+0x1f7): undefined reference to `pthread_create'
prog.cpp:(.text.startup+0x248): undefined reference to `clock_gettime'
prog.cpp:(.text.startup+0x274): undefined reference to `pthread_join'
collect2: error: ld returned 1 exit status
stdout
Standard output is empty