I wrote a short test program on Linux to test how memcpy performs when used in multiple threads. I didn't expect it to be as devastating. Execution time went from 3.8 seconds to over 2 minutes while running two instances of the program concurrently took about 4.7 seconds. Why is this?
// thread example
#include <iostream>
#include <thread>
#include <string.h>
using namespace std;
void foo(/*int a[3],int b[3]*/)
{
int a[3]={7,8,3};
int b[3]={9,8,2};
for(int i=0;i<100000000;i++){
memcpy(a,b,12*(rand()&1));
}
}
int main()
{
#ifdef THREAD
thread threads[4];
for (char t=0; t<4; ++t) {
threads[t] = thread( foo );
}
for (auto& th : threads) th.join();
cout << "foo and bar completed.\n";
#else
foo();
foo();
foo();
foo();
#endif
return 0;
}