p_bench_yield.c
上传用户:tsgydb
上传日期:2007-04-14
资源大小:10674k
文件大小:2k
源码类别:

MySQL数据库

开发平台:

Visual C++

  1. /* ==== p_bench_mutex.c =================================================
  2.  * Copyright (c) 1993-1995 by Chris Provenzano, proven@athena.mit.edu
  3.  *
  4.  * Description : Benchmark mutex lock and unlock times
  5.  *
  6.  *  1.00 93/11/08 proven
  7.  *      -Started coding this file.
  8.  */
  9. #include <errno.h>
  10. #include <pthread.h>
  11. #include <stdio.h>
  12. #define OK 0
  13. #define NOTOK  -1
  14. /* ==========================================================================
  15.  * usage();
  16.  */
  17. void usage(void)
  18. {
  19. printf("p_bench_yield [-d?] \n");
  20. printf("t[-c count] \n");
  21. printf("t[-C thread count] \n");
  22. printf("t[-O optimization level]n");
  23.     errno = 0;
  24. }
  25. void *yield(void * arg)
  26. {
  27. int i, * count;
  28. count = (int *)arg;
  29. for (i = 0; i < *count; i++) {
  30. pthread_yield();
  31. }
  32. return(NULL);
  33. }
  34. main(int argc, char **argv)
  35. {
  36. struct timeval starttime, endtime;
  37. pthread_mutex_t lock;
  38. pthread_attr_t attr;
  39. pthread_t thread_id;
  40. int thread_count = 1;
  41. int optimization = 0;
  42. int count = 1000000;
  43. int i, debug = 0;
  44. char word[256];
  45.     /* Getopt variables. */
  46.     extern int optind, opterr;
  47.     extern char *optarg;
  48. pthread_init();
  49. while ((word[0] = getopt(argc, argv, "C:O:c:d?")) != (char)EOF) {
  50. switch (word[0]) {
  51. case 'C':
  52. thread_count = atoi(optarg);
  53. break;
  54. case 'O':
  55. optimization = atoi(optarg);
  56. break;
  57. case 'c':
  58. count = atoi(optarg);
  59. break;
  60. case 'd':
  61. debug++;
  62. break;
  63. case '?':
  64. usage();
  65. return(OK);
  66. default:
  67. usage();
  68. return(NOTOK);
  69. }
  70. }
  71. pthread_attr_init(&attr);
  72.     if (optimization > 0) {
  73. pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
  74. }
  75.     if (optimization > 1) {
  76. pthread_attr_setfloatstate(&attr, PTHREAD_NOFLOAT);
  77. }
  78. pthread_mutex_init(&lock, NULL);
  79. if (gettimeofday(&starttime, NULL)) {
  80. perror ("gettimeofday");
  81. return 1;
  82. }
  83. for (i = 1; i < thread_count; i++) {
  84. if (pthread_create(&thread_id, &attr, yield, &count)) {
  85. perror ("pthread_create");
  86. return 1;
  87. }
  88. if (pthread_detach(thread_id)) {
  89. perror ("pthread_detach");
  90. return 1;
  91. }
  92. }
  93. if (pthread_create(&thread_id, &attr, yield, &count)) {
  94. perror ("pthread_create");
  95. return 1;
  96. }
  97. if (pthread_join(thread_id, NULL)) {
  98. perror ("pthread_join");
  99. return 1;
  100. }
  101. if (gettimeofday(&endtime, NULL)) {
  102. perror ("gettimeofday");
  103. return 1;
  104. }
  105. printf("%d pthread_yields took %d usecs.n", count, 
  106. (endtime.tv_sec - starttime.tv_sec) * 1000000 +
  107. (endtime.tv_usec - starttime.tv_usec));
  108. return 0;
  109. }