Anas
Anas

Reputation: 379

threading vs Synchronization

I have a code in C++ that runs for more than 20000 times. the code solves multiple linear systems (matrices) and I am working on optimizing this code. I am just confused whether I should create a thread and kill it every iteration or just create the threads during the first iteration and do Synchronization between all other threads during the rest of the iterations. Why way is more expensive ? I am using thread library in C++ and I am planning to use Semaphore for sync.

Upvotes: 0

Views: 85

Answers (1)

Pavel Umnikov
Pavel Umnikov

Reputation: 66

It's really hard to answer what is better in such specific situation in threading. But there is good practise to create threads on first iteration and kill them after the whole computation sequence, because of thread creation/destruction overhead.

Another interseting topic is multitasking, where you just create tasks/jobs that have portions of data to be computed. It's good because you automatically get computation balancing on CPU(but this depenends on your algorithm).

Upvotes: 1

Related Questions