@@ -23,8 +23,9 @@ pub fn main() !void {
2323 // const t5 = try std.Thread.spawn(.{}, testDataChan, .{});
2424 // t5.join();
2525
26- const t6 = try std .Thread .spawn (.{}, testTimerLifecycle , .{});
27- t6 .join ();
26+ // const t6 = try std.Thread.spawn(.{}, testTimerLifecycle, .{});
27+ // t6.join();
28+ try testPreemption ();
2829}
2930
3031pub fn testDataChan () ! void {
@@ -268,18 +269,18 @@ pub fn testPreemption() !void {
268269 }.run , .{& counter2 });
269270
270271 // 暂时禁用协程3,只测试协程1和协程2的抢占
271- // _ = try s.go(struct {
272- // fn run() !void {
273- // const schedule = try zco.getSchedule();
274- // const co = try schedule.getCurrentCo();
275- // var i: usize = 0;
276- // while (i < 20) : (i += 1) {
277- // try co.Sleep(100 * std.time.ns_per_ms); // 睡眠100ms
278- // std.log.info("状态检查协程运行中... {} (观察抢占效果)", .{i});
279- // }
280- // std.log.info("状态检查协程完成", .{});
281- // }
282- // }.run, .{});
272+ _ = try s .go (struct {
273+ fn run () ! void {
274+ const schedule = try zco .getSchedule ();
275+ const co = try schedule .getCurrentCo ();
276+ var i : usize = 0 ;
277+ while (i < 20 ) : (i += 1 ) {
278+ try co .Sleep (100 * std .time .ns_per_ms ); // 睡眠100ms
279+ std .log .info ("状态检查协程运行中... {} (观察抢占效果)" , .{i });
280+ }
281+ std .log .info ("状态检查协程完成" , .{});
282+ }
283+ }.run , .{});
283284
284285 std .log .info ("开始运行调度器,测试时间片抢占..." , .{});
285286 std .log .info ("如果时间片抢占正常工作,应该看到协程1和协程2交替输出进度" , .{});
@@ -341,29 +342,31 @@ pub fn testTimerLifecycle() !void {
341342 }
342343 }.run , .{& counter2 });
343344
344- // 协程3:短时间运行,用于观察调度效果
345- _ = try s .go (struct {
346- fn run () ! void {
347- const schedule = try zco .getSchedule ();
348- const co = try schedule .getCurrentCo ();
349- std .log .info ("协程3开始运行(短时间,会主动让出CPU)" , .{});
345+ // // 协程3:短时间运行,用于观察调度效果
346+ // _ = try s.go(struct {
347+ // fn run() !void {
348+ // const schedule = try zco.getSchedule();
349+ // const co = try schedule.getCurrentCo();
350+ // std.log.info("协程3开始运行(短时间,会主动让出CPU)", .{});
350351
351- for (0.. 10) | i | {
352- std .log .info ("协程3: 步骤 {}" , .{i });
353- try co .Suspend (); // 主动挂起
354- try co .Sleep (50 * std .time .ns_per_ms ); // 睡眠50ms
355- }
352+ // for (0..10) |i| {
353+ // std.log.info("协程3: 步骤 {}", .{i});
354+ // try co.Suspend(); // 主动挂起
355+ // try co.Sleep(50 * std.time.ns_per_ms); // 睡眠50ms
356+ // }
356357
357- std .log .info ("协程3完成" , .{});
358- }
359- }.run , .{});
358+ // std.log.info("协程3完成", .{});
359+ // }
360+ // }.run, .{});
360361
361362 std .log .info ("开始运行调度器,测试时间片抢占..." , .{});
362363 std .log .info ("如果时间片抢占正常工作,应该看到协程1和协程2交替输出进度" , .{});
363364
364365 // 主协程等待一段时间
365- const mainCo = try s .getCurrentCo ();
366- try mainCo .Sleep (5 * std .time .ns_per_s );
366+ // const mainCo = try s.getCurrentCo();
367+ // try mainCo.Sleep(5 * std.time.ns_per_s);
368+
369+ std .time .sleep (10 * std .time .ns_per_s );
367370
368371 std .log .info ("测试完成!" , .{});
369372 std .log .info ("协程1最终计数: {}" , .{counter1 });
0 commit comments