2

I wanted to know if there is a way to use std::thread without exceptions, if not what alternatives can I use ?

Actually I target windows and linux desktops but this may be extended in future.

edit: just using compiler options to disable exceptions, is not an acceptable solution. Errors have to be handed in one way or another.

The problem is that only exceptions that are used in my code are those handling std::thread errors. I'd like to get rid of those to have an exception-free code

edit2: I found tinythread++ library, it seems easy to modify if needed and works without exceptions.

ildjarn
  • 62,044
  • 9
  • 127
  • 211
Thelvyn
  • 264
  • 3
  • 8
  • you can use those exceptions as long as you are debugging your project. after that, you can assume there will be no exceptions thrown simply because exceptions will indicate unexpected environment changes. – Ali1S232 Jan 03 '13 at 12:43
  • The real problem is that you assume creating a thread always succeeds. Usually that's also the case, but generally it's a wrong assumption. It _might_ fail, exceptionally. That's the reason why `std::thread` **must** have exceptions in its constructor, it's the only valid way to communicate failure (`detach` and `join` throwing, OTOH, is bull, these should `assert`, as it is not "exceptional failure" to join a non-joinable thread, it's a programming error). Though, as Luke B suggests, just turn off exceptions if you don't like them. – Damon Jan 03 '13 at 13:28
  • "just using compiler options to disable exceptions, is not an acceptable solution" So then use exceptions. You're basically asking to disable a part of the C++ language but still use the C++ language. – GManNickG Jan 04 '13 at 19:04

1 Answers1

1

You can disable STL's exceptions, but it's different for each compiler. Of course, since the standard actually says some exceptions must be thrown, whenever you do something that would throw an exception, you will get undefined behavior.

In gcc you disable exceptions by using -fno-exceptions

in MSVC: Can I disable exceptions in STL?

Community
  • 1
  • 1
Luke B.
  • 1,258
  • 1
  • 17
  • 28
  • 2
    Undefined behavior is not acceptable, I need to handle errors in one way or another. – Thelvyn Jan 03 '13 at 12:40
  • For example detect a thread creation failure, then cleanup nicely and report the error. – Thelvyn Jan 03 '13 at 16:06
  • @Thelvyn why not create your own thread class to encapsulate std::thread and deal with every possible exception there instead of the rest of the code? – Luke B. Jan 04 '13 at 18:21
  • That's not the problem, it would mean using exceptions. I do not want to avoid the propagation of exceptions but avoid their usage completely. I found tinythread++ library, it seems easy to modify if needed. – Thelvyn Jan 04 '13 at 18:59
  • 3
    @Thelvyn: You're going to have a bad time programming in C++ then. Exceptions are part of the language, get used to it. – GManNickG Jan 04 '13 at 19:05