My struct contains:
running: std.Thread.ResetEvent,
playing: std.Thread.ResetEvent,
My constructor is:
22| pub fn init(allocator: std.mem.Allocator, exit: ExitFn) !*Tone {
23| var self: *Tone = try allocator.create(Tone);
24| self.allocator = allocator;
25| self.exit = exit;
26|
27| self.device = null;
28| self.frames = null;
29| self.beep_length_seconds = 0.0;
30|
31| self.running = .{};
32| self.playing = .{};
33|
34| // Initialize the running.
35| self.running.set();
36| // But don't start playing.
37| // self.playing.reset();
38| var thread = try std.Thread.spawn(.{ .allocator = self.allocator }, Tone.run, .{self});
39| std.Thread.detach(thread);
40|
41| return self;
42| }
My error is:
src/@This/deps/sound/api.zig:35:25: error: expected type 'usize', found 'f32'
self.running.set();
Why do I have this error at line 35 and what am I supposed to do?
Seems likely that the error is pointing to the wrong place. Does the error still occur if you comment out line 35?
When I comment line 35 I get:
install transitive failure
└─ standalone-sdl transitive failure
└─ install standalone-sdl transitive failure
└─ zig build-exe standalone-sdl Debug native 2 errors
src/@This/deps/sound/api.zig:38:36: error: expected type 'usize', found 'f32'
var thread = try std.Thread.spawn(.{ .allocator = self.allocator }, Tone.run, .{self});
~~~~~~~~~~^~~~~~
referenced by:
startPlaying: src/@This/deps/sound/api.zig:73:21
frame: src/@This/frontend/screen/panel/HelloWorld/HelloWorld_panel.zig:152:30
frameCurrent: src/@This/frontend/screen/panel/HelloWorld/panels.zig:32:39
frame: src/@This/frontend/screen/panel/HelloWorld/screen.zig:85:30
frame: src/@This/frontend/api.zig:43:53
main: standalone-sdl.zig:171:27
callMain: /usr/local/zig11/lib/std/start.zig:574:32
initEventLoopAndCallMain: /usr/local/zig11/lib/std/start.zig:508:34
callMainWithArgs: /usr/local/zig11/lib/std/start.zig:458:12
main: /usr/local/zig11/lib/std/start.zig:473:34
src/@This/deps/sound/api.zig:178:22: error: error is ignored
self.play();
~~~~~~~~~^~
src/@This/deps/sound/api.zig:178:22: note: consider using 'try', 'catch', or 'if'
referenced by:
init: src/@This/deps/sound/api.zig:38:81
main: standalone-sdl.zig:64:47
callMain: /usr/local/zig11/lib/std/start.zig:574:32
initEventLoopAndCallMain: /usr/local/zig11/lib/std/start.zig:508:34
callMainWithArgs: /usr/local/zig11/lib/std/start.zig:458:12
main: /usr/local/zig11/lib/std/start.zig:473:34
nil@NIL:~/zig/okp$
You are correct. Thanks again.
Why do you declare thread as var, it’s never mutated?
2 Likes
Looking at my code, I have been assuming that a struct’s constants are shared by each implementation of the struct type. I don’t want that so I kept them as var.