mardi 14 décembre 2021

Class with no blocking methodes with multithreading

I am programming a class which should use multithreading features. The goal is that I don't have any blocking methods from the outside although I use libraries in the class that have blocking functions. I want to run them in their own threads. Unfortunately I get a memory fault (core dumped) error.

What would be the best practice in c++11 to implement something like this and why do I get the error,how can I specify the memory for the function I want to call in the thread best in advance?

My Class

.. 
class foo {

  void startUp();

  foo();
  ~foo();
  
  std::thread * volatile foo_worker = nullptr;

};

void foo::startUp() {
  int retValue = 0;

  foo_worker = new std::thread([&] {
    retValue = blocking_lib_func();
  });
      
}

foo::~foo() {
  ....
  foo_worker->join();
}

My Main

int main() 

  foo test();
  test.statUp()
}

Aucun commentaire:

Enregistrer un commentaire