Multithreading 共享库中的块Go例程
我有一个共享库,它提供了一个函数,在处理过程中将进入等待状态。等待是通过C++提供的条件变量实现的。有人知道如何从Go正确调用这个函数吗 C++函数: 我有一个队列来存储所有要处理的任务Multithreading 共享库中的块Go例程,multithreading,go,block,condition-variable,Multithreading,Go,Block,Condition Variable,我有一个共享库,它提供了一个函数,在处理过程中将进入等待状态。等待是通过C++提供的条件变量实现的。有人知道如何从Go正确调用这个函数吗 C++函数: 我有一个队列来存储所有要处理的任务 queue<Task> tasks; Mutex mutex; condition_variable cv; void process(string img_path) { std::unique_lock<Mutex> lock(mutex); Task task(i
queue<Task> tasks;
Mutex mutex;
condition_variable cv;
void process(string img_path) {
std::unique_lock<Mutex> lock(mutex);
Task task(img_path);
tasks.push_back(task);
cv.wait(); //wait the task to be processed, because i have to process the tasks in a batch way using GPU
}
队列任务;
互斥互斥;
条件变量cv;
作废流程(字符串img\U路径){
std::唯一锁(互斥锁);
任务(img_路径);
任务。推回(任务);
cv.wait();//等待要处理的任务,因为我必须使用GPU以批处理方式处理任务
}
上面的代码只是用来说明关键组件,即条件变量的阻塞等待函数。它将被编译成一个动态库。如果从Python调用,我发现了如中所示的解决方案。有人知道如何从Golang呼叫吗?我认为您的问题归结为,条件变量只是模糊了一些东西。作为一个小的演示,你可以做一个GO程序和一个由C++文件构建的库。
package main
// #cgo LDFLAGS: func.so
// void someFunc();
import "C"
import (
"fmt"
"time"
)
func main() {
fmt.Printf("Before C stuff.\n")
t0 := time.Now()
C.someFunc()
fmt.Printf("%v\n", time.Now().Sub(t0))
}
现在的C++文件:
#include <iostream>
#include <unistd.h>
extern "C" {
void someFunc();
}
void someFunc() {
sleep(10);
std::cout << "And print something\n";
}
使用相同的技术,您应该能够调用阻塞条件变量的函数。go运行时不会在意,并且会在必要时阻止调用goroutine。我认为您的问题归结为,条件变量只是掩盖了一些事情。作为一个小的演示,你可以做一个GO程序和一个由C++文件构建的库。
package main
// #cgo LDFLAGS: func.so
// void someFunc();
import "C"
import (
"fmt"
"time"
)
func main() {
fmt.Printf("Before C stuff.\n")
t0 := time.Now()
C.someFunc()
fmt.Printf("%v\n", time.Now().Sub(t0))
}
现在的C++文件:
#include <iostream>
#include <unistd.h>
extern "C" {
void someFunc();
}
void someFunc() {
sleep(10);
std::cout << "And print something\n";
}
使用相同的技术,您应该能够调用阻塞条件变量的函数。go运行时不会在意,并且会在必要时阻止调用goroutine。您能否分享一些代码,以便我们更好地了解您的意图?谢谢您的帮助。我已经更新了这个问题。你能分享一些代码让我们更好地了解你打算做什么吗?谢谢你的帮助。我已经更新了问题。