1

I am currently developing my website with c++. I know that nginx handles connections with multiple threads to provide high performance. And not all c++ code is thread safe.

  • What's the relationship between nginx's multithreading and c++ application's multithreading?

  • Do I have to worry about this?

  • What should I do to avoid possible issues?

5
  • 1
    Sharing your research helps everyone. Tell us what you've tried and why it didn’t meet your needs. This demonstrates that you’ve taken the time to try to help yourself, it saves us from reiterating obvious answers, and most of all it helps you get a more specific and relevant answer. Also see How to Ask
    – gnat
    Oct 7, 2013 at 10:37
  • 2
    Is your application run by nginx as CGI, FCGI, backend HTTP server (nginx is reverse proxy) or module? The answer depends on that (and mostly not on whether you use nginx or other web server in particular).
    – Jan Hudec
    Oct 7, 2013 at 11:08
  • @JanHudec my app run as normal cgi, nginx as the webserver. That is, I compile my c++ code to binary and name it to .cgi, put it to somewhere like cgi-bin/myapp.cgi. Nginx is used as normal webserver just like apache. Oct 7, 2013 at 11:26
  • @gnat Thanks for your advice, I have not fully understood the whole picture of web programming, I previously just write some php code and run it, without much knowledge of socket, proxy, multithreading, process etc. Now, I switched to c++, and I start to be aware of the deeper layer of web programming. Maybe this question is too broad, I just don't have a clue. I think I need to gain the basic knowledge of thread, process, nginx, cgi etc first. Oct 7, 2013 at 11:37
  • @user2758004: Ok. So it's CGI. Therefore the server starts a new instance for each and every request. Nothing more to concern about.
    – Jan Hudec
    Oct 7, 2013 at 12:29

1 Answer 1

2

I assume you are using nginx as a reverse proxy. In such case any parallel connections that come to you app are queued by operating system. You should not even count nginx in your equation because you can never predict when a connection comes. What you control though is the moment of accepting/handling it.

Truth is that usually listening for connections is handled in a single thread, in some kind of a loop. It is the handling part may or may not be done in a multi-threaded.

The simple and safe design below.

enter image description here

With such approach there is no need to worry about multiple threads. If a different connection comes in the middle of handling the first one then it will simply be queued and have to wait.

You can improve this if you write your app in a way that the relevant state is stored in a separate entity/database and then you may simply start several single-threaded C++ processes.

If you fork your C++ process or use a FastCGI backend you may share a single listening socket that will dispatch the incoming connections to several single-threaded C++ app instances.

I would strongly advice a multi-process approach due to the "leaky" and "crashy" nature of C++. If you have multiple processes any one of them may be restarted and / or crash without compromising the whole system.

Incidentally you get thread-safety by design if handling only one connection at a time by a process. This would mimic Node.JS web servers which have a reputation for being snappy.

6
  • 3
    c++ is no more leaky or crashy than other languages. Node.js only has a reputation for handling IO very well, not for being fast.
    – gbjbaanb
    Oct 7, 2013 at 11:49
  • I just know that different threads in a process share the same resource, thus when you use c/c++ to manipulate some of them, conflicts may occur. And I heard that the reason that nginx is faster than apache is because nginx uses multithreads while apache handles every connection with a process. Oct 7, 2013 at 11:53
  • @user2758004 Apache handles connections by forking off child processes and also through multithreading. It's a pain in the ass.
    – James
    Oct 7, 2013 at 12:18
  • 1
    @user2758004: There is basically no difference between threads and processes. Processes do a copy-on-write on few pages and that's it. Nginx is faster mainly because it is simply much simpler.
    – Jan Hudec
    Oct 7, 2013 at 12:33
  • @gbjbaanb: C++ does not have have a reputation for begin safe and with a good reason. Of course by using shared pointers/RAII, and other useful constructs you vastly improve your chances. I love C++11 but I won't deny that with great power comes great responsibility.
    – RushPL
    Oct 7, 2013 at 12:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.