实例化C++实现类的对象
线程安全性
由于解释器并没有提供线程的调度功能,而是借助宿主语言使用系统机制。而解释器从一开始实现并不是线程安全,所以,所以呢?给线程的执行加了一个限制:线程函数除了可以访问当前thread对象的数据外,不能访问引擎当前变量池中的其他变量。
线程的逻辑,要保存到一个文件里,有一个新的解析器对象去执行。与主线程中的解析器是隔离开的,做到互补干扰。缺点也一目了然:需要访问的已有的全局变量,访问不到。要想访问:重新赋值,或者把数据塞给thread对象。
c++类
//thread.h
#pragma once
#include "varobject.h"
/**
脚本内部的变量存取,都是非线程安全的
*/
class CScript;
namespace os
{
class thread : public varobject
{
public:
//在脚本中展现的类名
static const char* get_script_classname() {return "thread";}
typedef struct st_thread_startup
{
HANDLE hEvent;
thread* pThread;
CStringA toRunFile;
}*lp_st_thread_startup;
thread(std::tr1::shared_ptr<_variant_>* pParams, DWORD dwParams);
~thread();
int QueryMethod(LPCSTR lpszMethodName);
BOOL CallMethod(int nMethodId, std::tr1::shared_ptr<_variant_>* pParams, DWORD dwParamN, std::tr1::shared_ptr<_variant_>& varR, std::tr1::shared_ptr<_variant_>* arrVarValue, DWORD dwCount);
protected:
unsigned start(const char* torunfile, unsigned stacksize, unsigned initflag);
protected:
static unsigned __stdcall thread_proc(void* lp);
std::tr1::shared_ptr<CScript> m_script;
};
}
//thead.cpp
#include "stdafx.h"
#include "thread.h"
#include "script.h"
#include "scriptinc.h"
namespace os
{
#define THREAD_METHOD_START 1
#define THREAD_METHOD_PAUSE 2
#define THREAD_METHOD_RESUME 3
#define THREAD_METHOD_STOP 4
#define THREAD_METHOD_WAIT 5
#define THREAD_METHOD_KILL 6
#define VARIANT_ID_NAME "id"
#define VARIANT_HANDLE_NAME "handle"
thread::thread(std::tr1::shared_ptr<_variant_>* pParams, DWORD dwParams)
{
append(VARIANT_ID_NAME, std::tr1::shared_ptr<_variant_>(new (std::nothro