multithreading - C++ Session management methods -


i want create session management scheme , in few words , has way:

  • a std::map keep track of current active sessions (filled id string , and associated message queue)
  • a set of threads on each message queue.

there 2 ways create scheme:

  1. keep components in main program , when needed fill map new session id , associated message queue , start new detached thread reference queue polling on argument.

     //main program code    _session_map[id] = queue;    thread = new thread(&queue);    thread.detach();    thread.start();  //main program code 
  2. create session_manager class hides mechanisms main program.

     class session_manager  {     //session_manager code     std::string new_session(std::string id)     {        _session_map[id] = queue;        thread = new thread(&queue);        thread.detach();        thread.start();     }      private:       std::map<std::string,message_queue> _session_map;  }; 

what better way create scheme? i'm not sure if second scheme work correctly because i'm not expert on using threads. don't have idea on how keep track of closed sessions hve suggestion?

a few years have given entirely different answer now.

i not sure mean "session management" or how queues relate workers. start bit of assuming:

  • you want n threads work in parallel, competing each other on jobs found in queue.
  • your session kind of working session, last until sequence of jobs done.

in older days of c++ thread-programming, have outlined architectures on how 1 can implement such scheme.

but suspect, need job getting done , not "theory class".

so, instead of fiddling low level threads (which os specific), chose showcase (also os specific) more abstract way things done.

advantages:

  • concurrent programming without having deal locks, mutexes, static thread functions calling pthis->execute() etc.
  • conceptually easy understand. message blocks, sources, targets, messages, worker objects (actors/agents). no promise-future c++-linq reactive functional programming replacement attempts (attempt on humor!).
  • appears come pretty close have in mind.

disadvantages:

  • hides low level stuff oldies proud of knowing , spent years of pure joy , despair with.
  • runs on windows platforms (afaik), unfortunately.

all code here uses windows concurrency runtime.

#include "stdafx.h" #include <thread> #include <concrt.h> #include <agents.h> #include <iostream>  template<class _job> class worker      : public concurrency::agent  {     concurrency::isource<_job> *m_source;     volatile bool m_running;     uint32_t m_counter; public:     worker(concurrency::isource<_job> *source)         : m_source(source)         , m_running(true)         , m_counter(0ul)     {}     ~worker()     {}     uint32_t counter() const     {         return m_counter;     }     void stop()     {         m_running = false;     }     virtual void run()     {         while (m_running)         {             try             {                 _job job = concurrency::receive(m_source, 1000);                 m_counter++;             }             catch (concurrency::operation_timed_out& /*timeout*/)             {                 std::cout << "timeout." << std::endl;             }         }         _job job;         while (concurrency::try_receive(m_source, job))         {             m_counter++;         }         done();     } };  typedef uint64_t job_t;  int _tmain(int argc, _tchar* argv[]) {     const size_t num_workers = 4;     concurrency::unbounded_buffer<job_t> buffer;     worker<job_t> workers[num_workers] =          { worker<job_t>(&buffer)         , worker<job_t>(&buffer)         , worker<job_t>(&buffer)         , worker<job_t>(&buffer)         };     std::vector<concurrency::agent*> agents;      (auto& worker : workers)     {         agents.push_back(&worker);         worker.start();     }      (uint64_t jobid = 0ull; jobid < 1000000ull; jobid++)     {          concurrency::asend(buffer, jobid);     }      (auto& worker : workers)     {         worker.stop();     }      concurrency::agent::wait_for_all(num_workers,&agents[0]);      (auto& worker : workers)     {         std::cout << "counter: " << worker.counter() << std::endl;     }      return 0; } 

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -