I was wondering how to implement Redis NOSQL calls into the the C++ Std Threading library code from the C++ Concurrency in Action book.
I am using the Github repo https://github.com/subjam/concurrency-in-action
source file of
https://github.com/subjam/concurrency-in-action/blob/master/ch7/stack.cpp
I was wondering if I placed my Redis NOSQL connection code in the push() function is wise.
I am also using Redox which is a C++ wrapper which includes the Hiredis C library.
https://github.com/hmartiro/redox
I am using the subscriber NOSQL pattern which would be included in the Threading push() function. This would make the connection and query executed each time there is a push but no semaphore/mutex would be needed (I assume). At line 145 of push()
in stack.cpp
, can I rewrite the push
function as?
void push(stack<int>* s)
{
Redox rdx; Subscriber sub;
if(!rdx.connect() || !sub.connect()) return;
sub.subscribe("hello", [](const string& topic, const string& msg) {
…parse msg fields into data structure …
s->push(data);
});
}
Or should I make the the following connection code once before executing the push() function?
Redox rdx; Subscriber sub;
if(!rdx.connect() || !sub.connect()) return ;
Would there be a threading conflict if I keep the Redis connection code outside of the push()
Or is there a more optimal way of coding this within stack.cpp?
Thanks