How to use std.http.Server process request concurrently

const std = @import("std");

pub fn main() !void {
    var gpa = std.heap.GeneralPurposeAllocator(.{
        .safety = true,
        .verbose_log = false,
        .thread_safe = true,
    }){};
    // GeneralPurposeAllocator
    const allocator = gpa.allocator();
    const expect = std.testing.expect;

    const max_header_size = 8192;
    var server = std.http.Server.init(allocator, .{ .reuse_address = true });
    defer server.deinit();

    const address = try std.net.Address.parseIp("127.0.0.1", 8000);
    try server.listen(address);
    const server_port = server.socket.listen_address.in.getPort();

    const server_thread = try std.Thread.spawn(.{}, (struct {
        fn apply(s: *std.http.Server) !void {
            var gpa2 = std.heap.GeneralPurposeAllocator(.{
                .safety = true,
                .verbose_log = false,
                .thread_safe = true,
            }){};
            // GeneralPurposeAllocator
            const allocator2 = gpa2.allocator();
            var res = try s.accept(.{
                .allocator = allocator2,
                .header_strategy = .{ .dynamic = max_header_size },
            });
            defer res.deinit();
            std.debug.print("spawn server_thread res address=[{*}]\n", .{&res});
            defer _ = res.reset();
            try res.wait();

            try expect(res.request.transfer_encoding == .chunked);

            const server_body: []const u8 = "message from server!\n";
            res.transfer_encoding = .{ .content_length = server_body.len };
            try res.headers.append("content-type", "text/plain");
            try res.headers.append("connection", "close");
            try res.send();

            var buf: [128]u8 = undefined;
            const n = try res.readAll(&buf);
            try expect(std.mem.eql(u8, buf[0..n], "ABCD"));
            _ = try res.writer().writeAll(server_body);
            try res.finish();
        }
    }).apply, .{&server});

    const server_thread2 = try std.Thread.spawn(.{}, (struct {
        fn apply(s: *std.http.Server) !void {
            var gpa2 = std.heap.GeneralPurposeAllocator(.{
                .safety = true,
                .verbose_log = false,
                .thread_safe = true,
            }){};
            // GeneralPurposeAllocator
            const allocator2 = gpa2.allocator();
            var res = try s.accept(.{
                .allocator = allocator2,
                .header_strategy = .{ .dynamic = max_header_size },
            });
            defer res.deinit();
            std.debug.print("spawn server_thread2 res address=[{*}]\n", .{&res});

            defer _ = res.reset();
            try res.wait();

            try expect(res.request.transfer_encoding == .chunked);

            const server_body: []const u8 = "message from server!\n";
            res.transfer_encoding = .{ .content_length = server_body.len };
            try res.headers.append("content-type", "text/plain");
            try res.headers.append("connection", "close");
            try res.send();

            var buf: [128]u8 = undefined;
            const n = try res.readAll(&buf);
            try expect(std.mem.eql(u8, buf[0..n], "ABCD"));
            _ = try res.writer().writeAll(server_body);
            try res.finish();
        }
    }).apply, .{&server});

    for (0..100) |i| {
        _ = i;
        const request_bytes =
            "POST / HTTP/1.1\r\n" ++
            "Content-Type: text/plain\r\n" ++
            "Transfer-Encoding: chunked\r\n" ++
            "\r\n" ++
            "1\r\n" ++
            "A\r\n" ++
            "1\r\n" ++
            "B\r\n" ++
            "2\r\n" ++
            "CD\r\n" ++
            "0\r\n" ++
            "\r\n";

        const stream = try std.net.tcpConnectToHost(allocator, "127.0.0.1", server_port);
        defer stream.close();
        _ = try stream.writeAll(request_bytes[0..]);
    }

    server_thread.join();
    server_thread2.join();
}

The code above run crashed at windows 10.

Any help is appreciated ,Thanks.

Welcome to the forum!
You are making it kind of hard for us to help you here. Your code example is hard to read without syntax highlighting. Please enclose your entire code in triple backticks ``` like this:
```

try xy(123);

```
Additionally it would be great if you could provide more information about the crash. Usually Zig should give you a message and a stacktrace when the program crashes in debug mode.

@ IntegratedQuantum
Thanks, the code looks better now. And here is error.

spawn server_thread res address=[http.Server.Response@49d83f6270]
spawn server_thread2 res address=[http.Server.Response@49d93f6320]
error.Unexpected: GetLastError(64): 鎸囧畾鐨勭綉
error.Unexpected: GetLastError(64): 鎸囧畾鐨勭綉
thread 6712 panic: reached unreachable code
???:?:?: 0x7ff7113941ad in ??? (stest.exe)
???:?:?: 0x7ff7113e09fd in ??? (stest.exe)
???:?:?: 0x7ff7113bd048 in ??? (stest.exe)
???:?:?: 0x7ff7113bc8bd in ??? (stest.exe)
???:?:?: 0x7ff7113bd09e in ??? (stest.exe)
???:?:?: 0x7ff7113cc2d2 in ??? (stest.exe)
???:?:?: 0x7ff7113e5156 in ??? (stest.exe)
???:?:?: 0x7ff7113e5286 in ??? (stest.exe)
???:?:?: 0x7ff7113cf2ce in ??? (stest.exe)
???:?:?: 0x7ff711397ffe in ??? (stest.exe)
???:?:?: 0x7ff7113d3837 in ??? (stest.exe)
???:?:?: 0x7ff7113997a7 in ??? (stest.exe)
???:?:?: 0x7ff71142c151 in ??? (stest.exe)
???:?:?: 0x7ff71141e1fe in ??? (stest.exe)
???:?:?: 0x7ff7113ff0f3 in ??? (stest.exe)
???:?:?: 0x7ff7113e91f5 in ??? (stest.exe)
???:?:?: 0x7ff7113e7545 in ??? (stest.exe)
???:?:?: 0x7ff7113d0958 in ??? (stest.exe)
???:?:?: 0x7ff711397b44 in ??? (stest.exe)
???:?:?: 0x7ffe3a72257c in ??? (KERNEL32.DLL)
???:?:?: 0x7ffe3bfcaa77 in ??? (ntdll.dll)
thread 12752 panic: switch on corrupt value
???:?:?: 0x7ff7113bdd68 in ??? (stest.exe)
???:?:?: 0x7ff7113bf249 in ??? (stest.exe)
???:?:?: 0x7ff7113cab1e in ??? (stest.exe)
???:?:?: 0x7ff7113cc305 in ??? (stest.exe)
???:?:?: 0x7ff7113e5156 in ??? (stest.exe)
???:?:?: 0x7ff7113e5286 in ??? (stest.exe)
???:?:?: 0x7ff7113cf2ce in ??? (stest.exe)
???:?:?: 0x7ff711397ffe in ??? (stest.exe)
???:?:?: 0x7ff7113d3837 in ??? (stest.exe)
???:?:?: 0x7ff7113997a7 in ??? (stest.exe)
???:?:?: 0x7ff71142c151 in ??? (stest.exe)
???:?:?: 0x7ff71141e1fe in ??? (stest.exe)
???:?:?: 0x7ff7113ff0f3 in ??? (stest.exe)
???:?:?: 0x7ff7113e91f5 in ??? (stest.exe)
???:?:?: 0x7ff7113e9a05 in ??? (stest.exe)
???:?:?: 0x7ff7113d27a8 in ??? (stest.exe)
???:?:?: 0x7ff7113984a4 in ??? (stest.exe)
???:?:?: 0x7ffe3a72257c in ??? (KERNEL32.DLL)
???:?:?: 0x7ffe3bfcaa77 in ??? (ntdll.dll)
run stest: error: the following command exited with error code 3:
C:\work\prj\stest\zig-out\bin\stest.exe
Build Summary: 3/5 steps succeeded; 1 failed (disable with --summary none)
run transitive failure
└─ run stest failure
error: the following build command failed with exit code 1:
C:\work\prj\stest\zig-cache\o\6e915dc1f93e0acf95799ebbc47010c7\build.exe C:\Users\pebble_4tbrtmr\scoop\apps\zig-dev\0.12.0-dev.1388\zig.exe C:\work\prj\stest C:\work\prj\stest\zig-cache C:\Users\pebble_4tbrtmr\AppData\Local\zig --seed 0x2485bf6c run

That’s unfortunate. It seems that the debug info is broken. Judging by the error, it could be anything. Probably an assertion failure somewhere deep inside the standard library or something like that.

I assume you are using zig 0.11.0? You could try to use a 0.12-dev version instead. I think there were a few fixes to the debug info since the 0.11.0 release, so 0.12-dev might give some better error traces with file and line numbers.

i am using the latest 0.12 version dev 1388,which is the latest version today. Also the code is almost copied from the std.http.server unit test with copy of another server thread and allocator modified.
i meant to precess the request concurrently.

The code is copied from here .(from about line 731).