/testapp.cpp

http://github.com/donhuanmatus/redis4cpp · C++ · 111 lines · 87 code · 21 blank · 3 comment · 3 complexity · 0abbfa9b36c67cb1e4ea426498724a9a MD5 · raw file

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