From the C++11 standard, functionalities related to concurrent programming were introduced. These features can be found in the files:
<conditional_variable>
<future>
<mutex>
<thread>
In this file you have access to several functions and classes that offer the basics for you to build competing applications.
Specifically answering your doubts regarding equivalents in Java:
First we have to remember that ideologically C++ and Java are very different languages, in C++ object orientation is just another tool and is usually not imposed on the user. Therefore, for the use of competing libraries there is not the same requirement to create a class that inherits from Runnable
or Thread
.
This is an example in C++ of the creation of a std::thread
performing the function thread_main
:
std::thread t{ thread_main };
thread_main
can be anything that can be invoked as a function that receives no parameters and returns nothing.
Some examples of implementation:
// Função comum
void thread_main()
{
while (true)
{
std::cout << "Ola de outra thread.\n";
}
}
// Lambda
auto thread_main = []() {
while (true)
{
std::cout << "Ola de outra thread.\n";
}
};
Other C Thread Implementation Differences++:
std::thread
starts executing immediately. (As if calling the method start
java in the constructor)
- before an object of the type
std::thread
be destroyed is required to be called one of the following methods:
std::thread::join
: blocks and waits for the thread to finish running.
std::thread::detach
: frees the thread to continue running independently.
With respect to Java methods, there is only direct equivalence:
Thread.yield
= std::this_thread::yield
In C++ notification functionality (signals) are implemented by a separate class, std::condition_variable
. So we have the following equivalences:
Object.wait
:
Object.notify
:
Object.notifyAll
:
For the other features you asked, there is no direct equivalence:
References: