1

I'm a beginner of c++ multi threading program. I created a dummy code for my question. Hoge class is communication class which is connected socket and I assume Hoge::update() is data receiving class via the socket. And if specific data has arrived, Hoge instance pass the data to Fuga instance for specifigc processing.

So my questions are,

  1. I don't want to block Hoge::update(). So after storing data, I don't want to use th_process_data.join(). Are there any better solution for this?
  2. After processing data in another thread, how to return this processed data to Hoge instance. Some callback class is solution?

Hoge.cc

Fuga fuga;
Hoge::update() {
  while(true) {
    if(check_something()) {
      auto data = get_data();
      fuga.push_data(data);
    }
  }
}

Hoge::on_received(const Data& data) {
  std::cout << "received: " << data.id << std::endl;
  // do something...
}

Fuga.cc

std::vector<Data> data_list;
std::mutex mtx;

Fuga::push_data(const Data& data) {
  {
    std::lock_guard<std::mutex> lock(mtx);
    data_list.push_back(data);
  }
  std::thread th_process_data([&]{ do_processing(); });
  // Q1. I don't want to block this thread for Hoge::update()
}

Fuga::do_processing() {
  Data data;
  {
    std::lock_guard<std::mutex> lock(mtx);
    data = data_list.pop();
  }

  // heavy task for data...
  std::this_thread::sleep_for(std::chrono::seconds(3)); 

  // Q2. How to pass this processed data to Hoge::on_received(const Data& data)
}
jef
  • 3,890
  • 10
  • 42
  • 76

1 Answers1

1

Part of your Q is not very clear to me as it seems open ended many possibilites. However your 2 queries are objective, hence I am trying to answer from my recent experience with sockets.

"1. I don't want to block Hoge::update(). So after storing data, I don't want to use th_process_data.join(). Are there any better solution for this?"

In such case, you may do:

th_process_data.detach();

This will save you from blocking on .join(). You may also use std::future and std::promise combo if your design allows. More information can be found in this post.

"2. After processing data in another thread, how to return this processed data to Hoge instance. Some callback class is solution?"

I don't see a big deal in simply calling Hoge::on_received() method and pass the data. The thread will be still the th_process_data only. If you are worried about providing the time slicing and using sleep_for() method for it, then you may also look for std::yield as an alternative.


According to your current design you have put 2 std::mutex in 2 methods. I feel, it's not required.
Also, remember that you are creating a thread every time the Fuga::push_data() is invoked. If this method is invoked frequently and you don't want to load the CPU with the expense of multiple threads creation, then better to create single thread once and wait in it for the data to be received. But that will require a change of design.

Community
  • 1
  • 1
iammilind
  • 68,093
  • 33
  • 169
  • 336