Skip to content

Instantly share code, notes, and snippets.

@luchenqun
Created January 29, 2018 09:19
Show Gist options
  • Select an option

  • Save luchenqun/5c2fdb39b771befd6e944f12b4c905b5 to your computer and use it in GitHub Desktop.

Select an option

Save luchenqun/5c2fdb39b771befd6e944f12b4c905b5 to your computer and use it in GitHub Desktop.
#include <string>
#include <thread>
#include "RedisClient.h"
void f(int index){
std::string strKey = "name";
std::string strVal;
CRedisClient* r = CRedisClient::Instance();
int ret = r->Get(strKey, &strVal);
std::cout << index << ":" << std::this_thread::get_id() << " request ret = " << ret << ", val = " << strVal << std::endl;
}
int main(int argc, char **argv) {
CRedisClient* redisCli = CRedisClient::Instance();
if (!redisCli->Initialize("127.0.0.1", 6379, 2, 30))
{
std::cout << "connect to redis failed" << std::endl;
return -1;
}
std::vector<std::thread> tv; //保存线程的情况
for(int j = 1; j <= 20 ; ++j) {
std::thread t(f, j);
tv.push_back(std::move(t));
}
for(auto &thread : tv) {
thread.join();
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment