File was renamed from test/productor.c |
| | |
| | | |
| | | 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); |
| | | |
| | | } |
| | |
| | | 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; |
| | |
| | | |
| | | 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++; |
| | | |
| | |
| | | int i; |
| | | pthread_t tids[NTHREADS]; |
| | | void *res[NTHREADS]; |
| | | struct Targ targs[NTHREADS]; |
| | | |
| | | |
| | | |
| | | for (i = 0; i< NTHREADS; i++) { |
| | |
| | | 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]); |
| | | } |
| | | } |
| | | |