santazhang / simple-rpc

Simple RPC in C++, with Python binding
http://www.yzhang.net/simple-rpc/
BSD 3-Clause "New" or "Revised" License
27 stars 6 forks source link

why add sconns_l_.lock()? #14

Closed jiayunwei closed 6 years ago

jiayunwei commented 6 years ago

sconnsl.lock(); ServerConnection* sconn = new ServerTcpConnection(this, clntsocket); sconns.insert(sconn); pollmgr_->add(sconn); sconnsl.unlock();

santazhang commented 6 years ago

I believe it's intended to guard sconns_.insert(sconn). sconns_ could be accessed by multiple threads.

jiayunwei commented 6 years ago

But I found that your progrem is a single thread(server.c: 639:Pthread_create(&loopth, nullptr, Server::start_server_loop, start_server_loop_args);).Besides why do not use main thread?

santazhang commented 6 years ago

This library is multi-thread. You were looking at the server side code, which basically works like this:

A event loop thread (or maybe a few threads? I don't quite remember since it's been quite a few years) listens to all the network IO events. Connection, on the server side, is encapsulated in the ServerConnection class.

A thread pool runs behind the IO thread. When a request comes in, it got parsed in the IO threads, and then sent to the thread pool to do the actual work -- calling RPC function code. After a result is generated, it get serialized, and sent to IO threads so it can be delivered via network back to client side code.

The server needs to keep a registry of all the ServerConnection's, so when the server is gracefully shutting down, it can properly close those connections. That's why I added std::unordered_set<ServerConnection*> sconns_ to keep tab of them. And sconnsl is only added to protect it from data race, because sconns_ could be accessed in IO threads, thread pool (if rpc code want to gracefully shutdown the server), or main thread (for example, a signal handler for Ctrl+C wants to shut down the server).

And as for why not use main thread -- Honestly, I don't quite remember :) I guess it's just my personal preference. I like keeping main thread available, so for example I can use it to do other work.

jiayunwei commented 6 years ago

Thanks! I find that you are intersted in distribute system, just in time, I want to learn it. Do you have some advice to me?

santazhang commented 6 years ago

Well, you know, read good papers from literature, read good tech blog posts to see what is the practice in industry. I recommend http://highscalability.com/ which has many interesting reads.