Opening more then 1 thread crashes the game

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By AlpacaLord

Hi!
I’m pretty new to godot and I want to use threads to generate chunks of my world.
When I open more then 1 thread the function “createBlock()” crashes the game.
This is the error:
drivers/unix/net_socket_posix.cpp:190 - Socket error: 10054

Here is my code for creating the chunks:

    extends Spatial

var CHUNK = load("res://scenes/Chunk.tscn")

const WORLDSIZE = 2

func _ready():
	for x in range(WORLDSIZE):
		for y in range(WORLDSIZE):
			var chunk = CHUNK.instance()
			var chunkSize = 16
			chunk.init(Vector3(chunkSize*x*2,0,chunkSize*y*2))
			self.call_deferred("add_child",chunk)
	pass

Here is the code for creating a chunk:

 extends Spatial

const CHUNKWIDTH = 16
const CHUNKHEIGHT = 10
const LAYERSIZE = CHUNKWIDTH*CHUNKWIDTH
const TOTALSIZE = LAYERSIZE * CHUNKHEIGHT

var block_class = load("res://scripts/BlockController.gd")

const BLOCK = preload("res://scenes/Block.tscn")

var GRASS = load("res://materials/Grass.tres") as SpatialMaterial
var DIRT = load("res://materials/Dirt.tres") as SpatialMaterial
var STONE = load("res://materials/Stone.tres") as SpatialMaterial
var BRICK = load("res://materials/Brick.tres") as SpatialMaterial

var blocks = []
var blockNodes = []

var thread
var mutex

func init(pos):
	translate(pos)

func _ready():
	thread = Thread.new()
	thread.start(self, "prepareChunk", rand_range(0,100), 2)
	pass

func _process(delta):
	print(thread.is_active())

func createBlock(pos, material):
	var blockInstance = BLOCK.instance()
	blockInstance.init(material,pos)
	self.call_deferred("add_child",blockInstance)
	blockNodes.push_back(blockInstance)
	pass

func prepareChunk(data):
	for y in range(CHUNKHEIGHT):
		for x in range(CHUNKWIDTH):
			for z in range(CHUNKWIDTH):
				var block = block_class.new()
				block.setPos(Vector3(x,-y,z))
				blocks.push_back(block)

	for i in range(blocks.size()):
		var b = blocks[i]
		createBlock(b.pos*2,BRICK)

func _exit_tree():
	thread.wait_to_finish()