I want to run in parallel (not concurrently)1 four threads doing completely independent things. I'm new to parallelism and I have a couple of questions. The reason why I want to do this is because performance is really important for me. I'm working on a 4-core Windows machine and I'm using C++ in Visual Studio Community 2015.
Should I try to schedule the threads myself, so that each one runs on a different core or should I leave the OS Scheduler to do that? In my opinion I think it would be faster if I force it to run each thread on a different core. How can I do that?
This is what I have tried so far:
#include <thread>
void t1(){//do something}
void t2(){//do something}
void t3(){//do something}
void t4(){//do something}
int main(){
std::thread thread1(t1);
std::thread thread2(t2);
std::thread thread3(t3);
std::thread thread4(t4);
t1.join();
t2.join();
t3.join();
t4.join();
}
I know that join()
blocks the thread until it finishes but I'm not sure if it runs the threads in parallel? Is my code executing the threads concurrently or in parallel?
1 Note:
Concurrency is essentially when two tasks are being performed at the same time. This might mean that one is 'paused' for a short duration, while the other is being worked on.
Parallelism requires that at least two processes/tasks are actively being performed at a particular moment in time.