This macro demonstrate usage of ROOT7 graphics from many threads Three different canvases in three different threads are started and regularly updated.
Extra thread created in background and used to run http protocol, in/out websocket communications and process http requests Main application thread (CLING interactive session) remains fully functional
#include <thread>
#include <iostream>
void draw_canvas(
const std::string &title,
Int_t col1)
{
auto hist1 =
new TH1D(
"hist1",
"hist1", 100, -10, 10);
auto hist2 =
new TH1D(
"hist2",
"hist2", 100, -10, 10);
hist1->SetLineColor(col1);
hist2->SetLineColor(
kBlue);
for (
int n = 0;
n < 10000; ++
n) {
hist1->Fill(px - 2);
hist2->Fill(py + 2);
}
int maxloop = 100;
canvas->Show();
std::cout << title << " started" << std::endl;
for (int loop = 0; loop < maxloop; ++loop) {
for (
int n = 0;
n < 10000; ++
n) {
hist1->Fill(px - 2);
hist2->Fill(py + 2);
}
canvas->Modified();
canvas->Update();
canvas->Run(0.2);
}
std::cout << title << " completed" << std::endl;
canvas->Remove();
}
void rcanvas_mt(bool block_main_thread = true)
{
if (block_main_thread) {
gEnv->SetValue(
"WebGui.HttpThrd",
"yes");
gEnv->SetValue(
"WebGui.SenderThrds",
"yes");
}
std::thread thrd1(draw_canvas,
"First canvas",
kRed);
std::thread thrd2(draw_canvas,
"Second canvas",
kBlue);
std::thread thrd3(draw_canvas,
"Third canvas",
kGreen);
if (block_main_thread) {
thrd1.join();
thrd2.join();
thrd3.join();
} else {
thrd1.detach();
thrd2.detach();
thrd3.detach();
}
}
int Int_t
Signed integer 4 bytes (int).
float Float_t
Float 4 bytes (float).
static std::shared_ptr< RCanvas > Create(const std::string &title)
Create new canvas instance.
Provides v7 drawing facilities for TObject types (TGraph, TH1, TH2, etc).
static std::shared_ptr< RWebWindowsManager > & Instance()
Returns default window manager Used to display all standard ROOT elements like TCanvas or TFitPanel.
TDirectory::TContext keeps track and restore the current directory.
1-D histogram with a double per channel (see TH1 documentation)
Random number generator class based on M.
virtual void Rannor(Float_t &a, Float_t &b)
Return 2 numbers distributed following a gaussian with mean=0 and sigma=1.
Namespace for ROOT features in testing.
void EnableThreadSafety()
Enable support for multi-threading within the ROOT code in particular, enables the global mutex to ma...
- Date
- 2018-08-16
- Warning
- This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback is welcome!
- Author
- Sergey Linev
Definition in file rcanvas_mt.cxx.