Advertisement
Guest User

Untitled

a guest
Jul 2nd, 2013
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.79 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <unistd.h>
  3. #include <sys/wait.h>
  4. #include <pthread.h>
  5.  
  6. #define TEST_THREADS
  7. static const int MAX_ITER = 50*1000;
  8.  
  9. void TestFork()
  10. {
  11.     for(int i = 0; i < MAX_ITER; ++i) {
  12.         pid_t p = fork();
  13.         if (p) {
  14.             int status;
  15.             waitpid(p, &status, 0);
  16.         }
  17.         else
  18.             exit(1);
  19.     }
  20. }
  21.  
  22. void *ThreadFunc( void *parg )
  23. {
  24.     return NULL;
  25. }
  26. void TestThreads()
  27. {
  28.     pthread_attr_t attr;
  29.     pthread_attr_init( &attr );
  30.     for(int i = 0; i < MAX_ITER; ++i) {
  31.         pthread_t t;
  32.         pthread_create( &t, &attr, ThreadFunc, NULL );
  33.         void *status;
  34.         int retCode = pthread_join( t, &status );
  35.     }
  36. }
  37.  
  38.  
  39. int main() {
  40. #ifndef TEST_THREADS
  41.     TestFork();
  42. #else
  43.     TestThreads();
  44. #endif
  45.     return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement