Non-blocking networking with native extension [Solved]


#1

I’m experimenting with native extensions(yes, again :crazy_face:).
Basically any network call or listener from native extension blocks the main thread(at least lib that I’m using).

So what is the suggested way of doing something without blocking main thread or is this even possible with NE?

Any tips, suggestions are most welcome.


#2

Internally, we’ve talked about moving our Thread implementation to the Defold SDK, so it might be there in a few sprints.

In the meantime, you can always use something light weight like Mattias Gustavssons open source thread lib.

Also, depending on your API, you have the option to not do blocking calls? Simple make a call, get some ticket/handle back, and then check the status of the ticket/handle in the update function.


#3

Android, iOS, macOS, HTML5 have non-blocking networking API. Not sure about other platforms, but threading should do the trick there as well.


#4

Thank you @Mathias_Westerdahl .
Actually I am working with uWebSockets
It is a single threaded lib. But It has Async and multithreaded example. But these are blocking the Defold. Of course I am not %100 sure if I am doing everything right. Looks like multithreaded example works correct on Xcode. Trying to figure it out…


#5

Thank you @sergey.lerg.
I am looking for a single lib solution. I guess I can’t handle to work with different APIs for different platforms. Maybe it is the best solution but too much work for me.


#6

Yes, their “async” example isn’t very asynchronous. If you can’t use polling then you need to use threads.


#7

If I use thread.h as you suggested, looks like it runs on different thread (14). If I don’t, it runs on main thread (1). But still blocks the Defold. I guess library is not thread safe at all.
Any idea, suggestion? cause I’m stuck


#8

Well, it should run on a different thread, since you created a new one? Wasn’t that what you were after?

How are you waiting for your thread on the main thread?


#9

Yes, definitely. But somehow It is still blocking the Defold. I was expecting not to.

Using join thread_join( thread )
when its completed thread_destroy( thread )


#10

thread_join is a blocking call. It will wait for the thread to finish. So if you do that on the main thread, it will block the main thread.

Instead, use some other mechanism to check if your thread has finished (E.g. a bool, atomic int or similar)


#11

Ah! shit, sorry. I’m on my way Sir :slight_smile:


#12

Thank you(again :slight_smile: ) work like a charm. Defold run sooo smoothly when it has a stupid web server :slight_smile:
Using t->join(); was my mistake.
Actually multithreaded example is working without the t->join(); too