thread does not work the way I thought it did in Odin

This commit is contained in:
2026-03-08 05:59:10 -04:00
parent 2279ea063e
commit d9633d8a03

View File

@@ -204,14 +204,14 @@ server_start :: proc(server: ^Server) -> bool {
conn_data.conn = conn
conn_data.source = source
// Spawn a new thread for this connection
t := thread.create(connection_worker_thread)
if t != nil {
//t.init_context = context // We dont actually need need to share the main threads context
t.data = conn_data
thread.start(t)
} else {
// Failed to create thread, close connection
// Spawn with self_cleanup, thread resources are freed automatically on exit
t := thread.create_and_start_with_data(
conn_data,
connection_worker,
self_cleanup = true,
)
if t == nil {
net.close(conn)
free(conn_data, server.allocator)
}
@@ -230,11 +230,9 @@ server_stop :: proc(server: ^Server) {
}
}
// Worker thread procedure
connection_worker_thread :: proc(t: ^thread.Thread) {
defer thread.destroy(t)
conn_data := cast(^Connection_Task_Data)t.data
// Spawn connection worker with rawptr instead of a new thread
connection_worker :: proc(data: rawptr) {
conn_data := cast(^Connection_Task_Data)data
defer free(conn_data, conn_data.server.allocator)
handle_connection(conn_data.server, conn_data.conn, conn_data.source)