登录
首页 >  Golang >  Go问答

调用 Go 回调的线程化低级 C/C++ 代码层:从 Go 应用程序开始

来源:stackoverflow

时间:2024-02-26 11:51:25 397浏览 收藏

编程并不是一个机械性的工作,而是需要有思考,有创新的工作,语法是固定的,但解决问题的思路则是依靠人的思维,这就需要我们坚持学习和更新自己的知识。今天golang学习网就整理分享《调用 Go 回调的线程化低级 C/C++ 代码层:从 Go 应用程序开始》,文章讲解的知识点主要包括,如果你对Golang方面的知识点感兴趣,就不要错过golang学习网,在这可以对大家的知识积累有所帮助,助力开发能力的提升。

问题内容

我有一个 Go 应用程序和一些 C API 函数(例如一些 Win32 API 函数),它们异步工作并生成工作线程。此 API 函数从其中一些工作线程调用回调。线程是系统线程,由 C 代码(而不是 Go)在内部创建。现在,我想将 Go 函数作为回调传递给该 C API 函数。因此,Go 回调函数将由 C 函数在工作线程上下文中调用,而 Go 应用程序并不知道。

我们可以假设已经采取了安全措施,并且回调中的所有数据访问都受到互斥体的适当保护,以免干扰主要的 Go 代码。

问题是“Go 是否支持这种情况?”,即回调是否可以正常工作,或者由于 Go 运行时不是为我想做的事情而设计的,所以内部很容易崩溃?


解决方案


我进行了一个 go 回调的实验,从 20 个本机 windows 线程并行调用。回调会增加一个变量,将元素添加到地图中并将值打印在屏幕上。一切都很顺利,所以我认为在更复杂的场景中也不会出现问题。

这是我测试的源代码供其他人使用:

代理.h

#ifndef _proxy_h_
#define _proxy_h_
long threaded_c_func(long param);
#endif

代理.c

#include "proxy.h"
#ifdef win32
#include 
#endif

#define rounds 20

volatile long passed = 0;

extern long long threadedcallback(long cbidx);

dword winapi threadfunc(lpvoid param) {    
    threadedcallback(*((long *)param));
    interlockedincrement(&passed);
}

long threaded_c_func(long cbidx) {

    for (int i  = 0; i < rounds; i++)
    {
        dword threadid = 0;
        createthread(null, 1024*1024, &threadfunc, (lpvoid) &cbidx, 0, &threadid);
    }
    while (passed < rounds)
    {
        sleep(100);
    }
    return rounds;
}

callbacktest.go

package main

/*
#cgo CFLAGS: -I .
#cgo LDFLAGS: -L .

#include "proxy.h"

long threaded_c_func(long param);
*/
import "C"

import (
    "fmt"
    "strconv"
    "sync"
)

var hashTable map[int32]string

var count int32
var mtx sync.Mutex

//export threadedCallback
func threadedCallback(cbidx int) C.longlong {
    mtx.Lock()
    defer mtx.Unlock()
    count++
    hashTable[count] = strconv.Itoa(int(count))
    fmt.Println("Current counter ", count)
    return C.longlong(count)
}

func main() {
    hashTable = make(map[int32]string)
    var expected C.long
    expected = C.threaded_c_func(1)
    if int32(expected) == count {
        fmt.Println("Counters match")
    } else {
        fmt.Println("Expected ", int32(expected), " got ", count)
    }
    for k, v := range hashTable {
        if strconv.Itoa(int(k)) == v {
            fmt.Println(v, " match")
        } else {
            fmt.Println(v, "don't  match")
        }
    }
}

以上就是本文的全部内容了,是否有顺利帮助你解决问题?若是能给你带来学习上的帮助,请大家多多支持golang学习网!更多关于Golang的相关知识,也可关注golang学习网公众号。

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>