0

I'm on some c++ mobile product, but I need my apps main thread is still running without any blocking when doing some heavy work on the background thread and run back on main thread. But I realized there is no runOnMainThread/runOnUIThread in c++ thread api. I trying to figure it out the issue and found that need to depend library, or create your own thread event queue. Although it is good, but i am thinking to have a behavior which can runOnUIThread.

Oktaheta
  • 606
  • 5
  • 21
  • You can't implement a UI without dependencies. Whatever library you use always has a way to invoke code on the UI thread. Necessarily so since UI is never thread-safe. If it is truly unavailable then just use a better one. Get an answer that is actually helpful to you by describing how you implemented the UI, either naming the OS or the library. – Hans Passant May 10 '18 at 11:28
  • Hi Hans, I have done the project called https://github.com/Taymindis/NonBlockpp , it able to signal to main thread safely one per time. – Oktaheta May 10 '18 at 11:44
  • duplicate? https://stackoverflow.com/questions/34212056/how-do-i-post-code-to-be-run-on-the-android-main-thread-from-a-separate-thread-i – UKMonkey May 10 '18 at 12:03
  • @u That is java, My lib is c++, I suggest you try to use it . If you facing any error. Don't hesitate to discuss together – Oktaheta May 10 '18 at 12:10

3 Answers3

1

How it does not work: the mentioned library creates a timer, installs a SIGALRM signal handler and dispatches queued tasks when signals are fired. This allows tasks being processed on the main thread even when it is busy. However POSIX permits only a small set of async-signal-safe functions to be invoked inside of signal handler. Running arbitrary с++ code inside of signal handler violates that restriction and leaves application in hopelessly doomed state.

user7860670
  • 35,849
  • 4
  • 58
  • 84
  • Being a little pedantic - but not really an answer to the question ;) – UKMonkey May 10 '18 at 10:56
  • Did you mean signal handler scope has some restriction ? May I have some sample please. – Oktaheta May 10 '18 at 11:26
  • @Oktaheta You can find the list of functions that can be safely called from signal handler at the page I've linked. – user7860670 May 10 '18 at 11:30
  • @VTT so far I’ve tested with my c++ mobile programs which calling http, it has no issue, and I’ve run strsss test as well. I just don’t get you mean by restriction within signal handler, because my lib is SIGALRM function chain and it only allow one per time, it has atomic locked – Oktaheta May 10 '18 at 11:41
  • @Oktaheta Alright, have you checked the list of async-signal-safe functions at the page I've linked? Calling anything not in that list is unsafe. – user7860670 May 10 '18 at 11:50
  • @VTT Yes, i've red through before, indeed my lib is implementing SIGALARM sigqueue based. Due to different platform different signal notify method. Maybe I am not so clear about the docs, so you mean that using signal is not a good way to call back to main thread? – Oktaheta May 10 '18 at 12:17
  • @Oktaheta Again, have you checked the list of async-signal-safe functions at the page I've linked? Please answer yes or no. – user7860670 May 10 '18 at 12:24
  • @VTT I've answered. Yes. – Oktaheta May 10 '18 at 12:27
  • @Oktaheta Great! Now another yes or no question: Do you call any functions not present in that list from signal handler? – user7860670 May 10 '18 at 12:29
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/170779/discussion-between-oktaheta-and-vtt). – Oktaheta May 10 '18 at 12:30
  • Thanks VTT. I've got what you mean. This is a good link to explain the issue. https://docs.oracle.com/cd/E19455-01/806-5257/gen-26/index.html – Oktaheta May 10 '18 at 13:17
0

After some research and development, I've created a library called NonBlockpp

it is a small c++ library to allow c++ mobile application able to process the heavy and time consuming task on background and back to Main thread again, It’s been tested and fired the main thread event.

It also allow to save the tasks and fire them later, all the task has no blocking each other and thread safety.

How it works: enter image description here

enter image description here enter image description here

If you found any query or suggestion, please don't hesitate to raise an issue and we can discuss it together.

Oktaheta
  • 606
  • 5
  • 21
  • 1
    Did you review `std::future` etc before writing the above? http://en.cppreference.com/w/cpp/thread/future Also pictures of code are almost unusable. – Richard Critten May 10 '18 at 10:05
  • "How it works" section somehow does not explain how it works at all. Why are there functions appearing at the call stack below `this_thread::sleep_for`? – user7860670 May 10 '18 at 10:12
  • Isn't the better way to do it to have the worker thread call back into the java code; so that you can then call the runOnMainThread on the context to handle the result; rather than having to poll to see if the task is done? – UKMonkey May 10 '18 at 10:28
  • 2
    The better place for this would be on [https://codereview.stackexchange.com/](https://codereview.stackexchange.com/). – user7860670 May 10 '18 at 10:54
  • @Richard, future is blocking the main thread, if you always looping isReady for every background task you doing, it might need a lot of effort – Oktaheta May 10 '18 at 11:28
  • @UKMonkey, you can do that, but you need some jni plug in, and you also need for iOS, and windows, and ... – Oktaheta May 10 '18 at 11:30
  • @VTT I’m sorry to misunderstand your guys, the picture above is the program that use that NonBlock library. It is signal alarm event based, it handle different notify with different platform, The above program just to show you that it works and assert with same thread id. The sleep_for just to show you that drawing something with 16ms per frame – Oktaheta May 10 '18 at 11:31
0

The project has rectify from signal to pollEvent due to signal handler might not be safe to use.

Please take a look the new changed.

NonBlockpp

Usage

Oktaheta
  • 606
  • 5
  • 21