I'm having trouble interrupting the workflow. There are numerous messages about thread interruption here, but they seem like stupid errors (which I am sure is also my question) or something that did not help
here
here
This example crashes
My example is based on this, it still crashes
In my example, there is a main thread that creates a workflow. The main thread waits for the user to press the enter button while the workflow is in a closed loop and sleeps at intervals of 1 second. When the user presses the enter button, the main thread interrupts the workflow and then calls the connection, the workflow should throw a thread_interrupted exception in boost :: this_thread :: sleep_for (sec), which should be caught, and then the work function should exit. This allows the main thread to continue, and then exits the program.
#define BOOST_THREAD_USE_LIB 1
#define BOOST_SYSTEM_NO_DEPRECATED 1
#define _WIN32_WINNT 0x0501
#define WINVER 0x0501
#include <boost/thread.hpp>
#include <boost/chrono/include.hpp>
#include <boost/chrono/duration.hpp>
#include <iostream>
using namespace std;
void ThreadFunctionSleep()
{
cout << boost::this_thread::get_id() << " worker thread " << endl;
int counter = 0;
while(1)
{
cout << boost::this_thread::get_id() << " thread iteration " << ++counter << " Press Enter to stop" << endl;
try
{
boost::chrono::seconds sec(1);
boost::this_thread::sleep_for(sec);
}
catch(boost::thread_interrupted&)
{
cout << boost::this_thread::get_id() << " Thread is stopped in ThreadFunction " << endl;
return;
}
catch ( boost::thread_resource_error &)
{
cout << boost::this_thread::get_id() << " boost thread_resource_error" << endl;
return;
}
catch(...)
{
cout << boost::this_thread::get_id() << " worker func other" << endl;
return;
}
}
}
int main()
{
std::cout << boost::this_thread::get_id() << " main thread " << std::endl;
boost::thread t(&ThreadFunctionSleep);
std::cout << boost::this_thread::get_id() << " Waiting for Enter" << std::endl;
char ch;
cin.get(ch);
try
{
t.interrupt();
}
catch(boost::thread_interrupted&)
{
std::cout << boost::this_thread::get_id() << " Thread is stopped" << std::endl;
}
catch ( boost::thread_resource_error &)
{
std::cout << boost::this_thread::get_id() << " boost thread_resource_error" << std::endl;
}
try
{
t.join();
}
catch(boost::thread_interrupted&)
{
std::cout << boost::this_thread::get_id() << " Thread is joined" << std::endl;
}
catch(boost::system::system_error &)
{
std::cout << boost::this_thread::get_id() << " boost system_error" << std::endl;
}
catch(...)
{
std::cout << boost::this_thread::get_id() << " other exception" << std::endl;
}
cout << boost::this_thread::get_id() << " main: thread ended" << endl;
return 0;
}
The result of my program, which I expect, looks something like this:
4f4 main thread
1264 worker thread
4f4 Waiting for Enter
1264 thread iteration 1 Press Enter to stop
1264 thread iteration 2 Press Enter to stop
1264 Thread is stopped in ThreadFunction
4f4 main: thread ended
If the numbers in front are the stream identifier. The problem is that I donβt see it at all. There are three different ways to execute code after pressing Enter. This indicates some sort of multi-threaded problem that I do not see.
1) t.join(),
11cc main thread
11cc644 worker thread
644 thread iteration 1 Press Enter to stop
Waiting for Enter
644 thread iteration 2 Press Enter to stop
644 Thread is joined
644 main: thread ended
Process returned 0 (0x0) execution time : 2.750 s
Press any key to continue.
2)
11cc main thread
11cc644 worker thread
644 thread iteration 1 Press Enter to stop
Waiting for Enter
644 thread iteration 2 Press Enter to stop
644 Thread is joined
644 main: thread ended
Process returned 0 (0x0) execution time : 2.750 s
Press any key to continue.
3) ,
11cc main thread
11cc644 worker thread
644 thread iteration 1 Press Enter to stop
Waiting for Enter
644 thread iteration 2 Press Enter to stop
644 Thread is stopped in ThreadFunction
boost:: thread_interrupted , ? , - , timed_wait() sleep_for()??
boost 1.53.0 MinGW 4.4.1. , runtime-link boost:: thread boost:: system