Go1.10用Go函数控制C函数的生命周期

package main

import (
    "context"
    "syscall"
    "time"
    "unsafe"
)

/*
int goexit(void* func)
{
    ((int (*)())func)();
    return 0;
}
*/
import "C"

func main() {
    go func() {
        time.Sleep(10e9)
        can()
    }()
    ptr := syscall.NewCallback(wait)
    C.goexit(unsafe.Pointer(ptr))
}

var ctx, can = context.WithCancel(context.Background())

func wait() int {
    <-ctx.Done()
    return 0
}

你可能感兴趣的:(Golang,C,Language)