From d04e3ff762c7ca96043c5c5630f25b26d0de5c55 Mon Sep 17 00:00:00 2001 From: Fons Rademakers <Fons.Rademakers@cern.ch> Date: Wed, 28 Jun 2006 14:50:40 +0000 Subject: [PATCH] now works properly on AMD and Mac multicores and shows nicely 4 threads updating each a histogram. git-svn-id: http://root.cern.ch/svn/root/trunk@15602 27541ba8-7e3a-0410-8455-c3a389f83636 --- tutorials/threadsh1.C | 100 +++++++++++++++++++++++------------------- 1 file changed, 55 insertions(+), 45 deletions(-) diff --git a/tutorials/threadsh1.C b/tutorials/threadsh1.C index 0cdcb6e108c..65e4cabbd81 100644 --- a/tutorials/threadsh1.C +++ b/tutorials/threadsh1.C @@ -13,8 +13,9 @@ #include "TThread.h" #include <Riostream.h> -TCanvas *c[5]; -TH1F *hpx[5]; +TCanvas *c[4]; +TH1F *hpx[4]; +TThread *t[5]; Bool_t finished; void *handle(void *ptr) @@ -23,7 +24,7 @@ void *handle(void *ptr) int nfills = 2500000; int upd = 5000; - char name[5]; + char name[32]; sprintf(name,"hpx%ld",nr); TThread::Lock(); hpx[nr] = new TH1F(name,"This is the px distribution",100,-4,4); @@ -31,43 +32,33 @@ void *handle(void *ptr) TThread::UnLock(); Float_t px, py, pz; gRandom->SetSeed(); - for (Int_t i=0;i<nfills;i++) { - c[nr]->cd(); + for (Int_t i = 0; i < nfills; i++) { gRandom->Rannor(px,py); pz = px*px + py*py; - Float_t random = gRandom->Rndm(1); hpx[nr]->Fill(px); if (i && (i%upd) == 0) { - //TThread::Printf("Here I am loop index: %3d , thread: %ld", i, nr); - //printf("Here I am loop index: %3d , thread: %ld\n", i, nr); - if (i == upd) hpx[nr]->Draw(); + if (i == upd) { + TThread::Lock(); + c[nr]->cd(); + hpx[nr]->Draw(); + TThread::UnLock(); + } c[nr]->Modified(); - //c[nr]->Update(); + gSystem->Sleep(10); } - //TThread::Sleep(0, 10000000); } return 0; } -void *updater(void *ptr) +void *joiner(void *) { - long nthrds = (long) ptr; - int i; + t[0]->Join(); + t[1]->Join(); + t[2]->Join(); + t[3]->Join(); + + finished = kTRUE; - while (!finished) { - for (i = 1; i <= nthrds; i++) { - if (c[i]->IsModified()) { - printf("Update canvas %d\n", i); - c[i]->Update(); - } - } - //TThread::Sleep(0, 10000000); - //gSystem->Sleep(10); - } - for (i = 1; i <= nthrds; i++) { - c[i]->Modified(); - c[i]->Update(); - } return 0; } @@ -81,42 +72,61 @@ void threadsh1() finished = kFALSE; gDebug = 1; - c[1] = new TCanvas("c1","Dynamic Filling Example",100,20,400,300); + c[0] = new TCanvas("c0","Dynamic Filling Example",100,20,400,300); + c[0]->SetFillColor(42); + c[0]->GetFrame()->SetFillColor(21); + c[0]->GetFrame()->SetBorderSize(6); + c[0]->GetFrame()->SetBorderMode(-1); + c[1] = new TCanvas("c1","Dynamic Filling Example",510,20,400,300); c[1]->SetFillColor(42); c[1]->GetFrame()->SetFillColor(21); c[1]->GetFrame()->SetBorderSize(6); c[1]->GetFrame()->SetBorderMode(-1); - c[2] = new TCanvas("c2","Dynamic Filling Example",510,20,400,300); + c[2] = new TCanvas("c2","Dynamic Filling Example",100,350,400,300); c[2]->SetFillColor(42); c[2]->GetFrame()->SetFillColor(21); c[2]->GetFrame()->SetBorderSize(6); c[2]->GetFrame()->SetBorderMode(-1); - c[3] = new TCanvas("c3","Dynamic Filling Example",100,350,400,300); + c[3] = new TCanvas("c3","Dynamic Filling Example",510,350,400,300); c[3]->SetFillColor(42); c[3]->GetFrame()->SetFillColor(21); c[3]->GetFrame()->SetBorderSize(6); c[3]->GetFrame()->SetBorderMode(-1); printf("Starting Thread 0\n"); - TThread *h0 = new TThread("h0", updater, (void*) 3); - h0->Run(); + t[0] = new TThread("t0", handle, (void*) 0); + t[0]->Run(); printf("Starting Thread 1\n"); - TThread *h1 = new TThread("h1", handle, (void*) 1); - h1->Run(); + t[1] = new TThread("t1", handle, (void*) 1); + t[1]->Run(); printf("Starting Thread 2\n"); - TThread *h2 = new TThread("h2", handle, (void*) 2); - h2->Run(); + t[2] = new TThread("t2", handle, (void*) 2); + t[2]->Run(); printf("Starting Thread 3\n"); - TThread *h3 = new TThread("h3", handle, (void*) 3); - h3->Run(); + t[3] = new TThread("t3", handle, (void*) 3); + t[3]->Run(); + printf("Starting Thread 4\n"); + t[4] = new TThread("t4", joiner, (void*) 3); + t[4]->Run(); TThread::Ps(); - h1->Join(); - TThread::Ps(); - h2->Join(); - h3->Join(); - finished = kTRUE; - h0->Join(); + while (!finished) { + for (int i = 0; i < 4; i++) { + if (c[i]->IsModified()) { + printf("Update canvas %d\n", i); + c[i]->Update(); + } + } + gSystem->ProcessEvents(); + } + + t[4]->Join(); TThread::Ps(); + + delete t[0]; + delete t[1]; + delete t[2]; + delete t[3]; + delete t[4]; } -- GitLab