9

Google C++ coding style recommends against C++ exceptions, and we don't use them too. For most of the STL library containers one can just ignore the exceptions, because normally they indicate critical errors and are difficult to handle anyway, so crashing is acceptable.

However there is a problem with multi-threading (std::thread), for example entering a non-recursive mutex twice throws an exception. This situation is not critical and could be handled by waiting.

My question is: anyone knows what is Google using as a threading library? Is there any C++ cross-platform threading library that doesn't use exceptions?

Thank you

10
  • 14
    Out of curiosity, you have a reason, applicable to your situation, for not using exceptions right? You're not just blindly following the Google style guide, are you? Because that guide is meant for programmers writing sofware for Google (and I know of many in the company who despise the policy), not as general advice. Commented Sep 29, 2013 at 1:18
  • 11
    Google's C++ style is archaic -- why are you trying to follow it? Like anything else exceptions can be misused but they are not bad and simply banning them is kind of pathetic. Commented Sep 29, 2013 at 1:19
  • 3
    Also, entering a std::mutex you already own is undefined by the standard -- at best, you can hope for an exception. At worst, you'd probably deadlock. The exception here does fit your category of a critical error. Commented Sep 29, 2013 at 1:20
  • 1
    @CoryNelson: About entering the same mutex more than once from the same thread, it's completely well-defined and supported for recursive mutexes (muteces?) in the standard (as the OP implies.) Commented Sep 29, 2013 at 1:24
  • 2
    OP doesn't imply; he explicitly says he is using a non-recursive mutex. Commented Sep 29, 2013 at 1:26

4 Answers 4

13

It should be noted that Google's style guide does not not preclude the handing of exceptions rather the throwing of exceptions. I.e. deal with the problem, but don't make it any worse by throwing more exceptions.

In the case of re-entering a non-recursive mutex: that is clearly a programmer error rather than some unexpected bolt from the blue. The exception should be allowed to propagate up to the calling code so that it can be seen and dealt with as a bug. It should be noted that the Google Test framework does not rely on exceptions, but it can certainly catch and report them.

While Google's style guide takes an extreme position, there is no doubt that exceptions can highly problematic when writing reusable libraries. For example, we found developing with WinCE 6.0 that exceptions got sliced on re-throwing and on ARM platforms couldn't be reliably thrown across DLL boundaries. In addition, catching an exception could take several milliseconds, so they definitely shouldn't be used for non-exceptional circumstances (i.e. 'expected' errors) where real-time performance is required. The clue's in the name really.

Sign up to request clarification or add additional context in comments.

Comments

11

The practices in Google's style guide dates back to the early nineties of the previous century, when threads were rather exotic beasts. As such, it doesn't make sense to wonder how that style and threads would mix. If you use "modern" (21st century) techniques like threads, you don't use Google's style guide and vice versa.

2 Comments

What style guide should one use then?
Well, there's always a bit of subjectivity to this, but C++ Coding Standards is much better
9

IIRC the reason that Google does not use exceptions is that much of their codebase is not exception safe, and they cannot afford to rewrite it.

From their site:

On their face, the benefits of using exceptions outweigh the costs, especially in new projects. However, for existing code, the introduction of exceptions has implications on all dependent code. If exceptions can be propagated beyond a new project, it also becomes problematic to integrate the new project into existing exception-free code. Because most existing C++ code at Google is not prepared to deal with exceptions, it is comparatively difficult to adopt new code that generates exceptions.

Given that Google's existing code is not exception-tolerant, the costs of using exceptions are somewhat greater than the costs in a new project. The conversion process would be slow and error-prone. We don't believe that the available alternatives to exceptions, such as error codes and assertions, introduce a significant burden.

Our advice against using exceptions is not predicated on philosophical or moral grounds, but practical ones. Because we'd like to use our open-source projects at Google and it's difficult to do so if those projects use exceptions, we need to advise against exceptions in Google open-source projects as well. Things would probably be different if we had to do it all over again from scratch.

Comments

1

Personally speaking I much prefer function return codes than exceptions. But regardless of one's own preferences or coding styles, the important thing is to catch problems where they occur, don't let them propagate, persist or cause mayhem.

What I do, especially during development, is on detecting any sort of error in any sort of thread I get the process to freeze itself. On Linux I raise the SIGSTOP signal. The benefit of that is that I can then attach with a debugger and see the whole process in all its broken glory.

2 Comments

Actually, that practice of SIGSTOP'ing oneself during development seems useful. One doesn't always have a debugger at hand. +1 for that
@JonasWielicki, yes, it's a handy trick. Another one is to get your program doing its own core dump on error and killing itself. Run that from a script that keeps restarting it. You end up with a collection of core files, each one frozen at the point of error which you can analyse later post mortem. It's far better than the program just dying with an unhandled mysterious exception, and saves having to run it under a debugger (which might not be an option for a long running program). Handy for those hard to find rarely occurring bugs. Plus you can run LOTS of them at once, find bugs quicker

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.