Can I use std::thread?

Hi, I tried to make a std::thread in UE 4.11.2 and the editor immediately crashes. It compiles and the same source code works outside Unreal in a normal VS2015 C++ project.

So, I am doing something wrong or are std::thread not supported (yet)?

Hey there! Please follow this guide for multi-threading! If this answered your question, please upvote and mark as answered, good luck!

I don’t think Unreal Engine allows you to create you own threads. It doesn’t even allow you to use the new keyword for dynamic allocation, much less with threads!

If you can never use standard C++ library, UE4 has it’s own API wrappers so use UE4 APIs as much as you can this allows UE4 and you game to compile on any platform that it support regardless of incompatibilities. So UE4 got his own threading system, VictorBurgos linked tutorial about it.

But regardless standard C++ should work either way (As well as 3rd party liberies, sometimes youy are forced to use std to communicate with them but you should alwasy convert them up to UE4 types when you start to mix them with engine), there probably some conflict with UE4 code that cause crash (note that UE4 is not aware of thread you making when you use std::thread), multithreading is very delicate matter, it other reason why you should avoid standard libraries,

Yes, I know there is FRunnable and FThreadRunnable but I prefer using the standard library whenever possible :slight_smile:

It seems possible in a DLL but yes, the C++ inside unreal is quite different to what I learnt at school.

It’s same just UE4 use API wrappers to make portability and some features better. Also “new” works normally, simply you can’t use it on UObject because it has custom creation process, i mean extra init code ,where new would just allocate object in memoery, also refection system does not support most of pointer types only UObject pointers… which can be only pointers, thats why new is rarely use, a most you will see it in editor code which use some non-UObject APIs

UE’s own threading system seems fine, but is there more documentation than 's tutorial? These tutorials are great, but it’s sometimes a bit stressful when it’s the only answer you get from your search engine and you don’t find what you are looking for.

So, this should be possible but it’s not recommended? I also hope UE will isolate the editor process from the game at some point, it’s a bit strange to see it crashing all the time when developing.

Theres is no offical documention other then API refrences, UE4 is huge and documentation slowly need to grow, more advance topics like multithreading, onlinesubsystems (which is not advance, but then it lacks in blueprint support), Slate and editor coding (you know exteding editor features), if there is documentation at all it’s very basic and engine source code is practically best learning material for them (aspecially for editor coding and Slate), can’t do much about that, so try searching examples in UE4 github for examples or some tutorials from others, see how editor implements something, thats how i learn to do things in editor… it practicly only way to learn how to code extra stuff to editor

Yes, it is possible, C++ in UE4 is normal C++, but it highly unrecommanded. UE4 simply has it own API wraps and engine monitors activity on them and expect you to use them, if you use standard C++ libery, engine code is not aware of it and it may act strangely aspecially with delicate matter which is threading, UE4 expect to use UE4 threading is used, so if you ride it code on you own threading system it may crash once it start talking to it’s own threading mechanisms (“what? i’m on diffrent thread? i don’t see i’m on diffrent thread… i don’t even see that thread existing, what the hell is going on!!! i’m halting!!11”) Use C++ standard library only if you really force to do it, like communicating with 3rd party libraries or use something that UE4 really don’t support, note that UE4 it self use C++ standard library to wrap them to it’s own APIs.

Your module is valid engine module, one of any that engine have, UE4 module system is kind of like Linux kernel modules :stuck_out_tongue: If something goes wrong in your module entire engine crash. I dont know if Epic will be able to ever isolate game because they would need to run 2 instances of engine not to mention whole system communicating between them, that massive amount of work. Because your module is integral part of engine it self what you see in engine source code you can absolutly do in any other module regardless where it is engine code, plugin or game project. Just remeber if you communicate with editor code in any way, you need to mark module as editor module in *.uproject or *.uplugin and make diffrent module for runtime or use #if WITH_EDITOR because editor code (except Slate, it normaly works in runtime and powers UMG) is not avable in packaged game

Thanks your explanations :slight_smile:

,Actually I tried once. But finally I found it will cause a crash when using std::thread to make multi-thread program.

I’m not sure but I think UE4 using a thread pool instead of creating a new thread directly.Maybe people of Epic can tell me more about how the multi-thread system work in UE4

So I think you should not using std::thread :slight_smile:

I’m right there with you, antoinegloum – The standard libraries always seem like the way to go. In this case, and since we’re using the Unreal Engine as a framework, the code and interfaces it provides really are part of the “standard” libraries for this environment. That makes using them the best practice.

As developers (and especially since we have access to the source code for the engine), we’re free to go back to the basics and reinvent any wheels we like. So if you’re really tied to using the C++ libraries exclusively, feel free to do so. It’ll probably be more work though, possibly even akin to creating your own engine.

Just my two cents. :slight_smile:

One comment: While it is true that there’s not a lot of documentation about multithreading on UE4, I actually found a lot of documentation info as comments directly in the header file ThreadingBase.h (Where FRunnable is declared). actually did point that out in the wiki, it is something you definitely don’t want to miss.

It 2018 and I’m currently using std::thread in ue4.19.1, in a editor plugin, looks good.

… link is not working. i found these :

wiki.unrealengine.com