一个人的战争(2) : 成员函数与线程

全面战争:战锤40K新实机解析:从F2A战术到Waaagh狂潮的设计与实现 在即时战略游戏(RTS)中,F2A(全选并攻击)是一种经典的操作范式,它代表了大规模单位管理与自动化攻击的底层逻辑。其技术原理在于通过快捷键指令高效调度战场资源,实现宏观层面的战术执行,而非依赖精细的微操作。这种设计在降低玩家入门门槛的同时,通过阵营差异化、兵种克制与地形系统构建了深度的策略空间,其技术价值在于平衡了游戏的易上手性与难精通性。在游戏引擎层面,实现大规模同屏单位渲染与复杂AI行为计算,需要依赖LOD系统、实例化渲染与多线程优化等核心技术。应用场景广泛覆盖从快节奏遭遇战到史诗级战役的多种模式。本 阅读详情

         本来今天打算贴BT Client的Tracker Requester的代码的,可是整理的不太完善,因为里面牵扯到很多线程和IOCP的东西,所以我先把一部分自己封装的线程代码贴上来供各位读者评论

       先看下我对Win32线程的最浅封装,

       注意这个是声明在thrad/win32/data_type.h下的


struct ThreadHandle                  //这个控制线程的句柄,ID等,提供这么各类主要是为了跨平台的考虑(并不成熟)
{
        HANDLE            m_hdl;
        t_u_int32        m_tid;
        
bool            m_is_suspend;
}
;

struct ThreadAttr                  //属性类
{
        t_u_int32        stack_size;
        t_u_int32        priority;
        t_u_int32        create_flags;
public:
        ThreadAttr() : stack_size(
0), priority(0), create_flags(0) { }
}
;


typedef unsigned 
int (__stdcall *ThreadFun)(void *);  //win32下的被调用的函数原型

 

下面这部分用了点Bridge模式,有点繁杂,不过是为了一个不成熟的跨平台考虑的

 

/****************************************************threadx_impl_base.h*******************************************/
#include 
"thread_def.h"
#include 
"../misc/non_copy_able.h"

namespace ThreadSpace {

enum ThreadPriority { HIGH, NORMAL, LOW };        //优先级控制

struct ThreadImpl : private NonCopyable            //各个平台要提供的线程控制的Impl_基类
{
        
virtual ThreadHandle* get_handle() = 0;
        
virtual void terminate() = 0;
        
virtual bool suspend() = 0;
        
virtual bool resume() = 0;
        
virtual ThreadPriority get_priority() const = 0;
        
virtual bool set_priority(ThreadPriority tp) = 0;
        
virtual bool is_active() const = 0;
        
//virtual t_u_long wait_for_end(t_u_long timeout) = 0;
        virtual ~ThreadImpl(){ };
}
;

}


/*****************************************************threadx.h*****************************************************************/


#include 
"thread_def.h"
#include 
"threadx_impl_base.h"

namespace ThreadSpace {




class ThreadX : private NonCopyable
{
private:
        ThreadImpl 
*m_pimpl;
public:
        ThreadHandle
* get_handle();
        
void terminate();
        ThreadX(ThreadImpl 
*pimpl);
        
bool suspend();
        
bool resume();
        ThreadPriority get_priority() 
const;
        
bool set_priority(ThreadPriority tp);
        
bool is_active() const;
        
//t_u_long wait_for_end(t_u_long timeout = INFINITE);
        virtual ~ThreadX();
        
}
;



}



/**********************************************************threadx.cpp*********************************************************************/

#include 
"threadx.h"



namespace ThreadSpace {


ThreadX::ThreadX(ThreadImpl 
*pimpl) : m_pimpl(pimpl) { }

bool ThreadX::suspend()
{
        
return m_pimpl->suspend();
}


bool ThreadX::resume()
{
        
return m_pimpl->resume();
}


void ThreadX::terminate()
{
        m_pimpl
->terminate();
}


ThreadPriority ThreadX::get_priority() 
const
{
        
return m_pimpl->get_priority();
}


bool ThreadX::set_priority(ThreadPriority tp)
{
        
return m_pimpl->set_priority(tp);
}


ThreadX::
~ThreadX() { delete m_pimpl; }


bool ThreadX::is_active() const
{
        
return m_pimpl->is_active();
}



ThreadHandle
* ThreadX::get_handle()
{
        
return m_pimpl->get_handle();
}


}


/*******************************************************threadx_impl.h(在win32/)********************************************************/

#pragma once

#include 
"../threadx_impl_base.h"
#include 
"../thread_expt.h"

namespace ThreadSpace {



class Win32ThreadImpl : public ThreadImpl
{
private:
        ThreadHandle    m_handle;
public:
        Win32ThreadImpl(HANDLE hdl, t_u_long tid, 
bool is_suspend);
        
virtual ~Win32ThreadImpl();
public:
        
virtual ThreadHandle* get_handle();
        
virtual void terminate();
        
virtual bool suspend();
        
virtual bool resume();
        
virtual ThreadPriority get_priority() const;
        
virtual bool set_priority(ThreadPriority tp);
        
virtual bool is_active()const;
        
//virtual t_u_long wait_for_end(t_u_long timeout);
}
;




}


/************************************************************threadx_impl.cpp(win32/)*****************************************************/


#include 
"threadx_impl.h"

namespace ThreadSpace {


Win32ThreadImpl::Win32ThreadImpl(HANDLE hdl, t_u_long tid, 
bool is_suspend) //: m_hdl(hdl), 
                                                             m_tid(tid),
                                                             m_is_suspend(is_suspend)
{
        m_handle.m_hdl 
= hdl;
        m_handle.m_tid 
= tid;
        m_handle.m_is_suspend 
= is_suspend;

}


Win32ThreadImpl::
~Win32ThreadImpl() 
{
        ::CloseHandle(m_handle.m_hdl);
}


bool Win32ThreadImpl::is_active()const
{
        t_u_long code;
        
if(::GetExitCodeThread(m_handle.m_hdl, &code) == 0) throw ThreadExpt(::GetLastError());

        
return (code == STILL_ACTIVE);
}

        

void Win32ThreadImpl::terminate()
{
        ::TerminateThread(m_handle.m_hdl, 
-1);
}



bool Win32ThreadImpl::suspend()
{
        
if(m_handle.m_is_suspend) return true;

        
if(::SuspendThread(m_handle.m_hdl) == -1){
                
return false;
        }
else {
                m_handle.m_is_suspend 
= true;
                
return true;
        }

}


bool Win32ThreadImpl::resume()
{
        
if(!m_handle.m_is_suspend) return true;
        
        
if(::ResumeThread(m_handle.m_hdl) == -1){
                
return false;
        }
else{
                m_handle.m_is_suspend 
= false;
                
return true;
        }

}


ThreadPriority Win32ThreadImpl::get_priority() 
const
{
        
int res = ::GetThreadPriority(m_handle.m_hdl);
        
if(res == THREAD_PRIORITY_ERROR_RETURN) throw ThreadExpt(::GetLastError());

        
switch(res){
        
case THREAD_PRIORITY_TIME_CRITICAL:
        
case THREAD_PRIORITY_HIGHEST:
                
return HIGH;
        
case THREAD_PRIORITY_ABOVE_NORMAL:
        
case THREAD_PRIORITY_NORMAL:
        
case THREAD_PRIORITY_BELOW_NORMAL:
                
return NORMAL;
        
default:
                
return LOW;                //case THREAD_PRIORITY_LOWEST:
                                        
//case THREAD_PRIORITY_IDLE:
        }

}


bool Win32ThreadImpl::set_priority(ThreadPriority tp)
{
        
switch(tp){
        
case HIGH:
                
return (::SetThreadPriority(m_handle.m_hdl, THREAD_PRIORITY_HIGHEST) != 0);
        
case LOW:
                
return (::SetThreadPriority(m_handle.m_hdl, THREAD_PRIORITY_LOWEST) != 0);
        
default:
                
return (::SetThreadPriority(m_handle.m_hdl, THREAD_PRIORITY_NORMAL) != 0);
        }

}



/*t_u_long Win32ThreadImpl::wait_for_end(t_u_long timeout)
{
        return ::WaitForSingleObject(m_handle.m_hdl, timeout);
}
*/



ThreadHandle
* Win32ThreadImpl::get_handle()
{
        
return &m_handle;
}







}



以上是大概齐的框架,下面请看如何启动一个线程

 

 

/***************************************************************aux_fun.h*******************************************************/
#pragma once

#include 
"thread_def.h"


namespace ThreadSpace {


class ThreadX;

ThreadX
* create_thread(ThreadFun proc, void *param, const ThreadAttr &attr);   //创建一个线程(在各文件夹下有不同的实现)

void free_thread(ThreadX *&pthread);//delete ThreadX*... 同上

t_u_long wait_thread(ThreadX 
*pthread, t_u_long timeout = INFINITE);  //等待一个线程结束

t_u_long wait_multi_thread(ThreadX 
**ppthread, size_t count, bool wait_all = true, t_u_long timeout ); 等待所有一组结束


}



/**************************************************axu_fun.cpp (win32/)************************************************/

#include 
"../aux_fun.h"
#include 
"../threadx.h"
#include 
"threadx_impl.h"


#include 
<vector>
#include 
<cassert>
namespace ThreadSpace {




ThreadX
* create_thread(ThreadFun proc, void *param, const ThreadAttr &attr)
{
        t_u_int32 tid;
        HANDLE hdl 
= (HANDLE)_beginthreadex(0, attr.stack_size, proc, param, attr.create_flags, &tid);
        
if(hdl == 0) throw(ThreadExpt(errno));
        
        
return new ThreadX(new Win32ThreadImpl(hdl, tid, (attr.create_flags == CREATE_SUSPENDED)));

}



void free_thread(ThreadX *&pthread) 
{
        assert(pthread 
!= 0);
        
try{
                delete pthread;
                pthread 
= 0;
                
        }
catch(...)
        
{

        }

}




t_u_long wait_thread(ThreadX 
*pthread, t_u_long timeout)
{
        assert(pthread 
!= 0);
        t_u_long res 
= ::WaitForSingleObject(pthread->get_handle()->m_hdl, timeout);
        
if(res == WAIT_FAILED) throw (ThreadExpt(::GetLastError()));
        
return res;
}


t_u_long wait_multi_thread(ThreadX 
**ppthread, size_t count, bool wait_all, t_u_long timeout)
{
        assert(ppthread 
!= 0);
        std::vector
<HANDLE> vhdl;
        
for(size_t i = 0; i < count; ++i)
                vhdl.push_back(ppthread[i]
->get_handle()->m_hdl);

        t_u_long res 
= ::WaitForMultipleObjects(count, &vhdl[0], wait_all, timeout);

        
if(res == WAIT_FAILED) throw(ThreadExpt(::GetLastError()));

        
return res;



}


 

恩,下面是关键了,就是我们如何直接把一成员函数当做线程来执行平是我们的习惯是

 


class Test
{
public:
        
void test() { printf("xxxxxxxxx"); } //成员函数
        static unsigned int WINAPI ThreadFun(void *pvoid)
        
{
                Test 
*ptest = static_cast<Test*>(pvoid);
                ptest
->test();
                
return 0;
        }

}
;

int main()
{
        Test test;
        HANDLE hdl 
= (HANDLE)_beginthreadex(0, 0, Test::ThreadFun, (void*)&test, 0, 0);
    
        cin.
get();        
        
return 0; 
}

如果我们希望直接启动这个成员函数呢?OK,下面有些复杂的模板定义,请注意,这些东西也许无意义,但很有意思

 

 

#pragma once

#include 
<memory>

namespace ThreadSpace {

        
template
<class T, class MF, class P>
struct ThreadInfo
{
        T     
*ptype;        //传递进来的那个类的指针;
        MF    mem_fun;    //类成员函数指针
        P    *param;    //参数指针;
        
        ThreadInfo(T 
*pt, MF mf, P *pa) : ptype(pt), mem_fun(mf), param(pa) { }
}
;



template
<class ExecImpl>
class MemFunExector
{
private:
        typedef typename ExecImpl::HandleType        HandleType; 
//见下面的ExecImpBase注释
        typedef typename ExecImpl::Result            Result;
        typedef typename ExecImpl::Param            Param;    
private:
        template
<class C>//这个是那个被执行的线程函数,根据被实例化的不同的ThreadInfo模板来作实例化
        static Result ThreadFunc(Param param)
        
{
                std::auto_ptr
<C> p_info((C*)param);
                
//这样,根据调用一个函数指针来调用相应的成员函数
                return ((p_info->ptype)->*(p_info->mem_fun))(*p_info->param);
        }



public://首先这个模板函数接受一个类的指针,这个类的成员函数指针和成员函数参数
        template<class Type, class MFun, class MParam> 
        HandleType 
operator()(Type *pt, MFun fun, MParam &param)
        
{
                
//ThreadInfo<Type, MFun, MParam>相应的ThreadInfo实例化,这个类同样作为记录参数信息来用的
                
//ThreadFunc再根据上面的ThradInfo类作实例化
                return ExecImpl()(ThreadFunc<ThreadInfo<Type, MFun, MParam> >, (Param)(new ThreadInfo<Type, MFun, MParam>(pt, fun, &param)));
        }


        template
<class Type, class MFun, class MParam, class ATTR> 
        HandleType 
operator()(Type *pt, MFun fun, MParam &param, ATTR &attr)
        
{
                
return ExecImpl(attr)(ThreadFunc<ThreadInfo<Type, MFun, MParam> >, (Param)(new ThreadInfo<Type, MFun, MParam>(pt, fun, &param)));
        }

}
;


//这个类是为了提供不同平台的线程启动函数兼容的,每个平台都应该有一个不同的派生类
template<class H, class R, class P>
struct    ExecImpBase
{
        typedef H            HandleType;    
//注意,这个是启动那个成员函数返回的句柄,例如ThreadX*
        typedef R            Result;        //这个是那个成员函数的返回值,类似unsigned int
        typedef P            Param;     //那个成员函数的参数,类似void*p等,以后定义
        typedef Result    (*PF)(Param);
        
        
virtual HandleType    operator()(PF pf, Param param) = 0;
}
;


}





 

根据平台定义相应的Traits类

#pragma once


#include 
"../mem_fun_exector.h"


namespace ThreadSpace {

class ThreadX;
struct ThreadAttr;

struct ExecutorWin32 : public ExecImpBase<ThreadX*, unsigned int, void*>
{
        
const ThreadAttr        &m_attr;
        
        ExecutorWin32(
const ThreadAttr &attr);
        
        
virtual HandleType operator()(PF pf, Param param);
        
}
;


#define MFExecutor(type, mfun, param, attr) 
        MemFunExector
<ExecutorWin32>()((type), (mfun), (param), (attr))


}

 

 


class Test
{
public:
        
void test(void *p) { printf("xxxxxxxxx"); } //成员函数  注意,这个和上个不同,加了个void*p作为参数,是为了兼容那个模板
       }
;

int main()
{
        Test test;
        ThreadAttr attr;
        
void *p = 0;
        MFExecutor(
&test, Test::test, p, attr);
        cin.
get();        
        
return 0; 
}

 

OK,这部分就到此为止了,有什么错误请各位读者指教,有喜欢模板的朋友请加我QQ 2070341,明天我会贴IOCP的相关类或者Tracker requester类的代码,请各位看官支持,谢谢

深入探索《植物大战僵尸:花园战争2》游戏模组及C#应用 C#(发音为“看井”)是一种由微软开发的强类型、面向对象的编程语言,它最初作为.NET框架的一部分在2000年发布。C#的设计受到了Java语言的显著影响,并且从C和C++中继承了很多特性。C#旨在为开发者提供一种强大而灵活的方式来构建各种.NET应用程序。C#的一些关键特点包括:语言集成查询(LINQ):它允许开发者使用统一的查询语法对各种数据源进行查询。自动垃圾回收:这减少了内存泄漏的可能性,并简化了资源管理。异常处理:这是处理运行时错误的一种优雅方式。泛型。 阅读详情

相关推荐

C++ 线程函数是类的成员函数

C++ 线程函数是类的成员函数 C++ 线程函数是类的成员函数

arm之上的threadx OS编程之总结

一:threadx 开场白 由于工作的原因,涉及到了threadx操作系统,其实我个人觉得,threadx和ucosII十分的相识,只是threadx用于商业性质,而不像ucosII那样提供了源代码。 在消息队列,消息邮箱,事件标志组,信号量与ucosII有相似之处。我个人ucosII源代码自己研究过,对ucos的使用知道些。但是在设计多thread的能力还是欠缺,多个thread之间高效,简

OpenWrt/WLAN/驱动/Android/嵌入式开发总结 2万+

ThreadX_源码.rar

threadx 源码

基于mdm9206 threadx_os的I2c操作相关API介绍

大家好,今天小白给大家介绍一下,关于mdm9206 threadx_os的I2c操作相关的API,希望能和大家一起交流学习。 一:I2C简介 I2C是一种2线总线,用于将低速外设连接到处理器或微控制器。常见的I2C外设包括触摸屏控制器,加速度计,陀螺仪以及环境光和温度传感器。 2线总线包括数据线,时钟线和基本的START,STOP和确认信号,以驱动总线上的传输。 I2C外设也称为I2C从器件...

weixin_38498942的博客 1347

MFC常用的类及其成员函数简介

FC常用类及其成员函数 CRuntimeClass结构 在CRuntimeClass结构中定义了类名、对象所占存储空间的大小、类的版本号等成员变量及动态创建对象、派生关系判断等成员函数。每一个从CObject类派生的类都有一个CRuntimeClass结构同它关联,以便完成在运行时得到对象的信息或基类的信息。 要使用CRuntimeClass结构,必须结合使用RUNTIME_CLASS()宏和其他有关运行时类型识别的MFC宏。 CCmdTarget类 (1)消息发送 MFC应用程序为每个CCmdTarget派生类创建一个称为消息映射表的静态数据结构,可将消息映射到对象所对应的消息处理函数上。 (2)设置光标 BeginWaitCursor() 将光标改为沙漏形状; EndWaitCursor() 将光标改回到之前的形状; RestoreWaitCursor()用于将光标还原为等待状态。 (3)支持自动化 CCmdTarget类支持程序通过COM接口进行交互操作,自动翻译COM接口的方法。 CWinThread类 由CCmdTarget派生,主要工作是创建和处理消息循环。 CWinApp类 从CWinThread类派生,成员函数InitApplication()、InitInstance()、Run()。 在InitInstance()函数中,创建了一个单文档模板类或多文档模板类(CDocTemplate)的对象,并且在文档模板的构造函数中,系统定义的宏RUNTIME_CLASS创建了文档类对象,框架窗口类对象和视图类对象. 在MFC应用程序中有且仅有一个CWinApp派生类的对象,代表程序运行的主线程,代表应用程序本身。 CWnd类 由CCmdTarget类直接派生,是MFC中最基本的GUI对象。公共变量m_hWnd用于存放供API函数调用的窗口句柄。 CframeWnd类 从CWnd类派生而来,主要用来掌管一个窗口。其对象是一个框架窗口,包括边界、标题栏、菜单、最大化按钮、最小化按钮和一个激活的视图。常用成员函数: GetActiveDocument():得到当前文档的指针。 GetActiveView(): 得到当前视图的指针。 SetActiveView(): 激活一个视图。 GetTitle(): 得到框架窗口的标题。 SetTitle(): 设置框架窗口的标题。 SetMessageText(): 设置状态栏文本。 CDocument类 从CCmdTarget派生,作为用户文档的基类,代表了用户存储或打开一个文件。主要功能是把对数据的处理从对用户的界面处理中分离出来,同时提供一个与视图类交互的接口。常用的成员函数有: OnNewDocument(): 建立新文档。 OnOpenDocument(): 打开一个文档。 OnCloseDocument(): 关闭文档。 OnSaveDocument(): 保存文档。 UpdateAllView(): 通知所有视图文档被修改。 SaveModified(): 设置文档修改标志。 CView类 从CWnd类派生而来,是MFC视图类和用户视图类的基类。CWnd::Invalidate()或CWnd::InvalidateRect()可以刷新视图。常用函数有: GetDocument(): 视图类对象访问文档类对象中的数据的. OnDraw(): 这个函数有一个指向CDC类的指针参数, 通过它可能直接调用CDC类上显示数据和图形. 在应用程序窗口出现在及大小发生变化时, 系统将自动调用OnDraw函数 OnInitialUpdate(): 作一些初始化工作. 程序员的主要工作 (1) 重写WinApp派生类的虚函数InitInstance.在这个函数中,按自己的需要创建和显示窗口. (2) 在CDocument的派生类中,声明程序所需的数据和对这些数据进行必要操作的接口函数. (3) 在CViwe类的派生类中编写处理消息的代码.如果在消息处理中需要文档的数据,应该调用该类的成员函数GetDocument来获取文档对象,然后通过文档对象的接口函数对文档中的数据进行操作. (4) 在CViwe类的派生类中的OnDraw函数中编写窗口重绘时的代码. Gilbert觉得以上是很大的类, 下面介绍一些小类: CRect类 矩形类,拥有四个成员变量:top, left, bottom, right。分别表是左上角和右下角的坐标。可以通过以下的方法构造: CRect( int l, int t, int r, int b ); 指明四个坐标 CRect( const RECT& srcRect ); 由RECT结构构造 CRect( LPCRECT lpSrcRect ); 由RECT结构构造 CRect( POINT point, SIZE size ); 有左上角坐标和尺寸构造 CRect( POINT topLeft, POINT bottomRight ); 有两点坐标构造 它的几个成员函数: int Width( ) const; 得到宽度 int Height( ) const; 得到高度 CSize Size( ) const; 得到尺寸 CPoint& TopLeft( ); 得到左上角坐标 CPoint& BottomRight( ); 得到右下角坐标 CPoint CenterPoint( ) const; 得当中心坐标 此外矩形可以和点(CPoint)相加进行位移,和另一个矩形相加得到“并”操作后的矩形。 CPoint类 点的坐标,有两个成员变量:x, y。可以和另一个点相加。 CString类 表示可变长度的字符串。使用CString可不指明内存大小,CString会根据需要自行分配。几个成员函数: GetLength 得到字符串长度 operator + 相当于strcat Compare 比较 CompareNoCase 不区分大小写比较 MakeUpper 改为小写 MakeLower 改为大写

【校招】SHL 的 General Ability (GA, 通用能力测试)

SHL 的 General Ability (GA, 通用能力测试) 文章目录SHL 的 General Ability (GA, 通用能力测试)1、归纳推理题1.1 圈圈线条推理1.2 图形颜色位置推理1.3 箭头指向推理2. 数字推理题攻略3. 语言推理题 1、归纳推理题   归纳推理题我把它总结为三种类型:圈圈线条推理;图形颜色位置推理;箭头指向推理。 1.1 圈圈线条推理   你别看这个题目看起来很复杂,其实很简单,而且这个题型的题目背后原理都一样!我把我的 tips 偷偷分享给你们嘿嘿~先从 A

Sunnyside_ 2万+

展讯平台如何调试之assert的串口打印

虽然,通过串口输出,我们定位到了程序大约在什么地方走不动了,我们就一行一行的注释掉,但是知道建立起了APPtask就死掉了,这时不可能的啊!一定是在其他线程里面死了,导致了assert的! 没问题,我们有办法,在threadx_assert.c里面有_TXAS_SystemAssert函数会打印出死在了什么程序的多少行,OK,本来是用DIAG打印的,手机启动不起来,DIAG是不用指望了,把串口打

lowkeyway的专栏 2318

Oasys安装教程

在基环境下执行然后去 C:\Users.condarc打开 .condarc这个文件 删除干净里面的东西。

weixin_44254178的博客 1295

【STM32F407-threadx】第02章 Threadx移植(上)

第一讲、移植准备 交流群:1092151263 1.1 threadx源码包 1、github:https://github.com/azure-rtos/threadx/releases/tag/v6.1.8_rel 2、百度网盘:【预留位】 1.2 资料包 1、官网:https://rtos.com/solutions/threadx/real-time-operating-system/ 2、API&User Guide:https://rtos.com/solutions/thread

weixin_42856903的博客 1127

10-142 6-4 查询厂商“D“生产的PC和便携式电脑的平均价格

分数 10全屏浏览题目切换布局作者 lhm单位 河北农业大学本题目要求编写SQL语句,查询厂商D生产的PC和便携式电脑的平均价格。

dqd66的博客 2748

一次渗透测试引发的Json格式下CSRF攻击的探索

一次渗透测试引发的Json格式下CSRF攻击的探索

weixin_52501704的博客 1050

std::thread使用及实现原理精讲(全)

线程创建和销毁是昂贵的操作,应尽量避免频繁创建和销毁线程。线程间共享数据时,要确保数据访问的线程安全性。尽量避免在多个线程中访问和修改全局变量或静态变量,除非这些变量是线程安全的。使用 std::thread 时,要确保在程序结束前对所有线程调用 join() 或 detach(),以避免资源泄漏。总之,std::thread 为 C++ 提供了强大而灵活的多线程支持,使得开发者能够更容易地编写并行程序。然而,多线程编程也带来了额外的复杂性和挑战,需要开发者仔细考虑线程间的数据共享和同步问题。

红军不怕远征难,万水千山只等闲 4834

阿里云天池大数据长期赛:金融风控-贷款违约预测(含代码)

本次比赛让自己在大数据的处理流程上有了更明确的认知,从数据清洗、特征工程、数据建模,每一个过程都能学到很多经验和知识,值得去历练。

weixin_46685991的博客 1万+

【Unity】FOV战争迷雾

战争迷雾作为一种常用的游戏效果,被广泛的运用在RTS游戏和最近火热的moba类游戏中,在游戏中需要不断的进行探索和侦察来打开更多的视野区域,或者用来显示友方的位置,或者是躲避敌方等。 常见的战争迷雾主要有两种: 第一种是比较纯粹的将周围指定半径内的区域全部认为可见,即以整个圆形范围来表示可见的区域。由于玩过的RTS比较少,记不清哪些游戏使用的是这种方式了,这里随便找了张图片,大致是这样

MrASL的博客 1万+

基于vue2的echarts3D地球

采用vue2+js实现了echarts3D地球的放大、缩小和旋转

qq_44699269的博客 1132

UCIE Specification详解(五)

Logical Physical Layer(逻辑物理层)

Crazzy_M的博客 1225

多线程之使用:模拟一次宇宙战争来,看看多线程的其中一种使用场景。

多线程使用:模拟一次宇宙战争来,看看多线程的其中一种使用场景。 第一步(使用Runnable) 第一步,通过Runnable构建一个军队的线程,军队发动多次进攻,线程线程执行完便是军队战争结束 把控战争进度通过控制while循环实现 /** * @author liuzonghua * @Package top.maniy.util * @Description: * @date 2018/11/7 16:22 */ public class ArmyRunnable implements Run

IManiy的博客 376

CAN(2)-GD32

GD32中CAN的特征。

小柚子的博客 553

Unity 制造moba英雄联盟战争迷雾2

文接上一篇 如何使用这些数据? 首先我们需要把这些生成的数据读取出来保存到一个int二维数组里 代码如下 public Vector2 startPos;//地图开始点 public Vector2 Scale;//地图大小 public Texture2D renderTexture;//用来渲染的战争迷雾贴图 private Texture2D orignTex...

qq_35977631的博客 1272

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

手机端战争迷雾的实现

先展示效果: 最早是在war3看到战争迷雾,当时觉得真牛逼。到现在技术基本已经成熟,自己也就抽空做一个。 思路还是定在用tile来实现,毕竟从性能优化角度说,tile可以预先烘焙数据,比实时计算要快不少,这样的话手游也可以使用。 先画一批格子: 有了基本的地图数据,我们就需要开始考虑怎么实现我们的战争迷雾。首先把人的坐标转化到地图坐标,先考虑最基本的圆形,根据每个格子距离人的坐标的距离和视野半径比较,我们就可以得到这个格子是否可见。为了能够实现迷雾的效果,我们需要将这些数据保存在一张贴图中。而迷.

wubaohu1314的博客 809

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

从军事策略到GPU计算:ComputeShader线程调度的战争隐喻

本文通过军事策略与GPU计算的类比,深入探讨了ComputeShader线程调度的优化方法。文章以古代军阵为隐喻,解析了Unity中GPU并行任务的线程组织、资源调度及矩阵运算实战案例,揭示了高效协同在计算中的关键作用,为开发者提供了性能调优的实用策略。

xk7n92h3p5m的博客 718

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

战争迷雾开源库测评

【博物纳新】是UWA旨在为开发者推荐新颖、易用、有趣的开源项目,帮助大家在项目研发之余发现世界上的热门项目、前沿技术或者令人惊叹的视觉效果,并探索将其应用到自己项目的可行性。很多时候,我们并不知道自己想要什么,直到某一天我们遇到了它。 更多精彩内容请关注:lab.uwa4d.com 开源库链接:https://lab.uwa4d.com/lab/5b5642f6d7f10a201fd8df39...

UWA—简单优化,优化简单! 1368

不仅是并发:万字图解进程、线程与协程的“资源战争“

摘要: 本文深入解析进程、线程与协程的核心差异,揭示并发编程的本质并非"同时做事"而是"等待时做别的事"。通过工业比喻和Mermaid图表,作者清晰展示了三者的资源开销与适用场景:进程是资源隔离的重型工厂,线程是共享内存的流水线,协程则是轻量级的用户态任务调度。文章特别指出Python多线程在IO密集型任务中的价值,并通过电商案例展示了协程优化带来的5倍性能提升。最后提供了实战代码、性能对比和架构决策指南,帮助开发者根据任务类型(CPU/IO密集型)选择最佳并发模型。

m0_49456009的博客 792

XYBotV2游戏功能实现:五子棋对战与战争雷霆查询教程

XYBotV2是一款功能强大的微信机器人框架,其丰富的游戏功能让群聊互动更加有趣。本文将详细介绍XYBotV2中的两个核心游戏功能:五子棋对战和战争雷霆玩家查询。无论你是机器人新手还是普通用户,都能轻松掌握这些功能的使用方法。 ## 🎮 XYBotV2游戏功能概览 XYBotV2采用插件化设计,所有游戏功能都以独立插件形式实现。这种设计让功能扩展变得简单,同时也保证了系统的稳定性。目前XYB

gitblog_00737的博客 423

Unity战争迷雾深度解析:3种高性能实现方案对比与实战指南

FogOfWar是Unity引擎下一款基于渲染可见区域的战争迷雾解决方案,为RTS、MOBA和策略游戏提供专业级的视野遮蔽系统。这款工具能够显著提升游戏的战术深度和沉浸感,通过多种算法实现高效的视野计算和迷雾渲染,帮助开发者轻松集成战争迷雾功能。 ## 技术痛点:传统战争迷雾实现面临的挑战 在游戏开发中,实现战争迷雾系统常面临三大技术难题:性能开销过大、视野计算不精确、以及地形遮挡处理复杂。传

gitblog_01155的博客 712

Unity3D游戏高性能战争迷雾系统实现

一  效果图   先上效果图吧,这是为了吸引到你们的ヽ(。◕‿◕。)ノ゚ 战争迷雾效果演示图 战争迷雾调试界面演示图   由于是gif录制,为了压缩图片,帧率有点低,实际运行时,参数调整好是不会像这样一卡一顿的。 二  战争迷雾概述   战争迷雾一般用于Startcraft等RTS类型游戏,还有就是War3等Moba类型游戏,主要包括三个概念:未探...

weixin_30667301的博客 2283
上一篇: Anders Hejlsberg语录
下一篇: 一个人的战争(3) : 我眼中的异步与非阻塞
SolidusSnake
博客等级 码龄20年 1粉丝 14原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值