What is the best way to create a stream in C ++ when passing a class and index?

I am creating a data parallel program using pthreads and C ++. From the pthread function from the class, I learned how to put a pthread_createpointer to a static C ++ function (and provide an argument to it this).

However, I also need to specify a stream with an index so that it knows what data it works on. I could malloccreate a structure for each thread (with both a pointer to the C ++ class and an index), but it looks like it will add some accounting code and may lead to leaks if the structure is not freed. Is there a better way to do this?

+3
source share
4 answers

You can use Boost.Thread . It provides a safe way for you to pass more than one argument to your caller.

Yes, it has similar types of accounting, just like your question, but it uses C ++ mechanisms to ensure that it does not flow.

+8
source

Is there a better way to do this?

. void *, , , , . ThreadParameters , , , , .

, , , ThreadParameters, ThreadParameters .

+4

++ this , .

, , . this , .

, delete pthread_join. .

+4

( ), . ( this.) . .

class C { 
  Index index_; 
public:
  C(Index &index) : index_(idx) {}
  void Run() { ... }
}

Index workSet;
C worker(workSet);
worker.Run();
+1

Source: https://habr.com/ru/post/1731088/


All Articles