FTXUI: Window doesn't update on adding a element to Elements

I am adding data to Elements output from a separate thread, data is added correctly, but the screen isn’t updated on adding a new element, i need to move the focus area with arrow keys for the UI to update, how can i make it update everytime a new element is added to Elements output

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 20 (17 by maintainers)

Most upvoted comments

scoped_lock is always better than lock_guard and should be used.

However, when locking only one mutex, its behavior is equivalent here.

Note that modifying the Elements (aka std::vector) from one thread, while the “UI” thread is drawing them might result in crashes.

What is the safe way to do this?

You are modifying and reading your data from two thread. You need to use a mutex to ensure you don’t do this at the same time. https://en.cppreference.com/w/cpp/thread/lock_guard

Upon reading on the documentation about lock_guard, I found that it would be better to use a scoped_lock instead as it offers a replacement for lock_guard that provides the ability to lock multiple mutexes using a deadlock avoidance algorithm. (So, I guess much robust compared to lock_guard?)

However, I just started learning c++ so I’m not sure if scoped_lock would be an overkill just for the mutex lock. Can I get an opinion on what you think? Thank you.

Hi Thinkty, my memory is a bit rusty, but I don’t think using scoped_lock will be any better here, as lock guard does the job and I am using only one mutex not multiple.

Regards Vedant

Note that modifying the Elements (aka std::vector) from one thread, while the “UI” thread is drawing them might result in crashes.

What is the safe way to do this?

You are modifying and reading your data from two thread. You need to use a mutex to ensure you don’t do this at the same time. https://en.cppreference.com/w/cpp/thread/lock_guard

If you don’t want to use mutex, you could also use the Receiver class below as a thread-safe queue to request updates from one thread, but execute them on the UI thread. https://github.com/ArthurSonzogni/FTXUI/blob/master/include/ftxui/component/receiver.hpp

Using the method you told, causes crashes Could you investigate why?

and doesn’t work well as I am using a low powered device. I don’t really see why we have this implication.

I tried passing the screen object to component class, but I am unable to do so. It gives some error, like function not defined of some sorts. You will have to investigate and find what happens here. I don’t have enough informations.

Is it possible to refresh just one single component, rather than complete screen? I looked up the source, you are always doing element.first.render()

No, this is currently not possible. This is simpler this way. I could try to print only the required difference, but this would require printing many “displace the cursor to position (x,y)”. This wouldn’t be better.

What OS are you using? (Windows support is quite limited now)

My use case: I am reading from a file descriptor every 100ms, if new data is received it is pushed to Elements variable from the thread. So what i wanna do is, only when i push data, the screen or that component redraws on the screen with updated elements.

Then, use screen.PostEvent(Event::Custom) only when you need a refresh.