Object Oriented Multithread in C ++ 11 Native?

2

How is Multithread Orientado a Objetos implemented in C++11 ? in java this is possible through the% Class extension Thread / Implementing the Runnable interface and overriding the Run method of both. In C++11 I could not identify a Nativa way to do this. What are the equivalent constructs of Java to wait , notify , notifyAll , synchronized , yield , isAlive ?     

asked by anonymous 01.06.2015 / 14:23

1 answer

8

From the C ++ 11 standard, features were introduced regarding concurrent programming. These features can be found in the files:

  • <conditional_variable>
  • <future>
  • <mutex>
  • <thread>

In this files you have access to several functions and classes that provide the basics for you to build competing applications.

Responding specifically to your Java equivalent questions:

Firstly we have to remember that ideologically C ++ and Java are very different languages, in C ++ object orientation is just another tool and is not usually imposed on the user. Therefore, for concurrent libraries, there is no requirement to create a class that inherits from Runnable or Thread .

This is a C ++ example of creating a std::thread that runs the thread_main function:

std::thread t{ thread_main };

thread_main can be anything that can be invoked as a function that does not receive 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 differences in thread implementation in C ++:

  • std::thread starts to run immediately. (As if calling the start method of Java in the constructor)
  • Before an object of type std::thread is destroyed it is mandatory to call one of the following methods:
    • std::thread::join : blocks and waits for the thread to finish executing.
    • std::thread::detach : frees the thread to continue running independently.

Regarding Java methods, there is only one direct equivalence:

  • Thread.yield = std::this_thread::yield

In C ++ the notification functionality (signals) are implemented by a separate class, std::condition_variable . Thus, 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 for, there is no direct equivalence:

  • Thread.isAlive : std::thread does not provide a method to check if the thread is still running, to get this behavior it would be necessary to use one of the methods of 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 that you would like to use as a monitor object in synchronized 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:

01.06.2015 / 20:16