1. Can I run streams inside try {}?

  2. And what to do when an exception is sent? Wait for other threads to complete?

  3. Does std :: thread have a mechanism for stopping all threads, well, or specific tools for solving a similar problem? Or do you have to do everything manually?

    try{ // стартуют потоки }catch(std::exception исключение) { std::cout << "..."; } поток1 { // при вызове ведет к неопределенному поведению. // может выполнится может нет throw std::exception(строка); } поток2 { throw std::exception(строка); } 


And how can I report to the main stream? that an exception occurred in thread 1? Global variables?

  try { // стартуют потоки поток1; поток2; // здесь вечный цикл while(true) { ... основной поток }; }catch(std::exception исключение) { std::cout << "..."; } поток1 { try{ .. throw std::exception(строка); .. } catch(std::exception исключение) { } } поток2 { try{ .. throw std::exception(строка); .. } catch(std::exception исключение) { } } 

    3 answers 3

    You should not look at exceptions as a problem that needs to be “fought”. Exceptions are your friend. This is an error signal that you must handle.

    You can wrap the function that implements the flow logic in try / catch, if that fits your goals. You can catch bugs in other places.

    Note that try/catch around thread launch does not catch exceptions that occurred in the stream . You need to have try/catch in the main stream function itself. (Thanks to @avp for noticing this.)

    As to what to do when an exception occurred in the stream, the answer is the same as the question what to do if an exception ever occurred. You must catch the exception, determine what the error occurred, and respond accordingly. There is no general recipe and can not be. It may be necessary to send a signal to the central logic of the program that the program should be terminated immediately. It may be necessary to write to the log and repeat the last operation. It may be necessary to inform the user, and ask the correct parameters. Maybe the error can be ignored.

    Nobody can stop other threads for you, and it’s wrong - all of a sudden does the thread keep lock or is it in the middle of a critical operation? If necessary, stop the threads yourself. Note that stopping other threads in response to an exception is very rarely the right decision.

    • Well, for example, I have this situation. An exception is thrown if a not found or incorrect config entry in the xml file. That is, the current exception is intended to ensure that the user immediately finds this xml and fixed it manually. No other tasks like setting the default parameter can be. What would you do? - manking
    • @manking: Well, the workflow should report on the status of the task assigned to it. In case of an exception, it should, accordingly, report a failure with an indication of the wrong xml and possibly the line and symbol numbers where the error was found. The main thread will receive a report and will respond accordingly. It does not need to be aware of the fact that exceptions are used in the work flow. - VladD
    • @VladD, and yet (in the case of linux / pthreads), what happens if an exception is thrown in the function launched from pthread_create () that is caught externally to this pthread_create try {} ? - As you know, return from the start function of a thread ends the stream. Then the control is not transferred to the calling function (quite naturally, since the stack is gone, isn't it?). Will such an exception be caught? And if so, how is this implemented? - The question (the author) is really interesting. Personally, I have no experience with threads in crosses, so it's just interesting. - avp
    • @avp: as far as I understand, it is impossible to catch an exception from another thread. If there is an uncaught exception in the stream, the EMNIP process aborts. - VladD
    • And how can I report to the main stream? that an exception occurred in thread 1? - manking

    1) yes, but it will only handle startup problems, but not the thread itself.

    2) process. But in place, in the right stream. Each thread will continue execution.

    3) The flow must stop itself.

      1. It is possible, but exceptions thrown from the stream at the call site will not be caught. It is simply impossible because starting from the point of creation, the code is executed in parallel. If the exception goes beyond the bounds of the primary stream function, it will call std :: terminate (Standard 15.5.1, 30.3.1.2/4).
      2. Depends on the task and the error handling policy. You can crash (the default action), for example, you can make a wrapper over a stream that catches all exceptions that are not caught and signals a simplified example of them. Or
      3. there are wonderful std :: future std :: packaged_task , std :: async
        example