Research portable Memory game | Исследовать портируемую игру Память
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test1.c 3.0KB

8 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <errno.h>
  4. #include <string.h>
  5. #include <sys/types.h>
  6. #include <sys/wait.h>
  7. #include <unistd.h>
  8. #include <sys/stat.h>
  9. #include <fcntl.h>
  10. #include <pthread.h>
  11. #include "uthash.h"
  12. #undef uthash_noexpand_fyi
  13. #define uthash_noexpand_fyi(tbl) fprintf(stderr,"warning: bucket expansion inhibited\n")
  14. #define LOOPS 100000
  15. typedef struct {
  16. int i;
  17. UT_hash_handle hh;
  18. } elt;
  19. elt *elts=NULL; /* this is our hash table which two threads will use */
  20. pthread_rwlock_t lock;
  21. void *thread_routine_r( void *arg ) {
  22. int i;
  23. long num_found=0;
  24. elt *e;
  25. for(i=0;i<LOOPS;i++) {
  26. if (pthread_rwlock_rdlock(&lock) != 0) {
  27. fprintf(stderr,"can't acquire read lock\n");
  28. exit(-1);
  29. }
  30. HASH_FIND_INT(elts, &i, e);
  31. if (e) num_found++;
  32. pthread_rwlock_unlock(&lock);
  33. }
  34. return (void*)num_found;
  35. }
  36. void *thread_routine_w( void *arg ) {
  37. int i;
  38. long num_deld=0;
  39. elt *e;
  40. for(i=0;i<LOOPS;i++) {
  41. if (pthread_rwlock_wrlock(&lock) != 0) {
  42. fprintf(stderr,"can't acquire write lock\n");
  43. exit(-1);
  44. }
  45. HASH_FIND_INT(elts, &i, e);
  46. if (e) {
  47. /* HASH_DEL(elts, e); */
  48. /* num_deld++; */
  49. } else {
  50. e = malloc(sizeof(elt));
  51. if (!e) exit(-1);
  52. e->i = i;
  53. HASH_ADD_INT(elts, i, e);
  54. }
  55. pthread_rwlock_unlock(&lock);
  56. }
  57. return (void*)num_deld;
  58. }
  59. int main() {
  60. unsigned i;
  61. long num_added=0;
  62. int status;
  63. pthread_t thread_r1,thread_r2,thread_w1,thread_w2;
  64. void *thread_result;
  65. elt tmp, *e;
  66. if (pthread_rwlock_init(&lock,NULL) != 0) {
  67. fprintf(stderr,"lock init failed\n");
  68. exit(-1);
  69. }
  70. if (( status = pthread_create( &thread_r1, NULL, thread_routine_r, NULL) )) {
  71. printf("failure: status %d\n", status);
  72. exit(-1);
  73. }
  74. if (( status = pthread_create( &thread_r2, NULL, thread_routine_r, NULL) )) {
  75. printf("failure: status %d\n", status);
  76. exit(-1);
  77. }
  78. if (( status = pthread_create( &thread_w1, NULL, thread_routine_w, NULL) )) {
  79. printf("failure: status %d\n", status);
  80. exit(-1);
  81. }
  82. if (( status = pthread_create( &thread_w2, NULL, thread_routine_w, NULL) )) {
  83. printf("failure: status %d\n", status);
  84. exit(-1);
  85. }
  86. status = pthread_join( thread_r1, &thread_result );
  87. printf("thread result: %d %ld\n", status, (long)thread_result);
  88. status = pthread_join( thread_r2, &thread_result );
  89. printf("thread result: %d %ld\n", status, (long)thread_result);
  90. status = pthread_join( thread_w1, &thread_result );
  91. printf("thread result: %d %ld\n", status, (long)thread_result);
  92. status = pthread_join( thread_w2, &thread_result );
  93. printf("thread result: %d %ld\n", status, (long)thread_result);
  94. i = HASH_COUNT(elts);
  95. printf("final count of items in hash: %u\n", i);
  96. if (pthread_rwlock_destroy(&lock) != 0) {
  97. fprintf(stderr,"lock destroy failed\n");
  98. exit(-1);
  99. }
  100. }