Multi-threading module for Garry's Mod servers.
This module allows you to run code in separate threads and communicate with them using data channels.
The installation is fairly straight forward:
- Place the module (.dll) in
garrysmod/lua/bin/
(create the folder bin if it doesn't exist) - Copy
lua/includes/modules/threading.lua
into the same folder ingarrysmod/
There is no extra dependency so this should be all you need. Make sure to use the right DLL for your OS.
require "threading"
local thread = threading.newThread()
local datachannel = thread:OpenChannel( "data" )
datachannel:Receive( function( bytes, tag )
print( "result: " .. datachannel:ReadDouble() )
end )
thread:Run( function() -- You can either pass a string or a function with no upvalues
local datachannel = engine:OpenChannel( "data" )
while true do
local bytes, tag = datachannel:Wait()
if not bytes then return end
local a = datachannel:ReadDouble()
local b = datachannel:ReadDouble()
engine:Sleep( 1000 ) -- Do some expensive work, this is entirely separate
datachannel:StartPacket()
datachannel:WriteDouble( a + b )
datachannel:PushPacket()
end
end )
datachannel:StartPacket()
datachannel:WriteDouble( 45 )
datachannel:WriteDouble( 81 )
datachannel:PushPacket()
local packet = threading.newPacket()
packet:WriteDouble( 11 )
packet:WriteDouble( 54 )
datachannel:PushPacket( packet )
datachannel:PushPacket( packet )
datachannel:PushPacket( packet )
If you wish to build the module yourself, the build files are included in this repo.
To build for Windows you'll need Visual Studio and premake5.exe
- Drop
premake5.exe
next toBuildProjects.bat
- Run
BuildProjects.bat
- Open the VS solution from
projects/windows/
and build - Output is
build/gmsv_gthread_win32.dll
- Get
premake5
and place it next toBuildProjects.bat
- Run
premake5 --os=linux gmake2
- CD into
projects/linux/
make
optionally withconfig=(debug|release)
- Output is
build/gmsv_gthread_linux.dll
(the makefile renames the created .so to the GMod module format)