wangzhengquan
2020-07-07 37a7bd95042c19d7334b099d50ac6dc8e07e4b4e
test/multiple_queue_productor.c
File was renamed from test/productor.c
@@ -3,11 +3,13 @@
 
using namespace std;
#define NTHREADS 4
struct Targ targs[NTHREADS];
size_t qsize = 16;
void sigint_handler(int sig) {
  cerr << "sigint_handler" << endl;
  destroy();
  mm_destroy();
  exit(0);
}
@@ -17,9 +19,10 @@
  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);
  //SLinkedLockFreeQueue<struct Item> *queue = QFactory::createLinkedLockFreeQueue<struct Item> (targ->key, 10);
  //SArrayLockFreeQueue<struct Item> *queue = QFactory::createArrayLockFreeQueue<struct Item> (targ->key, 10);
  LockFreeQueue<struct Item> *queue = QueueFactory::createQueue<struct Item> (targ->key, qsize);
  /* Transfer blocks of data from stdin to shared memory */
  int end = targ->end;
  struct Item item;
@@ -29,12 +32,10 @@
  item.pic = i;
  item.info = i;
  while((end == -1 || (i < end) ) && (queue->add_timeout(item, &timeout)) ) {
  while((end == -1 || (i < end) ) && (queue->push(item)) ) {
    item.pic = i;
    item.info = i;
    // cout << "入队:" << item.pic << ", " << item.info << endl;
    //cout <<  item.pic << endl;
    printf("productor(%d) 入队:{%d, %d}\n", targ->key, item.pic,  item.info);
    i++;
    
@@ -54,7 +55,7 @@
  int i;
  pthread_t tids[NTHREADS];
  void *res[NTHREADS];
  struct Targ targs[NTHREADS];
   
 
  for (i = 0; i< NTHREADS; i++) {
@@ -64,14 +65,14 @@
    targs[i].end = 100000;
     
    pthread_create(&tids[i], NULL, run, (void *)&targs[i]);
    sleep(1);
    //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]);
      fprintf(stderr, "productor(%d) 写入 %ld 条数据\n", i, (long)res[i]);
    }
  }