Working with NSE threads, condition variables, and mutexes in NSE
NSE offers finer control over script parallelism by implementing threads, condition variables, and mutexes. Each NSE script is normally executed inside a Lua coroutine or thread, but it may yield additional worker threads if the programmer decides to do so.
This recipe will teach you how to deal with parallelism in NSE.
How to do it...
NSE threads are recommended for scripts that need to perform network operations in parallel. Let's see how to create NSE threads and use mutexes and condition variables:
- To create a new NSE thread, use the
new_thread()
function from thestdnse
library:local co = stdnse.new_thread(worker_main_function, arg1, arg2, arg3, ...)
- To synchronize access to a network resource, create a mutex on an object:
local my_mutex = nmap.mutex(object)
- Then, the function returned by
nmap.mutex(object)
can be locked as follows:my_mutex("trylock")
- After you are done...