Object-Oriented Multithread in Native C++11?

Asked

Viewed 572 times

2

How is implemented the Multithread Orientado a Objetos in C++11? in java this is possible through the Class Extension Thread / Implementation of the interface Runnable and overwriting the Run Method of both. In C++11 I couldn’t identify a way Nativato do this. What are the equivalent constructions of Java to wait, notify, notifyAll, synchronized, yield, isAlive?

1 answer

7


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:

    • Java

      Object obj;
      // ...
      obj.wait();
      
    • C++

      std::condition_variable obj_cond;
      std::mutex obj_mutex;
      object obj;
      // ...
      std::unique_lock<std::mutex> lock(obj_mutex);
      cond.wait(lock);
      
  • Object.notify:

    • Java

      Object obj;
      // ...
      obj.notify();
      
    • C++

      std::condition_variable obj_cond;
      object obj;
      // ...
      cond.notify_one();
      
  • Object.notifyAll:

    • Java

      Object obj;
      // ...
      obj.notifyAll();
      
    • C++

      std::condition_variable obj_cond;
      object obj;
      // ...
      cond.notify_all();
      

For the other features you asked, there is no direct equivalence:

  • Thread.isAlive: std::thread does not provide a method to check whether the thread is still running, to obtain this behavior it would be necessary to use one of the methods in the previous section or std::promise<T>/std::future<T>.
  • synchronized: In C++ you need to do this manually, you can create a std::mutex associated with each object you would like to use as a monitor object on synchronized of Java. An example of this conversion:

    • Java

      Object obj;
      // ...
      synchronized (obj)
      {
          // Faz algo com obj aqui
      }
      
    • C++

      std::mutex obj_mutex;
      object obj;
      // ...
      {
          std::lock_guard<std::mutex> guard(obj_mutex);
          // Faz algo com obj aqui
      }
      

References:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.