Golang bug. Issue with golang cgo pthread mutex lock crash with a lot of goroutines

what’s wrong with this? i already increased ulimit to 102400 etc. it’s not working?! something wrong with golang?

package main

/*
#include <pthread.h>
#include <stdlib.h>

pthread_mutex_t lock;

void lock_init() {
    pthread_mutex_init(&lock, NULL);
}

void lock_destroy() {
    pthread_mutex_destroy(&lock);
}

void lock_mutex() {
    pthread_mutex_lock(&lock);
}

void unlock_mutex() {
    pthread_mutex_unlock(&lock);
}
*/
import "C"
import (
//    "runtime"
    "sync"
    "log"
    "time"
)

func main() {
    C.lock_init()
    defer C.lock_destroy()

    var wg sync.WaitGroup
    for i := 0; i < 10000; i++ {
        wg.Add(1)
        go func() {
            C.lock_mutex()
            // Critical section: You can place code here that needs synchronization.
            time.Sleep(300*time.Nanosecond)
            C.unlock_mutex()
            wg.Done()
        }()
    }

    wg.Wait() // Wait for all goroutines to finish


    log.Printf("same thing will crash in 3 second with more iterations")
    time.Sleep(3*time.Second)
    for i := 0; i < 1000000; i++ {
        wg.Add(1)
        go func() {
            C.lock_mutex()
            // Critical section: You can place code here that needs synchronization.
            time.Sleep(300*time.Nanosecond)
            C.unlock_mutex()
            wg.Done()
        }()
    }

    wg.Wait() // Wait for all goroutines to finish

}

Why are you using pthread_mutex_t and not sync.Mutex?
Just for demonstration purpose?

pthread_create may fail with EAGAIN for other reasons than reaching max number of processes. Memory or stack space maybe.
Other limits than ulimit -u might be relevant, for example sysctl kernel.threads-max or sysctl vm.max_map_count

Not that I read the golang sources, but i can imagine that goroutines are not scheduled on threads that hold a pthread mutex. And so new threads are created until pthread_create fails

Anyway, it seems to be a bad idea to create 100k or more pthreads. They are not as cheap as goroutines

@Helmut_Wais for inter-program golang global mutex locking. i set kernel threads-max to 1024000 but still crash at 80000 goroutines. how to resolve? did SetMaxThreads to 1024000 too, doesnt work. crash

How does that work?

Can you create more threads in a C program using pthread_create?

no. do you have any working solution?

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.