CBrother 多线程
CBrother中的多线程是实实在在的多线程,它没有任何全局锁来限制线程的运行时间,在多核CPU的普及的今天,CBrother的多线程性能一定能满足你更多的需求
线程的创建
CBrother通过CreateThread方法创建多线程
function MulThreadFunc(parm)
{
print "MulThreadFunc parm:" + parm;
}
function main(parm)
{
CreateThread(MulThreadFunc,1); //第一个参数是函数,第二个参数是传递的参数
CreateThread(MulThreadFunc,2);
}
结果:
MulThreadFunc parm:1
MulThreadFunc parm:2
线程同步 Lock
多线程同时修改同一个数据,只有一个线程会修改成功,其他线程将抛出SyncException
故多线程修改同一个数据的时候一定要用Lock类来做线程之间的同步
var myLock = new Lock();
函数 | 描述 |
---|---|
lock | 加锁,其他线程再次加锁会阻塞 |
unlock | 解锁之后其他线程才可以获得锁权 |
例子:
var g_count = 0;
var g_lock = new Lock();
function MulThreadFunc(parm)
{
for(var i = 0 ; i < 10 ; i++)
{
g_lock.lock();
g_count++;
print "MulThreadFunc parm:" + parm + " g_count:" + g_count;
g_lock.unlock();
}
}
function main(parm)
{
CreateThread(MulThreadFunc,1);
CreateThread(MulThreadFunc,2);
while(g_count != 20)
{
Sleep(1000);
}
}
结果:
MulThreadFunc parm:1 g_count:1
MulThreadFunc parm:2 g_count:2
MulThreadFunc parm:1 g_count:3
MulThreadFunc parm:2 g_count:4
MulThreadFunc parm:2 g_count:5
MulThreadFunc parm:1 g_count:6
MulThreadFunc parm:2 g_count:7
MulThreadFunc parm:1 g_count:8
MulThreadFunc parm:1 g_count:9
MulThreadFunc parm:2 g_count:10
MulThreadFunc parm:2 g_count:11
MulThreadFunc parm:2 g_count:12
MulThreadFunc parm:2 g_count:13
MulThreadFunc parm:2 g_count:14
MulThreadFunc parm:1 g_count:15
MulThreadFunc parm:2 g_count:16
MulThreadFunc parm:1 g_count:17
MulThreadFunc parm:1 g_count:18
MulThreadFunc parm:1 g_count:19
MulThreadFunc parm:1 g_count:20
Thread 类
CBrother提供了Thread类,它有定时器功能,也可以处理线程消息
函数 | 描述 | 用法 |
---|---|---|
setThreadAction(action) | 给thread添加一个action处理类对象,action类接口在下面讲 | myThread.setThreadAction(action) |
addTimer(ms,func,cnt) | 添加一个心跳,每ms毫秒执行全局函数func,cnt为执行次数,cnt可以不传,缺省为-1,表示一直执行。线程启动前可以在外部调用,线程启动后只能在线程内部调用。 | myThread.addTimer(1000,func);myThread.addTimer(1000,func,5); |
addTimer(ms,clsobj,funcName,cnt) | 添加一个心跳,每ms执行clsobj对象的funcName方法,cnt为执行次数,cnt可以不传,缺省为-1,表示一直执行。线程启动前可以在外部调用,线程启动后只能在线程内部调用。 | myThread.addTimer(1000,clsobj,"test");myThread.addTimer(1000,clsobj,"test",5); |
addMsg(msg) | 给线程发送一条消息,会在action的onMsg方法里被处理,msg可以为任意对象 | myThread.addMsg(msg); |
start() | 启动线程 | myThread.start(); |
stop() | 停止线程 | myThread.stop(); |
thread对象销毁时,线程会停止。但如果thread对象被action对象引用,则线程不会停止。建议在不用线程的时候主动停止。
Thread响应Action类可以有如下接口
function onInit()
,线程启动初始化。
function onEnd()
,线程结束时。
function onMsg(msg)
,接收线程消息。
例子:
class ThreadMsg
{
var msgid;
}
class ThreadAction
{
var thread;
function ThreadAction(t)
{
thread = t;
this.thread.addTimer(1000,this,"onHeart1");
this.thread.addTimer(1000,this,"onHeart2",2);
}
function onInit()
{
print "thread init" ;
}
function onMsg(msg)
{
print "msg id:" + msg.msgid;
}
function onEnd()
{
print "thread end";
}
function onHeart1()
{
print "in action heart1";
}
function onHeart2()
{
print "in action heart2";
}
}
class testHeart
{
function onTestHeart()
{
print "in testHeart";
}
}
function TestTime()
{
print "in time1";
}
function TestTime2()
{
print "in time2";
}
function main(parm)
{
var thread = new Thread();
thread.addTimer(1000,TestTime);
thread.addTimer(1000,TestTime2,2);
thread.addTimer(1000,new testHeart(),"onTestHeart");
thread.setThreadAction(new ThreadAction(thread));
thread.start();
var i = 0;
while(1)
{
Sleep(1000);
var msg = new ThreadMsg();
msg.msgid = i++;
thread.addMsg(msg);
if(i >= 5)
{
break;
}
}
thread.stop();
}
结果:
thread init
msg id:0
in time1
in time2
in testHeart
in action heart1
in action heart2
msg id:1
in time1
in time2
in testHeart
in action heart1
in action heart2
msg id:2
in time1
in testHeart
in action heart1
msg id:3
in time1
in testHeart
in action heart1
msg id:4
thread end
Event 类
多线程编程中,经常需要某个线程在等待其它线程做完一些处理后才能继续执行。CBrother提供了Event类来做这样的事情。
var myEvent = new Event();
函数 | 描述 | 用法 |
---|---|---|
wait(outtime) | 开始等待,outtimeh毫秒后超时,不传递outtime默认为0,表示不超时。一个Event对象只能等待一次,多次调用返回false表示等待失败 | myEvent.wait();myEvent.wait(1000); |
endWait(data) | 通知Event结束等待,data为传递给Event的数据,可以不传递。一个Event对象在wait之后之能触发一次endWait,多次调用返回false表示结束失败 | myEvent.endWait();myEvent.endWait("end"); |
isTimeOut() | 结束等待后用来判断是正常结束还是超时结束。等待没结束之前调用没有意义。 | var isOut = myEvent.isTimeOut(); |
getData() | 等待结束后获取endWait传入的数据。等待没结束之前调用没有意义。 | var data = myEvent.getData(); |
例子:
var g_count = 0;
var g_lock = new Lock();
var g_Event = new Event();
function MulThreadFunc(parm)
{
if(parm == 1)
{
print "wait in thread:" + parm;
g_Event.wait(3000); //等待3秒
print "wait end in thread:" + parm;
}
for(var i = 0 ; i < 10 ; i++)
{
g_lock.lock();
g_count++;
print "MulThreadFunc parm:" + parm + " g_count:" + g_count;
g_lock.unlock();
}
if(g_Event.endWait())
{
print "endWait suc in thread:" + parm;
}
else
{
print "endWait faile in thread:" + parm;
}
}
function main(parm)
{
CreateThread(MulThreadFunc,1);
CreateThread(MulThreadFunc,2);
while(g_count != 20)
{
Sleep(1000);
}
}
结果:
wait in thread:1
MulThreadFunc parm:2 g_count:1
MulThreadFunc parm:2 g_count:2
MulThreadFunc parm:2 g_count:3
MulThreadFunc parm:2 g_count:4
MulThreadFunc parm:2 g_count:5
MulThreadFunc parm:2 g_count:6
MulThreadFunc parm:2 g_count:7
MulThreadFunc parm:2 g_count:8
MulThreadFunc parm:2 g_count:9
MulThreadFunc parm:2 g_count:10
endWait suc in thread:2
wait end in thread:1
MulThreadFunc parm:1 g_count:11
MulThreadFunc parm:1 g_count:12
MulThreadFunc parm:1 g_count:13
MulThreadFunc parm:1 g_count:14
MulThreadFunc parm:1 g_count:15
MulThreadFunc parm:1 g_count:16
MulThreadFunc parm:1 g_count:17
MulThreadFunc parm:1 g_count:18
MulThreadFunc parm:1 g_count:19
MulThreadFunc parm:1 g_count:20
endWait faile in thread:1