# Multithreaded graphics

**URL:** <https://root-forum.cern.ch/t/multithreaded-graphics/56127>\
**Category:** ROOT\
**Created:** [August 24, 2023, 3:32am UTC](https://root-forum.cern.ch/t/multithreaded-graphics/56127 "2023-08-24T03:32:55Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![meyerma](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/meyerma/32/29724_2.png) [@meyerma](https://root-forum.cern.ch/u/meyerma)\
**Post date:** [August 24, 2023, 3:32am UTC](https://root-forum.cern.ch/t/multithreaded-graphics/56127/1 "2023-08-24T03:32:55Z")

</div>

Hello,

I have a compiled ROOT C++ program and created a canvas that needs some refresh.  
I usually use gSystem-\>ProcessEvent(). However there is a time consuming calculation part.  
I was thinking to make my computation on any other thread and keep the graphics on the main side

Would you have any suggestion to do so with ROOT ? Should I just use std::thread maybe ?  
In particular how to detect when histogram are ready ? It seems the drawing can only be done in the main thread

---

<div class="post-metadata">

**Author:** ![meyerma](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/meyerma/32/29724_2.png) [@meyerma](https://root-forum.cern.ch/u/meyerma)\
**Post date:** [August 24, 2023, 6:54am UTC](https://root-forum.cern.ch/t/multithreaded-graphics/56127/2 "2023-08-24T06:54:32Z")

</div>

I have been able to do what I needed with `std::thread`… but sometimes I get my program to crash.  
I made sure that my workers finished before refreshing and going to the next iteration.

It looks like my canvas don’t like to be modified while redrawing it. Any suggestion ?

```auto
Assertion failed: ((!E || isa<DeclRefExpr>(E) || isa<MemberExpr>(E) || isa<FunctionParmPackExpr>(E)) && "Invalid Expr argument to DoMarkVarDeclReferenced"), function DoMarkVarDeclReferenced, file SemaExpr.cpp, line 18538.
Abort trap: 6

```

---

<div class="post-metadata">

**Author:** ![linev](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/2bfe46/32.png) [@linev](https://root-forum.cern.ch/u/linev)\
**Post date:** [August 24, 2023, 6:55am UTC](https://root-forum.cern.ch/t/multithreaded-graphics/56127/3 "2023-08-24T06:55:09Z")

</div>

Hi,

Yes, if you are using normal ROOT graphics, it is only possible to update graphics from the main thread - where ROOT application runs and event processing is performed. You also should be aware that histograms or graphs, shown in the canvases, should not be modified from other threads - otherwise you can get concurrency conflicts.

In your case you can run computation with histograms/graphs filling in separate thread and use any signaling mechanism - condition\_variable, semaphore, simple atomic variable - to transfer these graphical objects to main thread, there they will be displayed.

Since recently ROOT provides web-based implementation for `TCanvas`. It always can be tested  
by running `root --web=chrome` or specifying `gROOT->SetWebDisplay("chrome");`.  
It makes much less troubles when used in multi-threaded environment while graphics rendering performed in web browser and not in the applications threads. And it much less depends from `gPad`.

As alternative, you can try `RCanvas` and check `tutorials/rcanvas/rcanvas_mt.cxx` example.  
This new canvas was designed to work properly in multithreaded environment.  
It fully supports all histograms and graphs classes, which can be displayed with web-based TCanvas.

Regards,  
Sergey

---

<div class="post-metadata">

**Author:** ![ferhue](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ferhue/32/29462_2.png) [@ferhue](https://root-forum.cern.ch/u/ferhue)\
**Post date:** [August 24, 2023, 9:36am UTC](https://root-forum.cern.ch/t/multithreaded-graphics/56127/4 "2023-08-24T09:36:10Z")

</div>

Yes, I frequently use the traditional ROOT graphics on one thread, and the heavy calculation staff on a separate thread. I used `TThread` for this in the beginning, but then switched to `std::thread` because of some issues with `TThread::Printf` ([Data races TThread TTimer TApplication · Issue #8365 · root-project/root · GitHub](https://github.com/root-project/root/issues/8365)) as well as longer latency ([TThreadTimer behavior · Issue #8582 · root-project/root · GitHub](https://github.com/root-project/root/issues/8582)).

Did you call `ROOT::EnableThreadSafety`?

If you need one working example, check out the “Steps to reproduce” on [Data races TThread TTimer TApplication · Issue #8365 · root-project/root · GitHub](https://github.com/root-project/root/issues/8365)

There you also have the instructions to find out where your program has races, using valgrind.

Note that whenever you want to modify something from the GUI, you should use add the following in the member functions accessed by external threads:

`R__LOCKGUARD(&fGuiMutex); // scoped mutex`

where fGuiMutex is a private member of the GUI TGMainFrame-derived custom class:

`TMutex fGuiMutex{}; ///< GUI control mutex`

---

<div class="post-metadata">

**Author:** ![meyerma](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/meyerma/32/29724_2.png) [@meyerma](https://root-forum.cern.ch/u/meyerma)\
**Post date:** [August 24, 2023, 11:53am UTC](https://root-forum.cern.ch/t/multithreaded-graphics/56127/5 "2023-08-24T11:53:46Z")

</div>

Dear @ferhue,

Thank you so much ! This was great input :))  
I eventually started using std::thread as it is simple to use.

May I ask about the difference between these two lines ? Sorry, I don’t get explicit and implement MT…

```auto
ROOT::EnableImplicitMT();
ROOT::EnableThreadSafety();

```

Additionally, when you say:

> Note that whenever you want to modify something from the GUI, you should use add the following in the member functions accessed by external threads  
> What do you mean by “modify something” ?  
> Deleting an histogram should be done before calling `R__LOCKGUARD(&fGuiMutex);` ?

---

<div class="post-metadata">

**Author:** ![ferhue](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ferhue/32/29462_2.png) [@ferhue](https://root-forum.cern.ch/u/ferhue)\
**Post date:** [August 24, 2023, 11:59am UTC](https://root-forum.cern.ch/t/multithreaded-graphics/56127/6 "2023-08-24T11:59:47Z")

</div>

Explicit vs implicit, see here:

> **[Multi-threading](https://root.cern/manual/multi_threading/)**
>
> Data Analysis Framework

What I was thinking of is that you have 5 threads, each of them calling fMainWindow-\>ModifyHistogram(), so to avoid races, you need sth like:

```auto
TGMainWindow::ModifyHistogram(x,y) {
R__LOCKGUARD(&fGuiMutex);
fHistogram->Fill(x,y);
fCanvas->Modified();
fCanvas->Update();
}
``
```

---

<div class="post-metadata">

**Author:** ![meyerma](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/meyerma/32/29724_2.png) [@meyerma](https://root-forum.cern.ch/u/meyerma)\
**Post date:** [August 24, 2023, 12:02pm UTC](https://root-forum.cern.ch/t/multithreaded-graphics/56127/7 "2023-08-24T12:02:52Z")

</div>

I have actually a set of histograms that is being deleted on the fly (to free some memory), and some other being created… if I understand well, I should call something like:

```auto
TGMainWindow::ModifyHistogram(x,y) {
R__LOCKGUARD(&fGuiMutex);
fCanvas->Modified();
fCanvas->Update();
}

```

At least… should I make sure the canvas containing a deleted histogram should be deleted too ?

---

<div class="post-metadata">

**Author:** ![ferhue](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ferhue/32/29462_2.png) [@ferhue](https://root-forum.cern.ch/u/ferhue)\
**Post date:** [August 25, 2023, 7:52am UTC](https://root-forum.cern.ch/t/multithreaded-graphics/56127/8 "2023-08-25T07:52:05Z")

</div>

No need to delete the canvas, just delete the histogram from within the GUI class, after you call the lockguard.

```auto
TGMainWindow::DeleteHistogram(TH1* h) {
R__LOCKGUARD(&fGuiMutex);
delete h;
fCanvas->Modified();
fCanvas->Update();
}

```

---

<div class="post-metadata">

**Author:** ![meyerma](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/meyerma/32/29724_2.png) [@meyerma](https://root-forum.cern.ch/u/meyerma)\
**Post date:** [August 25, 2023, 10:12am UTC](https://root-forum.cern.ch/t/multithreaded-graphics/56127/9 "2023-08-25T10:12:33Z")

</div>

> [@ferhue](#):
>
> `TGMainWindow`

I usually just create a canvas. Is there a way to do so from a TCanvas ?

---

<div class="post-metadata">

**Author:** ![ferhue](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ferhue/32/29462_2.png) [@ferhue](https://root-forum.cern.ch/u/ferhue)\
**Post date:** [August 25, 2023, 2:50pm UTC](https://root-forum.cern.ch/t/multithreaded-graphics/56127/10 "2023-08-25T14:50:26Z")

</div>

You could create a class derived from TCanvas, such as:

```auto
class MyCanvas : public TCanvas {
MyCanvas(args) : TCanvas(args) {}
UpdateHistogram(h) { Lockguard...};
TMutex fGuiMutex{};
}

```

Another option is to derive from TGMainFrame and then have a private member with a TRootEmbeddedCanvas. See full example here: [Oscilloscope class for binary waveforms 16-bit - #3 by ferhue](https://root-forum.cern.ch/t/oscilloscope-class-for-binary-waveforms-16-bit/22103/3)  
But the first option is simpler.

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [September 8, 2023, 2:50pm UTC](https://root-forum.cern.ch/t/multithreaded-graphics/56127/11 "2023-09-08T14:50:59Z")

</div>

Closed on September 8, 2023, 2:50pm UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
