E
E
Eugene Ordinary2016-12-16 16:47:49
Programming
Eugene Ordinary, 2016-12-16 16:47:49

How to start and stop a parallel thread?

We need to make such a program. The console program waits in an infinite loop for input of text commands. When entering a command, you need to start calculations in a parallel thread (or process) and switch to the waiting mode for the next command. In the process of calculation, the result is gradually refined depending on the depth of research achieved. When a certain time elapses or when a stop command is entered, the calculation must be stopped immediately and the result returned.
How can this be implemented? Suggest something to read. Only the most simple, understandable, for dummies. I don't understand how to create a thread, how to stop it, how to return results, how to share access to shared variables.
I came across this example

#include <thread>
 
void threadFunction()
{
     // do smth
}
 
int main()
{
     std::thread thr(threadFunction);
     thr.join();
     return 0;
}

And clarified:
Calling join blocks the calling thread (in our case, the main thread) until thr (or rather threadFunction()) has done its job. If the thread function returns a value, it will be ignored.
join blocks the calling thread -- this doesn't work in my case. How to do it really in parallel?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Smithson, 2016-12-16
@Smithson

shatalov.ghost17.ru/winapi/threads.html

From a C++ point of view, a thread is a regular function that has a specific prototype. The CreateThread function is used to create a thread.

J
Johanga, 2016-12-16
@johanga

You need to read about std::mutex.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question