/testapp.cpp
C++ | 111 lines | 87 code | 21 blank | 3 comment | 1 complexity | 0abbfa9b36c67cb1e4ea426498724a9a MD5 | raw file
1 2#include <iostream> 3#include <sstream> 4#include <string> 5#include <boost/asio.hpp> 6#include "boost/bind.hpp" 7#include <cstdlib> 8#include "boost/bind.hpp" 9#include <boost/thread/thread.hpp> 10#include <boost/thread/mutex.hpp> 11#include <boost/thread/locks.hpp> 12#include <boost/thread/shared_mutex.hpp> 13 14#include "src/interface.h" 15#include "src/dataaccess.h" 16 17using namespace std; 18 19boost::shared_mutex _print; 20 21 22std::ostringstream results; 23 24void PrintResult(redis4cpp::CommandBasePtr cmd) 25{ 26 try 27 { 28 boost::lock_guard<boost::shared_mutex> guard(_print); 29 std::cout << "//command: " << cmd->OutputBuffer() << "//result: " << cmd->Result() << std::endl; 30 std::cout << "******************************************************************" << std::endl; 31 } 32 catch(std::exception& e) 33 { 34 std::cerr << "PrintResult Exception: " << e.what() << "\n"; 35 } 36} 37 38void ExecutionGetLoop(redis4cpp::DataAccess* db) 39{ 40 try 41 { 42 int count = 0; 43 while(count < 10) 44 { 45 redis4cpp::CommandBase* get = new redis4cpp::CommandBase("EVAL", boost::bind(&PrintResult, _1)); 46 get->AddArgument("return redis.call('get', 'nmykey');"); 47 get->AddArgument(0); 48 db->AsyncCommand(get); 49 50 count++; 51 } 52 53 } 54 catch(std::exception& e) 55 { 56 std::cerr << "ExecutionLoop Exception: " << e.what() << "\n"; 57 } 58} 59 60void ExecutionSetLoop(redis4cpp::DataAccess* db) 61{ 62 try 63 { 64 int count = 0; 65 while(count < 10) 66 { 67 redis4cpp::CommandBase* set = new redis4cpp::CommandBase("SET", boost::bind(&PrintResult, _1)); 68 set->AddArgument("nmykey"); 69 set->AddArgument(count); 70 db->AsyncCommand(set); 71 72 count++; 73 } 74 75 } 76 catch(std::exception& e) 77 { 78 std::cerr << "ExecutionLoop Exception: " << e.what() << "\n"; 79 } 80} 81 82int main(int argc, char** argv) { 83 84 try 85 { 86 boost::asio::io_service io_service; 87 redis4cpp::DataAccess db(io_service); 88 89 // Create a pool of threads to run all of the io_services. 90 boost::thread_group threads; 91 92 // Create threads for read/write async operations 93 for (std::size_t i = 0; i < 2; ++i) 94 threads.create_thread(boost::bind(&boost::asio::io_service::run, &io_service)); 95 96 boost::thread_group datathreads; 97 datathreads.create_thread(boost::bind(&ExecutionSetLoop, &db)); 98 datathreads.create_thread(boost::bind(&ExecutionGetLoop, &db)); 99 datathreads.join_all(); 100 101 // wait for them 102 threads.join_all(); 103 } 104 catch (std::exception& e) 105 { 106 std::cerr << "Exception: " << e.what() << "\n"; 107 } 108 109 return 0; 110} 111