local thread = require "thread"
local process = require "process"
local event = require "event"

local parentThread, childThread
local parentThreadProc, childThreadProc
local childStarted = false

-- local function currentProcessData()
-- 	return process.info().data
-- end
--
-- print("Non-threaded process data: ", currentProcessData())

parentThread = thread.create(function()
	print("Started parent thread")
	parentThreadProc = process.findProcess()
	-- print("Parent thread process data: ", currentProcessData())
	childThread = thread.create(function()
		print("Started child thread")
		-- print("Child thread process data: ", currentProcessData())
		childThreadProc = process.findProcess()
		-- childThreadCo = coroutine.running()
		childStarted = true
		while true do
			event.pull()
			print("Resume child")
		end
		-- os.sleep(math.huge)
	end)
	-- childThread:attach()
	print("Join child thread inside parent thread")
end)
-- parentThread:attach()

while not childStarted do
	os.sleep(0.1)
end

print("Parent thread process:", parentThreadProc)
print("Child thread process:", childThreadProc)
print("Child status:", childThread:status())
print("Killing parent thread")
parentThread:kill()
print("Parent status:", parentThread:status())
print("Child status:", childThread:status())

print("Skipping 5 events")
for _ = 1, 5 do
	event.pull()
end
print("Child status:", childThread:status())
