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.

76 lines
1.6KB

  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. #define NTHREADS 2
  16. typedef struct {
  17. int i;
  18. int v;
  19. UT_hash_handle hh;
  20. } elt;
  21. elt *elts=NULL; /* this is our hash table which two threads will use */
  22. pthread_rwlock_t lock;
  23. void *thread_routine( void *arg ) {
  24. int keepgoing=0;
  25. /* TODO write me */
  26. return NULL;
  27. }
  28. int main() {
  29. unsigned i;
  30. long num_added=0;
  31. int status;
  32. pthread_t thread[NTHREADS];
  33. void *thread_result;
  34. elt tmp, *e;
  35. if (pthread_rwlock_init(&lock,NULL) != 0) {
  36. fprintf(stderr,"lock init failed\n");
  37. exit(-1);
  38. }
  39. /* populate it to start */
  40. for(i=0; i<LOOPS; i++) {
  41. e = malloc(sizeof(elt));
  42. if (!e) exit(-1);
  43. e->i = i;
  44. e->v = 0;
  45. HASH_ADD_INT(elts, i, e);
  46. }
  47. for(i=0; i<NTHREADS; i++) {
  48. if (( status = pthread_create( &thread[i], NULL, thread_routine, NULL) )) {
  49. printf("failure: status %d\n", status);
  50. exit(-1);
  51. }
  52. }
  53. for(i=0; i<NTHREADS; i++) {
  54. status = pthread_join( thread[i], &thread_result );
  55. printf("thread result: %d %ld\n", status, (long)thread_result);
  56. }
  57. i = HASH_COUNT(elts);
  58. printf("final count of items in hash: %u\n", i);
  59. if (pthread_rwlock_destroy(&lock) != 0) {
  60. fprintf(stderr,"lock destroy failed\n");
  61. exit(-1);
  62. }
  63. }