UE4子线程调用游戏主线程注意事项 (含队列问题)

Non-GameTthread调用GameThread。

——————————————————

案例:

游戏起一个socket客户端与外部socket 服务器保持长连接通信;开辟一个接受线程(via. FRunnableThread::Create)来接受socket服务器的消息,然后在接受消息处理中有涉及到调用GameInstance或GameMode中的方法,导致程序宕掉(自个写的一个简单demo上又都正常,猜测可能是所调用方法的具体实现不同导致的)。

后面用IsInGameThread()判断,在处理函数中触发回调时间的时候,是出于非GameThread的,问题可能在于此?

然后google了下,比较有参考价值的如下两条:

1.How do I execute code on the Game thread from an FRunnable thread?

https://answers.unrealengine.com/questions/262585/how-do-i-execute-code-on-the-game-thread-from-an-f.html

2.[UE4]如何跨线程访问GameThread中的数据

https://blog.youkuaiyun.com/wag2765/article/details/84796693

https://aigo.iteye.com/blog/2296553

大致的方法都是在主线程创建一个队列(eg.static TQueue<FString> To_be_synchronized_object; ),然后子线程收到socket消息时,往主线程的队列里塞,然后主线程在tick中去取队列消息,进行处理。

 

符些参考的链接,含队列:

https://wiki.unrealengine.com/Multi-Threading:_How_to_Create_Threads_in_UE4#What_Not_to_Do

https://zhuanlan.zhihu.com/p/38881269

https://answers.unrealengine.com/questions/702658/when-i-use-tqueuegot-a-compiler-error.html

https://www.reddit.com/r/unrealengine/comments/6ll3ty/tqueue_initialization/

 

====================================================================================

//AsyncTask (ENamedThreads::GameThread ,[](){
        //    //此处在游戏线程上执行的代码
        //});

待研究。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值