#include"../log/blockqueue.hpp"#include"pthread.h"#include<cstdlib>#include<ctime>#include<unistd.h>
std::unique_ptr<BlockQueue<std::string>>newbkque(new BlockQueue<std::string>);
std::unique_ptr<BlockQueue<std::string>> bkqueue_ = std::move(newbkque);void*writeQueue(void*arg){srand(time(0));for(int i =0; i <2; i++){sleep(rand()%3);
bkqueue_->push(std::to_string(i));}}void*readQueue(void*arg){srand(time(0));for(int i =0; i <2; i++){sleep(rand()%2);
std::string s;if(bkqueue_->pop(s,3)){printf("bkqueue->pop: %s\n", s.c_str());}else{printf("bkqueue->pop failed\n");}}}intmain(){for(int i =0; i <3; i++){
pthread_t tid;pthread_create(&tid,NULL, writeQueue,NULL);pthread_detach(tid);}for(int i =0; i <5; i++){
pthread_t tid;pthread_create(&tid,NULL, readQueue,NULL);pthread_detach(tid);}pthread_exit(NULL);}
2. 功能测试:
多线程入队,多线程出队测试
#include"../log/blockqueue.hpp"#include"pthread.h"#include<cstdlib>#include<ctime>#include<unistd.h>
std::unique_ptr<BlockQueue<std::string>>newbkque(new BlockQueue<std::string>);
std::unique_ptr<BlockQueue<std::string>> bkqueue_ = std::move(newbkque);void*writeQueue(void*arg){srand(time(0));for(int i =0; i <2; i++){sleep(rand()%3);
bkqueue_->push(std::to_string(i));}}void*readQueue(void*arg){srand(time(0));for(int i =0; i <2; i++){sleep(rand()%2);
std::string s;if(bkqueue_->pop(s,3)){printf("bkqueue->pop: %s\n", s.c_str());}else{printf("bkqueue->pop failed\n");}}}intmain(){for(int i =0; i <3; i++){
pthread_t tid;pthread_create(&tid,NULL, writeQueue,NULL);pthread_detach(tid);}for(int i =0; i <5; i++){
pthread_t tid;pthread_create(&tid,NULL, readQueue,NULL);pthread_detach(tid);}pthread_exit(NULL);}