Size of std.c.pthread_mutex_t

My configuration: Linux x64, zig v0.13

Question: Should std.c.pthread_mutex_t have the same size as pthread_mutex_t (from pthread.h)?

// gcc main.c -o cpthread
#include <pthread.h>
#include <stdio.h>

int main() {
    printf("c pthread_mutex_t: %ld\n", sizeof(pthread_mutex_t));
    return 0;
}
// zig build-exe main.zig -lc
const std = @import("std");

pub fn main() void {
    std.debug.print("zig pthread_mutex_t: {}\n", .{@sizeOf(std.c.pthread_mutex_t)});
}

In my case output is:

> c pthread_mutex_t: 40
> zig pthread_mutex_t: 32

So is it a bug or not?

Hi! GCC seems to default to using gnux32 libc on your system, while Zig doesn’t:

1 Like

Hello @reeFridge
welcome to ziggit :slight_smile:

You found a bug!

I am using the following code to display the size for both C and zig.

const std = @import("std");
const c = @cImport(@cInclude("pthread.h"));

pub fn main() void {
    std.debug.print(
        "zig pthread_mutex_t: {}\n",
        .{@sizeOf(std.c.pthread_mutex_t)},
    );
    std.debug.print(
        "C pthread_mutex_t: {}\n",
        .{@sizeOf(c.pthread_mutex_t)},
    );
}

The result depends on the target.

❯ zig run test.zig -lc -target x86_64-linux-gnu
zig pthread_mutex_t: 32
C pthread_mutex_t: 40

❯ zig run test.zig -lc -target x86_64-linux-musl
zig pthread_mutex_t: 40
C pthread_mutex_t: 40

The source code is:

That is for x86_64 gnu the result is 40 if the abi is gnux32 (64-bit mode with 32-bit pointers) and 32 otherwise. The correct is the reverse.

Linux bits/pthreadtypes-arch.h:

#ifdef __x86_64__
# if __WORDSIZE == 64
#  define __SIZEOF_PTHREAD_MUTEX_T 40
2 Likes

Huh, yeah, makes sense!

I created a bug report: Wrong size of std.c.pthread_mutex_t · Issue #21229 · ziglang/zig · GitHub

2 Likes