wangzhengquan
2020-07-07 082633f08aae8eea19bd7050cbe4a75e5ed1ac6f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "test.h"
 
 
using namespace std;
 
 
 
void sigint_handler(int sig) {
  cerr << "sigint_handler" << endl;
  destroy();
  exit(0);
 
}
 
void* run (void *arg) {
 
  struct Targ * targ = (struct Targ * )arg;
 // cerr << "productor key="<<targ->key << endl;
  err_msg(0, "productor key = %d\n", targ->key );
  SLinkedLockFreeQueue<struct Item> *queue = QFactory::createLinkedLockFreeQueue<struct Item> (targ->key, 10);
  //SArrayLockFreeQueue<struct Item> *queue = QFactory::createArrayLockFreeQueue<struct Item> (targ->key, 10);
 
  /* Transfer blocks of data from stdin to shared memory */
  int end = targ->end;
  struct Item item;
  struct timespec timeout = {10, 0};
 
  int i = targ->start;
 
  item.pic = i;
  item.info = i;
  while((end == -1 || (i < end) ) && (queue->add_timeout(item, &timeout)) ) {
    item.pic = i;
    item.info = i;
 
    // cout << "入队:" << item.pic << ", " << item.info << endl;
    //cout <<  item.pic << endl;
 
    i++;
    
  }
  return (void *)i;
}
 
 
 
int main(int argc, char *argv[])
  signal(SIGINT,  sigint_handler);
  /* Create set containing two semaphores; initialize so that
     writer has first access to shared memory. */
    
  
  int i;
  pthread_t tids[NTHREADS];
  void *res[NTHREADS];
  struct Targ targs[NTHREADS];
   
 
  for (i = 0; i< NTHREADS; i++) {
    
    targs[i].key = i;
    targs[i].start = 0;
    targs[i].end = 100000;
     
    pthread_create(&tids[i], NULL, run, (void *)&targs[i]);
    sleep(1);
  }
 
  for (i = 0; i< NTHREADS; i++) {
    if(pthread_join(tids[i], &res[i])!=0) {
      perror("productor pthread_join");
    } else {
      fprintf(stderr, "productor %d 写入 %ld 条数据\n", i, (long)res[i]);
    }
  }
 
  
  //destroy();
  cerr << "productor quit" << endl;
  exit(EXIT_SUCCESS);
}