I am learning multi-thread programming. I write a simple program but the performance of my program decreases as the number of threads increases.
I want to measure my program after all thread done thread_local_init()
, so I use the flag
to synchronize.
But the value of elapsed.count() is increasing as the num of thread increases.
Here is my code:
#define THREADS 64 //32 //16 //8 //2
#define OPS 100*1024*1024
std::atomic<int> flag(0);
__thread int init[1024];
uint64_t* ptr_array;
std::chrono::time_point<std::chrono::system_clock> start;
void thread_local_init(){
for(int i=0;i<1024;i++) init[i]=i;
}
void do_alloc(int id){
thread_local_init();
flag++;
uint64_t each_ops = OPS / THREADS;
uint64_t w_start = each_ops * id;
uint64_t w_end = each_ops * (id+1);
while(flag!=THREADS){
}
start = std::chrono::system_clock::now();
for(uint64_t i=w_start;i<w_end;i++){
ptr_array[i] = i;
}
}
int main(int argc, char** argv){
std::thread threads[THREADS];
ptr_array = (uint64_t*)malloc(sizeof(ptr_array)*OPS);
for (int i = 0; i < THREADS; i++) {
threads[i] = std::thread(do_alloc,i);
}
for (auto& t: threads) {
t.join();
}
auto end = std::chrono::system_clock::now();
auto elapsed = end - start;
std::cout << elapsed.count() << '\n';
}