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