#question about threads
1 messages · Page 1 of 1 (latest)
how do you call it?
do you wait until the thread is done before reading the chunks?
if you access the chunk data while it's being written, it might only be halfway written
you need a mutex if you want to write to the same address on multiple threads, or if you want to write on one thread and read on another
otherwise you'll read incomplete data or write messed up data
it needs to be freed if it was allocated on the heap
anything on the stack is 'freed' automatically when the scope exits
if you use GeneralPurposeAllocator, it will error when your program exits if you forgot to free something
you only have to free stuff created with an allocator
blocks: [300]Block this is part of another struct that was allocated in the hashmap, so it is part of the allocation and will get freed when the hashmap is deinitialized
how are you testing if it works or not?
and where is AutoArrayHashMap defined?
there's nothing obviously wrong with that code, it should work
what happens if you don't call t.detach()?
no?
that's fine
if it printed after creating the mesh then it's not getting locked forever
where do you use the mesh data that was created?
in code
what is the code that uses the mesh?
also if rebuildMesh is false but mesh is undefined then won't it not rebuild the mesh even though it doesn't exist yet?
when is it given to opengl?
it needs to be given to opengl after it's created right?
so where do you detect that and try to send it to opengl?
join should work. does it work if instead of spawning a thread you call the function directly?
spawning a thread that calls a function and then immediately joining it should be equivalent to just calling the function
if the code compiles then there probably isn't a problem?
although you do have a deadlock opportunity here
that's okay, but if createMesh returns an error then the mutex will never get unlocked
const std = @import("std");
var myhm: std.AutoArrayHashMap(usize, usize) = undefined;
var mutex: std.Thread.Mutex = .{};
fn threadFn() !void {
for(myhm.values()) |*value| {
mutex.lock();
defer mutex.unlock();
value.* += 1;
}
}
pub fn main() !void {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();
const alloc = arena.allocator();
myhm = std.AutoArrayHashMap(usize, usize).init(alloc);
for(0..5) |i| try myhm.put(i, i);
std.log.info("before:", .{});
for(myhm.values()) |value| std.log.info("itm: {d}", .{value});
const thread = try std.Thread.spawn(.{}, threadFn, .{});
thread.join();
std.log.info("after:", .{});
for(myhm.values()) |value| std.log.info("itm: {d}", .{value});
}
was trying to make something similar to what you have. this works for me, I'm not sure why join isn't doing the same thing as calling the function directly for you?
yeah
it's the current scope
it has to deallocate the stack frame and stuff, it's not clearing any allocations you do