multithreading - Implementing reload method in C++ Thrift Server -
i'm using apache thrift framework, i'm writing application, has c++ server , python client. application has big resource object, used during life. application needs have reload method. should delete resources, , load new instances disk. wrote cleaning method, , calling before load method. that's work, if use tsimpleserver(one-threaded). when i'm starting use threadedserver or tnonblocking server(multi-threaded), cleannig method doesn't work, can see, it's calling doesn't clean. server starting use twice amount of memory. tried use mutex during cleaning method, didn't help. me,it's seems matter in multithreading, remote methods starts in child thread, can't release resources allocated in main thread.
template<typename t> void deleteprocessors(t &processor) { (typename t::iterator iter = processor.begin(); iter != processor.end(); ++iter) { delete iter->second; } processor.clear(); } class taggerhandler : virtual public taggerif { public: taggerhandler() { this->loadresources(); } void tagtext(std::string& _return, const std::string& text, const std::string& language) { //useful function } void reload(std::string& _return, const std::string& parmeters) { this->clearresources(); this->loadresources(); } private: void loadresources() { //here config reading, option declaring etc.. //maco object freeling library, allocate >100mb string language = "en"; this->macoprocs[language] = shared_ptr<maco>(new maco()); } void clearresources(){ macoprocs.clear(); //this way before shared_ptr //deleteprocessors(macoprocs); } map<string, shared_ptr<maco> > macoprocs; //this way before shared_ptr //map<string, maco*> macoprocs; };
thrift initialization:
int main(int argc, char **argv) { shared_ptr<taggerhandler> handler(new taggerhandler()); shared_ptr<tprocessor> processor(new taggerprocessor(handler)); shared_ptr<tservertransport> servertransport(new tserversocket(port)); shared_ptr<ttransportfactory> transportfactory(new tbufferedtransportfactory()); shared_ptr<tprotocolfactory> protocolfactory(new tbinaryprotocolfactory()); shared_ptr<threadmanager> threadmanager = threadmanager::newsimplethreadmanager(15); shared_ptr<posixthreadfactory> threadfactory = shared_ptr<posixthreadfactory>(new posixthreadfactory()); threadmanager->threadfactory(threadfactory); threadmanager->start(); //tsimpleserver server(processor, servertransport, transportfactory, protocolfactory); tnonblockingserver server(processor, protocolfactory, port, threadmanager); server.serve(); return 0;
}`
Comments
Post a Comment