Code Monkey home page Code Monkey logo

lua53mt's Introduction

声明

源代码来自于Lua官方网站,经过我的简单修改,使Lua成为支持多线程的脚本

#使用 systhread.create(startFunc, ...) --创建一个系统线程 参数1(startFunc): 系统线程的入口函数 参数(...): 要传给函数的参数 返回值: 成功,返回一个包含系统线程信息的userdata;失败,返回false. 注意: 线程创建后处于挂起状态

systhread.resume(systhr) --恢复一个系统线程的执行 参数1(systhr): 通过systhread.create 创建的系统线程(userdata). 返回值: 成功,返回resume的次数;失败,返回false. 注意: NULL

systhread.suspend(systhr) --挂起一个线程 参数1(systhr): 通过systhread.create 创建的系统线程(userdata). 返回值: 成功,返回suspend的次数;失败,返回false. 注意: NULL

systhread.terminate(systhr) --终止一个线程 参数1(systhr): 通过systhread.create 创建的系统线程(userdata). 返回值: 成功,返回true;失败,返回false. 注意: NULL

systhread.wait(systhr, [millisec]) --等待一个线程执行结束 参数1(systhr): 通过systhread.create 创建的系统线程(userdata). 参数2(millisec): 等待的时间(毫秒);如果不指定,则为INFINITE (无穷大) 返回值: 无返回值. 注意: NULL

systhread.exitcode(systhr) --等待一个线程执行结束 参数1(systhr): 通过systhread.create 创建的系统线程(userdata). 返回值: 成功,则返回线程的退出码(exitcode) 注意: 失败,若线程还在运行,返回false;其它的情况,返回nil

systhread.getid(systhr) --获取一个系统线程的ID 参数1(systhr): 通过systhread.create 创建的系统线程(userdata). 返回值: 成功,则返回线程的ID 注意: NULL

systhread.sleep(millisec) --使所在线程休眠 参数1(millisec): 等待的时间(毫秒) 返回值: 无返回值. 注意: NULL

改动

在全局表中加入了systhread这个表,即所有操作'系统(抢先式)线程的功能都封装在了这个表里; 所有的改动都在源代码中用 "//ADDED BY LUZHLON" 注释了.

lua53mt's People

Stargazers

 avatar kyozy avatar  avatar  avatar 爱克斯 avatar  avatar well.james avatar  avatar  avatar Jianxi.Hong avatar wyrover avatar 御风 avatar NicolasX avatar Tang Xiao Lin avatar Flamesky avatar Ynlh avatar

Watchers

NicolasX avatar

lua53mt's Issues

Lua销毁时出错的问题

DeleteCriticalSection(&g->cri_sec)
这一句应该放在
(*g->frealloc)(g->ud, fromstate(L), sizeof(LG), 0);
之前执行,而不是在之后

测试发现多线程并没有发挥多核特性(并没有加速作用),仅仅起到了多线程可重入不出错的作用(相当于加了全局锁)。

以下是我修改你的test.lua代码:

local xxx=0
local start = os.time()

local function ff(n)
   for i=1,100000000,1 do
	    xxx=xxx+1
    end
end

 local st1 = systhread.create(ff, 10)
 local st2 = systhread.create(ff, 10)
 local st3 = systhread.create(ff, 10)
 local st4 = systhread.create(ff, 10)


 st1:resume()
 st2:resume()
 st3:resume()
 st4:resume()

 st1:wait() print('Thread 1 exitcode : '..st1:exitcode())
 st2:wait() print('Thread 2 exitcode : '..st2:exitcode())
 st3:wait() print('Thread 3 exitcode : '..st3:exitcode())
 st4:wait() print('Thread 4 exitcode : '..st4:exitcode())
 
 local finish = os.time()
  print('start is  --> \t'..start.."\n")
  print('finish is  --> \t'..finish.."\n")

  print('finish-start  --> \t'..(finish-start).."\n")
  print('xxx is --> \t'..xxx.."\n")

 systhread.sleep(10000)

测试结果:
$ ./lua.exe test.lua

Thread 1 exitcode : 0
Thread 2 exitcode : 0
Thread 3 exitcode : 0
Thread 4 exitcode : 0
start is --> 1514133425

finish is --> 1514133434

finish-start --> 9

xxx is --> 400000000

问题所在:
如果使用了多核特性,那么 xxx必然不可能是 400000000,因为xxx并没有加锁,多线程若同时运行,+1操作必然重复,xxx必然小于 400000000,然而现在等于 400000000,仿佛加了锁一般,证明lua虚拟机中全局锁在起作用,一个时刻,虚拟机只执行一个线程的字节码。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.