C++ æ¨æºã® promise / future / thread ã«å¯¾å¿ãã UE4 æ¨æºã® TPromise / TFuture / FRunnableThread ã®ä½¿ãæ¹
C++ æ¨æºã® std::promise
/ std::future
/ std::thread
C++erã«ã¨ã£ã¦ã¯åºç¤çãªããããã
#include <iostream> #include <future> int main() { using namespace std; promise< int > p; auto f = p.get_future(); auto t = thread( [ =, p = move(p) ] () mutable { p.set_value( 123 ); } ); cout << f.get(); t.join(); }
C++ æ¨æºã§ã¯ lambda-expression ã std:thread
ã¸æ¾ãæããã®ãç°¡åãªã¨ãããå¬ãããã¤ã³ãã
対å¿ãã UE4 æ¨æºã® TPromise
/ TFuture
/ FRunnableThread
UE4 æ¨æºã§ã³ã¼ãä¸ã§åæ§ã®äºãè¨è¿°ãããã¨ããã¨å°ãããã©ãããã
#include "Runtime/Core/Public/Async/Future.h" void your_something_function() { // std::promise ã¨åæ§ TPromise< int > p; // std::future ã® std::promise ããã®åå¾ã¨åæ§ auto f = p.GetFuture(); // ãããã¡ãã£ã¨ããã©ããã: std::thread( ... ) ã«ç¸å½ããã³ã¼ã struct r_type: public FRunnable { TPromise<int> p; r_type( TPromise<int>&& p_ ) : p( std::move( p_ ) ) { } bool Init() override { return true; } uint32 Run() override { p.SetValue( 123 ); return 0; } void Stop() override { } } r( std::move( p ) ); // âãã®2ã¤ã®ãã©ã°ã¯ä»åã®ãããã«ã³ã¼ãä¾ãå¿ç¨ãã人ãããå ´åã«ééããã¨ã¨ã¦ãå±éºãªããç®ç«ã¤ããã«ãã®ããã«æ¸ãã¦ããã¾ããã constexpr auto bAutoDeleteSelf = false; constexpr auto bAutoDeleteRunnable = true; // âããã«âããã lambda-expression ããã®ã¾ã¾æ¾ãè¾¼ããªãã®ã§ FRunnable ã®æ´¾çã¯ã©ã¹ãå®ç¾©ãã¦ãã«ããã«ãã FRunnableThread::Create( &r, TEXT( "promise-future tester"), bAutoDeleteSelf, bAutoDeleteRunnable, 0 ); // std ã®ä¾ã§ã¯ cout ãã¦ããé¨åãä»åã³ã¼ãä¾ã§ã¯ UE_LOG ã§ä»£ç¨ãã¾ããã UE_LOG( LogTemp, Log, TEXT( "promise-future: %d" ), f.Get() ); }
ãããã«ã³ã¼ãä¾ã¨ã㦠C++ æ¨æºã® promise
/ future
/ thread
( / lambda-expression ) ã«å¯¾å¿ããäºãåªå
ã㦠UE4 æ¨æºã®ã³ã¼ããæ¸ãã¨ãããªãã¾ããï¼ FRunnableThread::Create
ã«ã¯å¼æ°ãå°ãç°¡åãªãªã¼ãã¼ãã¼ããããã r_type
ã new
ã㦠bAutoDeleteSelf
ç¸å½ã®ãã©ã°ã true
ã¨ãã¦æ§ããªãç¶æ³ã§ã¯ãã¡ãã使ã£ãæ¹ã楽ãªäºãããããããã¾ããã ï¼
å®ç¨ä¸ã¯ãã®ãããªä¾ã§ã¯ PROMISE-FUTURE-THREAD ç³»ã®ä½ã¬ãã«ã®è¨è¿°ãããã C++ æ¨æºãªã std::async
ã UE4 æ¨æºãªã Async
ã¸ãããã lambda-expression ãæ¾ãæããæ¹ãããå ´åãå¤ãã¨æãã¾ãããã¨ããã諸äºæ
ã«ãã PROMISE-FUTURE ç³»ã®ä½ã¬ãã«ã®ã³ã¼ãè¨è¿°ã欲ããäºãããã¾ããä¾ãã°æ¢åã®ã©ã¤ãã©ãªã¼ãéåæå¦çã®ã³ã¼ã«ããã¯ãã¿ã¼ã³ã® API ãæä¾ãã¦ããã¨ããã§ãå¤ã§ PROMISE 㨠FUTURE ãä½ã£ã¦éåæå¦çãå¼ã³ã¤ã¤ã³ã¼ã«ããã¯ã« PROMISE ã std::move
ã§æãã¦ããã¦ã³ã¼ã«ããã¯å
㧠PROMISE ã«å¤ãã»ãããã API ã®å¼ã³åºãå
ã§ã¯ FUTURE ããå¤ã®åå¾ãè¡ã£ãã FUTURE ã®ç¶æ
ãç£è¦ãããªã©ãããå ´åã¨ãã