x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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)?

Product Version: UE 4.11
Tags:
more ▼

asked May 05 '16 at 05:34 PM in C++ Programming

avatar image

antoinegloum
49 6 12 14

avatar image gedamial May 05 '16 at 05:55 PM

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!

avatar image antoinegloum May 05 '16 at 06:24 PM

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

avatar image Shadowriver May 05 '16 at 06:25 PM

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

avatar image michaelzwm Aug 07 '18 at 06:10 PM

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

(comments are locked)
10|2000 characters needed characters left

3 answers: sort voted first

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

more ▼

answered May 05 '16 at 05:48 PM

avatar image

VictorBurgos
2.4k 59 26 111

avatar image antoinegloum May 05 '16 at 06:23 PM

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

avatar image roomon1 May 06 '16 at 03:18 PM

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. :)

(comments are locked)
10|2000 characters needed characters left

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,

more ▼

answered May 05 '16 at 06:10 PM

avatar image

Shadowriver
33.9k 916 164 1093

avatar image antoinegloum May 05 '16 at 06:34 PM

UE's own threading system seems fine, but is there more documentation than Rama'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.

avatar image Shadowriver May 05 '16 at 07:00 PM

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 imo 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.

avatar image Shadowriver May 05 '16 at 07:00 PM

Your module is valid engine module, one of any that engine have, UE4 module system is kind of like Linux kernel modules :p 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

avatar image quinx Aug 02 '16 at 05:01 AM

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). Rama actually did point that out in the wiki, it is something you definitely don't want to miss.

avatar image antoinegloum May 06 '16 at 06:00 AM

Thanks your explanations :)

(comments are locked)
10|2000 characters needed characters left

,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 :)

more ▼

answered May 06 '16 at 06:49 AM

avatar image

GodOfPen
94 4 4 6

(comments are locked)
10|2000 characters needed characters left
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question