【发布时间】:2020-02-04 11:46:39
【问题描述】:
标题中提到我有一些奇怪的线程问题。
基本上,我将PS2000 Api 与node-addon-api 包装起来,就像链接示例中一样。
由于这些 api 回调需要类似“C”的函数,所以我用类似这样的东西来实现它们:
CLASSNAME *trampoline[N] = {}
template<int I>f(...){
trampoline[I]->tsfn.BlockingCall(&args, [](..., *args){
//do stuff
if(trampoline->calledCallbacksCount >= trampoline->estimatedCallbackCount){
trampoline->tsfn.Release();
}
})
}
CALLBACK *fptr[N] = {
f<0>, ..., f<N>
}
CLASSNAME::CLASSNAME(){
trampoline[nextEmptyIndex] = this;
this->callback = fptr[nextEmtyIndex];
}
CLASSNAME::read(){
this->tsfn = Napi::ThreadSafeFunction::New(...)
//...
nativethread = std::thread([this]{
//... start device stream & wait some time
if(ps2000_get_streaming_last_values(this->deviceHandle, this->callback) == 0){
// no callback
}
if(this->calledCallbacksCount >= this->estimatedCallbackCount){
this->tsfn.Release();
}
}
}
(
short **overviewBuffers,
short overflow,
unsigned long triggeredAt,
short triggered,
short auto_stop,
unsigned long nValues
)
api函数ps2000_get_streaming_last_values可以多次调用回调,回调次数可以通过**overviewBuffers(nValues)的长度来估算。
当我省略 tsfn.Release() 时,没有错误。
使用时也会返回napi_ok。
感觉好像有某种迟到的回调到空虚 - 但只有当线程被释放时......
我不知所措 - 有人有想法吗?
等等!
传递到 jsCallback.Call({Napi::ArrayBuffer::New(env, *buffer, size)}) 的 memcpied *buffers 会发生什么
这是访问冲突吗?在不发布 tsfn 的情况下如何解决这个问题?
我正在使用 VSCODE 开发 Windows,
我的启动 json 包含:
{
"name": "Debug",
"type": "cppvsdbg",
"request": "launch",
"program": "node",
"args": ["index.ts"],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false
}
但调试器没有触发...
【问题讨论】:
标签: c++ node.js node-addon-api