X Tutup
The Wayback Machine - https://web.archive.org/web/20220405131510/https://github.com/nodejs/node-addon-api/issues/1102
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Thread-Safe Function wtih ArrayBuffer #1102

Open
blindson76 opened this issue Nov 8, 2021 · 8 comments
Open

Thread-Safe Function wtih ArrayBuffer #1102

blindson76 opened this issue Nov 8, 2021 · 8 comments
Labels

Comments

@blindson76
Copy link

@blindson76 blindson76 commented Nov 8, 2021

Hi,
I'm trying to some background task and send its results to JS with ArrayBuffer in thread-safe function. But after a while the node main thread terminates by own while worker thread continue working. I'v tried to put some traces to see what happens but couldn't find anything. Please anynody tell me what is wrong in there.

Napi::Value ObjWrap::Start(const Napi::CallbackInfo &info)
{
 Napi::Env env = info.Env();
 if (!info[0].IsNumber())
 {
   throw Napi::TypeError::New(env, "Expected first arg to be number");
 }
 this->length = info[0].As<Napi::Number>().Int32Value();


 tsfn = ThreadSafeFunction::New(
   env,
   info[1].As<Function>(),  // JavaScript function called asynchronously
   "Resource Name",         // Name
   0,                       // Unlimited queue
   1,                       // Only one thread will use this initially
   [this]( Napi::Env ) {        // Finalizer used to clean threads up
     nativeThread.join();
   }
 );
 nativeThread = std::thread([this]{
   auto callback = [this]( Napi::Env env, Function jsCallback, char* data) {
     // Transform native data into JS data, passing it to the provided
     // 'jsCallback' -- the TSFN's JavaScript function.
     fprintf(stderr, "CB:%x\n", data);
     jsCallback.Call( {ArrayBuffer::New(env, data, length, [](void *hint, void* data){
       fprintf(stderr, "Free:%x\n", data);
       free(data);
     })});
     // We are finished with the data.
   };

   tsfn.Acquire();
   int i=0;
   int *test;
   while(true){
     char* buffer = (char*)calloc(length,1);
     fprintf(stderr, "Alloc:%x\n", buffer);
     test = (int*)buffer;
     test[0] = i++;
     napi_status status = tsfn.BlockingCall( buffer, callback );
     std::this_thread::sleep_for(std::chrono::milliseconds(10));
     if ( status != napi_ok )
     {
       // Handle error
       break;
     }
   }
 });
 return Napi::Number::New(env, this->length);
}

and it's output

Alloc:68180a8
CB:68180a8
msg 0
Alloc:68284d8
CB:68284d8
msg 1
Alloc:6838908
CB:6838908
msg 2
Alloc:6848d38
CB:6848d38
msg 3
Alloc:685f008
CB:685f008
msg 4
Alloc:686f438
CB:686f438
msg 5
Alloc:687f868
CB:687f868
msg 6
Alloc:688fc98
CB:688fc98
msg 7
Alloc:68a00c8
CB:68a00c8
msg 8
Alloc:68b04f8
CB:68b04f8
msg 9
Alloc:68c0928
CB:68c0928
msg 10
Alloc:68d0d58
CB:68d0d58
msg 11
Alloc:68e1188
CB:68e1188
msg 12
Alloc:6a00068
CB:6a00068
msg 13
Alloc:6a10498
CB:6a10498
msg 14
Alloc:6a208c8
CB:6a208c8
msg 15
Alloc:6a30cf8
CB:6a30cf8
msg 16
Alloc:6a41128
CB:6a41128
msg 17
Alloc:6a51558
CB:6a51558
msg 18
Alloc:6a61988
CB:6a61988
msg 19
Alloc:6a71db8
CB:6a71db8
msg 20
Alloc:6a821e8
CB:6a821e8
msg 21
Alloc:6a92618
CB:6a92618
msg 22
Alloc:6aa2a48
CB:6aa2a48
msg 23
Alloc:6ab2e78
CB:6ab2e78
msg 24
Alloc:6ac32a8
CB:6ac32a8
msg 25
Alloc:6ad36d8
CB:6ad36d8
msg 26
Alloc:6ae3b08
CB:6ae3b08
msg 27
Alloc:6af3f38
CB:6af3f38
msg 28
Alloc:6b04368
CB:6b04368
msg 29
Alloc:6b14798
CB:6b14798
msg 30
Alloc:6b24bc8
CB:6b24bc8
msg 31
Alloc:6b34ff8
CB:6b34ff8
msg 32
Alloc:6b45428
CB:6b45428
msg 33
Alloc:6b55858
CB:6b55858
msg 34
Alloc:6b65c88
CB:6b65c88
msg 35
Alloc:6b760b8
CB:6b760b8
msg 36
Alloc:6b864e8
CB:6b864e8
msg 37
Alloc:6b96918
CB:6b96918
msg 38
Alloc:6ba6d48
CB:6ba6d48
msg 39
Alloc:6bb7178
CB:6bb7178
msg 40
Alloc:6bc75a8
CB:6bc75a8
msg 41
Alloc:6bd79d8
CB:6bd79d8
msg 42
Alloc:6be7e08
CB:6be7e08
msg 43
Alloc:6c00068
CB:6c00068
msg 44
Alloc:6c10498
CB:6c10498
msg 45
Alloc:6c208c8
CB:6c208c8
msg 46
Alloc:6c30cf8
CB:6c30cf8
msg 47
Alloc:6c41128
CB:6c41128
msg 48
Alloc:6c51558
CB:6c51558
msg 49
Alloc:6c61988
CB:6c61988
msg 50
Alloc:6c71db8
CB:6c71db8
msg 51
Alloc:6c821e8
CB:6c821e8
msg 52
Alloc:6c92618
CB:6c92618
msg 53
Alloc:6ca2a48
CB:6ca2a48
msg 54
Alloc:6cb2e78
CB:6cb2e78
msg 55
Alloc:6cc32a8
CB:6cc32a8
msg 56
Alloc:6cd36d8
CB:6cd36d8
msg 57
Alloc:6ce3b08
CB:6ce3b08
msg 58
Alloc:6cf3f38
CB:6cf3f38
msg 59
Alloc:6d04368
CB:6d04368
msg 60
Alloc:6d14798
CB:6d14798
msg 61
Alloc:6d24bc8
CB:6d24bc8
msg 62
Alloc:6d34ff8
CB:6d34ff8
msg 63
Alloc:6d45428
CB:6d45428
msg 64
Alloc:6d55858
CB:6d55858
msg 65
Alloc:6d65c88
CB:6d65c88
msg 66
Alloc:6d760b8
CB:6d760b8
msg 67
Alloc:6d864e8
CB:6d864e8
msg 68
Alloc:6d96918
CB:6d96918
msg 69
Alloc:6da6d48
CB:6da6d48
msg 70
Alloc:6db7178
CB:6db7178
msg 71
Alloc:6dc75a8
CB:6dc75a8
msg 72
Alloc:6dd79d8
CB:6dd79d8
msg 73
Alloc:6de7e08
CB:6de7e08
msg 74
Alloc:6df8238
CB:6df8238
msg 75
Alloc:6e08668
CB:6e08668
msg 76
Alloc:6e18a98
CB:6e18a98
msg 77
Alloc:6e28ec8
CB:6e28ec8
msg 78
Alloc:6e392f8
CB:6e392f8
msg 79
Alloc:6e49728
CB:6e49728
msg 80
Alloc:6e59b58
CB:6e59b58
msg 81
Alloc:6e69f88
CB:6e69f88
msg 82
Alloc:6e7a3b8
CB:6e7a3b8
msg 83
Alloc:6e8a7e8
CB:6e8a7e8
msg 84
Alloc:6e9ac18
CB:6e9ac18
msg 85
Alloc:6eab048
CB:6eab048
msg 86
Alloc:6ebb478
CB:6ebb478
msg 87
Alloc:6ecb8a8
CB:6ecb8a8
msg 88
Alloc:6edbcd8
CB:6edbcd8
msg 89
Alloc:6eec108
CB:6eec108
msg 90
Alloc:6efc538
CB:6efc538
msg 91
Alloc:6f0c968
CB:6f0c968
msg 92
Alloc:6f1cd98
CB:6f1cd98
msg 93
Alloc:6f2d1c8
CB:6f2d1c8
msg 94
Alloc:6f3d5f8
CB:6f3d5f8
msg 95
Alloc:6f4da28
CB:6f4da28
msg 96
Alloc:6f5de58
CB:6f5de58
msg 97
Alloc:6f6e288
CB:6f6e288
msg 98
Alloc:6f7e6b8
CB:6f7e6b8
msg 99
Alloc:6f8eae8
CB:6f8eae8
msg 100
Alloc:6f9ef18
CB:6f9ef18
msg 101
Alloc:6faf348
CB:6faf348
msg 102
Alloc:6fbf778
CB:6fbf778
msg 103
Alloc:6fcfba8
CB:6fcfba8
msg 104
Alloc:6fdffd8
CB:6fdffd8
msg 105
Alloc:7000068
CB:7000068
msg 106
Alloc:7010498
CB:7010498
msg 107
Alloc:70208c8
CB:70208c8
msg 108
Alloc:7030cf8
CB:7030cf8
msg 109
Alloc:7041128
CB:7041128
msg 110
Alloc:7051558
CB:7051558
msg 111
Alloc:7061988
CB:7061988
msg 112
Alloc:7071db8
CB:7071db8
msg 113
Alloc:70821e8
CB:70821e8
msg 114
Alloc:7092618
CB:7092618
msg 115
Alloc:70a2a48
CB:70a2a48
msg 116
Alloc:70b2e78
CB:70b2e78
msg 117
Alloc:70c32a8
CB:70c32a8
msg 118
Alloc:70d36d8
CB:70d36d8
msg 119
Alloc:70e3b08
CB:70e3b08
msg 120
Alloc:70f3f38
CB:70f3f38
msg 121
Alloc:7104368
CB:7104368
msg 122
Alloc:7114798
CB:7114798
msg 123
Alloc:7124bc8
CB:7124bc8
msg 124
Alloc:7134ff8
CB:7134ff8
msg 125
Alloc:7145428
CB:7145428
msg 126
Alloc:7155858
CB:7155858
msg 127
Alloc:7165c88
CB:7165c88
msg 128
Alloc:71760b8
CB:71760b8
msg 129
Alloc:71864e8
CB:71864e8
msg 130
Alloc:7196918
CB:7196918
msg 131
Alloc:71a6d48
CB:71a6d48
msg 132
Alloc:71b7178
CB:71b7178
msg 133
Alloc:71c75a8
CB:71c75a8
msg 134
Alloc:71d79d8
CB:71d79d8
msg 135
Alloc:71e7e08
CB:71e7e08
msg 136
Alloc:71f8238
CB:71f8238
msg 137
Alloc:7208668
CB:7208668
msg 138
Alloc:7218a98
CB:7218a98
msg 139
Alloc:7228ec8
CB:7228ec8
msg 140
Alloc:72392f8
CB:72392f8
msg 141
Alloc:7249728
CB:7249728
msg 142
Alloc:7259b58
CB:7259b58
msg 143
Alloc:7269f88
CB:7269f88
msg 144
Alloc:727a3b8
CB:727a3b8
msg 145
Alloc:728a7e8
CB:728a7e8
msg 146
Alloc:729ac18
CB:729ac18
msg 147
Alloc:72ab048
CB:72ab048
msg 148
Alloc:72bb478
CB:72bb478
msg 149
Alloc:72cb8a8
CB:72cb8a8
msg 150
Alloc:72dbcd8
CB:72dbcd8
msg 151
Alloc:72ec108
CB:72ec108
msg 152
Alloc:72fc538
CB:72fc538
msg 153
Alloc:730c968
CB:730c968
msg 154
Alloc:731cd98
CB:731cd98
msg 155
Alloc:732d1c8
CB:732d1c8
msg 156
Alloc:733d5f8
CB:733d5f8
msg 157
Alloc:734da28
CB:734da28
msg 158
Alloc:735de58
CB:735de58
msg 159
Alloc:736e288
CB:736e288
msg 160
Alloc:737e6b8
CB:737e6b8
msg 161
Alloc:738eae8
CB:738eae8
msg 162
Alloc:739ef18
CB:739ef18
msg 163
Alloc:73af348
CB:73af348
msg 164
Alloc:73bf778
CB:73bf778
msg 165
Alloc:73cfba8
CB:73cfba8
msg 166
Alloc:73dffd8
CB:73dffd8
msg 167
Alloc:73f0408
CB:73f0408
msg 168
Alloc:7400838
CB:7400838
msg 169
Alloc:7410c68
CB:7410c68
msg 170
Alloc:7421098
CB:7421098
msg 171
Alloc:74314c8
CB:74314c8
msg 172
Alloc:74418f8
CB:74418f8
msg 173
Alloc:7451d28
CB:7451d28
msg 174
Alloc:7462158
CB:7462158
msg 175
Alloc:7472588
CB:7472588
msg 176
Alloc:74829b8
CB:74829b8
msg 177
Alloc:7492de8
CB:7492de8
msg 178
Alloc:74a3218
CB:74a3218
msg 179
Alloc:74b3648
CB:74b3648
msg 180
Alloc:74c3a78
CB:74c3a78
msg 181
Alloc:74d3ea8
CB:74d3ea8
msg 182
Alloc:74e42d8
CB:74e42d8
msg 183
Alloc:74f4708
CB:74f4708
msg 184
Alloc:7504b38
CB:7504b38
msg 185
Alloc:7514f68
CB:7514f68
msg 186
Alloc:7525398
CB:7525398
msg 187
Alloc:75357c8
CB:75357c8
msg 188
Alloc:7545bf8
CB:7545bf8
msg 189
Alloc:7556028
CB:7556028
msg 190
Alloc:7566458
CB:7566458
msg 191
Alloc:7576888
CB:7576888
msg 192
Alloc:7586cb8
CB:7586cb8
msg 193
Alloc:75970e8
CB:75970e8
msg 194
Alloc:75a7518
CB:75a7518
msg 195
Alloc:75b7948
CB:75b7948
msg 196
Alloc:75c7d78
CB:75c7d78
msg 197
Alloc:75d81a8
CB:75d81a8
msg 198
Alloc:75e85d8
CB:75e85d8
msg 199
Alloc:75f8a08
CB:75f8a08
msg 200
Alloc:7608e38
CB:7608e38
msg 201
Alloc:7619268
CB:7619268
msg 202
Alloc:7629698
CB:7629698
msg 203
Alloc:7639ac8
CB:7639ac8
msg 204
Alloc:7649ef8
CB:7649ef8
msg 205
Alloc:765a328
CB:765a328
msg 206
Alloc:766a758
CB:766a758
msg 207
Alloc:767ab88
CB:767ab88
msg 208
Alloc:768afb8
CB:768afb8
msg 209
Alloc:769b3e8
CB:769b3e8
msg 210
Alloc:76ab818
CB:76ab818
msg 211
Alloc:76bbc48
CB:76bbc48
msg 212
Alloc:76cc078
CB:76cc078
msg 213
Alloc:76dc4a8
CB:76dc4a8
msg 214
Alloc:76ec8d8
CB:76ec8d8
msg 215
Alloc:76fcd08
CB:76fcd08
msg 216
Alloc:770d138
CB:770d138
msg 217
Alloc:771d568
CB:771d568
msg 218
Alloc:772d998
CB:772d998
msg 219
Alloc:773ddc8
CB:773ddc8
msg 220
Alloc:774e1f8
CB:774e1f8
msg 221
Alloc:775e628
CB:775e628
msg 222
Alloc:776ea58
CB:776ea58
msg 223
Alloc:777ee88
CB:777ee88
msg 224
Alloc:778f2b8
CB:778f2b8
msg 225
Alloc:779f6e8
CB:779f6e8
msg 226
Alloc:77afb18
CB:77afb18
msg 227
Alloc:77bff48
CB:77bff48
msg 228
Alloc:77d0378
CB:77d0378
msg 229
Alloc:77e07a8
CB:77e07a8
msg 230
Alloc:7800068
CB:7800068
msg 231
Alloc:7810498
CB:7810498
msg 232
Alloc:78208c8
CB:78208c8
msg 233
Alloc:7830cf8
CB:7830cf8
msg 234
Alloc:7841128
CB:7841128
msg 235
Alloc:7851558
CB:7851558
msg 236
Alloc:7861988
CB:7861988
msg 237
Alloc:7871db8
CB:7871db8
msg 238
Alloc:78821e8
CB:78821e8
msg 239
Alloc:7892618
CB:7892618
msg 240
Alloc:78a2a48
CB:78a2a48
msg 241
Alloc:78b2e78
CB:78b2e78
msg 242
Alloc:78c32a8
CB:78c32a8
msg 243
Alloc:78d36d8
CB:78d36d8
msg 244
Alloc:78e3b08
CB:78e3b08
msg 245
Alloc:78f3f38
CB:78f3f38
msg 246
Alloc:7904368
CB:7904368
msg 247
Alloc:7914798
CB:7914798
msg 248
Alloc:7924bc8
CB:7924bc8
msg 249
Alloc:7934ff8
CB:7934ff8
msg 250
Alloc:7945428
CB:7945428
msg 251
Alloc:7955858
CB:7955858
msg 252
Alloc:7965c88
CB:7965c88
msg 253
Alloc:79760b8
CB:79760b8
msg 254
Alloc:79864e8
CB:79864e8
msg 255
Alloc:7996918
CB:7996918
msg 256
Alloc:79a6d48
CB:79a6d48
msg 257
Alloc:79b7178
CB:79b7178
msg 258
Alloc:79c75a8
CB:79c75a8
msg 259
Alloc:79d79d8
CB:79d79d8
msg 260
Alloc:79e7e08
CB:79e7e08
msg 261
Alloc:79f8238
CB:79f8238
msg 262
Alloc:7a08668
CB:7a08668
msg 263
Alloc:7a18a98
CB:7a18a98
msg 264
Alloc:7a28ec8
CB:7a28ec8
msg 265
Alloc:7a392f8
CB:7a392f8
msg 266
Alloc:7a49728
CB:7a49728
msg 267
Alloc:7a59b58
CB:7a59b58
msg 268
Alloc:7a69f88
CB:7a69f88
msg 269
Alloc:7a7a3b8
CB:7a7a3b8
msg 270
Alloc:7a8a7e8
CB:7a8a7e8
msg 271
Alloc:7a9ac18
CB:7a9ac18
msg 272
Alloc:7aab048
CB:7aab048
msg 273
Alloc:7abb478
CB:7abb478
msg 274
Alloc:7acb8a8
CB:7acb8a8
msg 275
Alloc:7adbcd8
CB:7adbcd8
msg 276
Alloc:7aec108
CB:7aec108
msg 277
Alloc:7afc538
CB:7afc538
msg 278
Alloc:7b0c968
CB:7b0c968
msg 279
Alloc:7b1cd98
CB:7b1cd98
msg 280
Alloc:7b2d1c8
CB:7b2d1c8
msg 281
Alloc:7b3d5f8
CB:7b3d5f8
msg 282
Alloc:7b4da28
CB:7b4da28
msg 283
Alloc:7b5de58
CB:7b5de58
msg 284
Alloc:7b6e288
CB:7b6e288
msg 285
Alloc:7b7e6b8
CB:7b7e6b8
msg 286
Alloc:7b8eae8
CB:7b8eae8
msg 287
Alloc:7b9ef18
CB:7b9ef18
msg 288
Alloc:7baf348
CB:7baf348
msg 289
Alloc:7bbf778
CB:7bbf778
msg 290
Alloc:7bcfba8
CB:7bcfba8
msg 291
Alloc:7bdffd8
CB:7bdffd8
msg 292
Alloc:7bf0408
CB:7bf0408
msg 293
Alloc:7c00838
CB:7c00838
msg 294
Alloc:7c10c68
CB:7c10c68
msg 295
Alloc:7c21098
CB:7c21098
msg 296
Alloc:7c314c8
CB:7c314c8
msg 297
Alloc:7c418f8
CB:7c418f8
msg 298
Alloc:7c51d28
CB:7c51d28
msg 299
Alloc:7c62158
CB:7c62158
msg 300
Alloc:7c72588
CB:7c72588
msg 301
Alloc:7c829b8
CB:7c829b8
msg 302
Alloc:7c92de8
CB:7c92de8
msg 303
Alloc:7ca3218
CB:7ca3218
msg 304
Alloc:7cb3648
CB:7cb3648
msg 305
Alloc:7cc3a78
CB:7cc3a78
msg 306
Alloc:7cd3ea8
CB:7cd3ea8
msg 307
Alloc:7ce42d8
CB:7ce42d8
msg 308
Alloc:7cf4708
CB:7cf4708
msg 309
Alloc:7d04b38
CB:7d04b38
msg 310
Alloc:7d14f68
CB:7d14f68
msg 311
Alloc:7d25398
CB:7d25398
msg 312
Alloc:7d357c8
CB:7d357c8
msg 313
Alloc:7d45bf8
CB:7d45bf8
msg 314
Alloc:7d56028
CB:7d56028
msg 315
Alloc:7d66458
CB:7d66458
msg 316
Alloc:7d76888
CB:7d76888
msg 317
Alloc:7d86cb8
CB:7d86cb8
msg 318
Alloc:7d970e8
CB:7d970e8
msg 319
Alloc:7da7518
CB:7da7518
msg 320
Alloc:7db7948
CB:7db7948
msg 321
Alloc:7dc7d78
CB:7dc7d78
msg 322
Alloc:7dd81a8
CB:7dd81a8
msg 323
Alloc:7de85d8
CB:7de85d8
msg 324
Alloc:7df8a08
CB:7df8a08
msg 325
Alloc:7e08e38
CB:7e08e38
msg 326
Alloc:7e19268
CB:7e19268
msg 327
Alloc:7e29698
CB:7e29698
msg 328
Alloc:7e39ac8
CB:7e39ac8
msg 329
Alloc:7e49ef8
CB:7e49ef8
msg 330
Alloc:7e5a328
CB:7e5a328
msg 331
Alloc:7e6a758
CB:7e6a758
msg 332
Alloc:7e7ab88
CB:7e7ab88
msg 333
Alloc:7e8afb8
CB:7e8afb8
msg 334
Alloc:7e9b3e8
CB:7e9b3e8
msg 335
Alloc:7eab818
CB:7eab818
msg 336
Alloc:7ebbc48
CB:7ebbc48
msg 337
Alloc:7ecc078
CB:7ecc078
msg 338
Alloc:7edc4a8
CB:7edc4a8
msg 339
Alloc:7eec8d8
CB:7eec8d8
msg 340
Alloc:7efcd08
CB:7efcd08
msg 341
Alloc:7f0d138
CB:7f0d138
msg 342
Alloc:7f1d568
CB:7f1d568
msg 343
Alloc:7f2d998
CB:7f2d998
msg 344
Alloc:7f3ddc8
CB:7f3ddc8
msg 345
Alloc:7f4e1f8
CB:7f4e1f8
msg 346
Alloc:7f5e628
CB:7f5e628
msg 347
Alloc:7f6ea58
CB:7f6ea58
msg 348
Alloc:7f7ee88
CB:7f7ee88
msg 349
Alloc:7f8f2b8
CB:7f8f2b8
msg 350
Alloc:7f9f6e8
CB:7f9f6e8
msg 351
Alloc:7fafb18
CB:7fafb18
msg 352
Alloc:7fc3f50
CB:7fc3f50
msg 353
Alloc:7fd4380
CB:7fd4380
msg 354
Alloc:7fe47b0
CB:7fe47b0
msg 355
Alloc:7ff4be0
CB:7ff4be0
msg 356
Alloc:8005010
CB:8005010
msg 357
Alloc:8015440
CB:8015440
msg 358
Alloc:8025870
CB:8025870
msg 359
Alloc:8035ca0
CB:8035ca0
msg 360
Alloc:80460d0
CB:80460d0
msg 361
Alloc:8056500
CB:8056500
msg 362
Alloc:8066930
CB:8066930
msg 363
Alloc:8076d60
CB:8076d60
msg 364
Alloc:8087190
CB:8087190
msg 365
Alloc:80975c0
CB:80975c0
msg 366
Alloc:80a79f0
CB:80a79f0
msg 367
Alloc:80b7e20
CB:80b7e20
msg 368
Alloc:80c8250
CB:80c8250
msg 369
Alloc:80d8680
CB:80d8680
msg 370
Alloc:80e8ab0
CB:80e8ab0
msg 371
Alloc:80f8ee0
CB:80f8ee0
msg 372
Alloc:8109310
CB:8109310
msg 373
Alloc:8119740
CB:8119740
msg 374
Alloc:8129b70
CB:8129b70
msg 375
Alloc:8139fa0
CB:8139fa0
msg 376
Alloc:814a3d0
CB:814a3d0
msg 377
Alloc:815a800
CB:815a800
msg 378
Alloc:816ac30
CB:816ac30
msg 379
Alloc:817b060
CB:817b060
msg 380
Alloc:818b490
CB:818b490
msg 381
Alloc:819b8c0
CB:819b8c0
msg 382
Alloc:81abcf0
CB:81abcf0
msg 383
Alloc:81bc120
CB:81bc120
msg 384
Alloc:81cc550
CB:81cc550
msg 385
Alloc:81dc980
CB:81dc980
msg 386
Alloc:81ecdb0
CB:81ecdb0
msg 387
Alloc:81fd1e0
CB:81fd1e0
msg 388
Alloc:820d610
CB:820d610
msg 389
Alloc:821da40
CB:821da40
msg 390
Alloc:822de70
CB:822de70
msg 391
Alloc:823e2a0
CB:823e2a0
msg 392
Alloc:824e6d0
CB:824e6d0
msg 393
Alloc:825eb00
CB:825eb00
msg 394
Alloc:826ef30
CB:826ef30
msg 395
Alloc:827f360
CB:827f360
msg 396
Alloc:828f790
CB:828f790
msg 397
Alloc:829fbc0
CB:829fbc0
msg 398
Alloc:82afff0
CB:82afff0
msg 399
Alloc:82c0420
CB:82c0420
msg 400
Alloc:82d2858
CB:82d2858
msg 401
Alloc:82e2c88
CB:82e2c88
msg 402
Alloc:82f30b8
CB:82f30b8
msg 403
Alloc:83034e8
CB:83034e8
msg 404
Alloc:8313918
CB:8313918
msg 405
Alloc:8323d48
CB:8323d48
msg 406
Alloc:8334178
CB:8334178
msg 407
Alloc:83445a8
CB:83445a8
msg 408
Alloc:83549d8
CB:83549d8
msg 409
Alloc:8364e08
CB:8364e08
msg 410
Alloc:8375238
CB:8375238
msg 411
Alloc:8385668
CB:8385668
msg 412
Alloc:8395a98
CB:8395a98
msg 413
Alloc:83a5ec8
CB:83a5ec8
msg 414
Alloc:83b62f8
CB:83b62f8
msg 415
Alloc:83c6728
CB:83c6728
msg 416
Alloc:83d7b78
CB:83d7b78
msg 417
Alloc:83e7fa8
CB:83e7fa8
msg 418
Alloc:83f83d8
CB:83f83d8
msg 419
Alloc:8408808
CB:8408808
msg 420
Alloc:8418c38
CB:8418c38
msg 421
Alloc:8429068
CB:8429068
msg 422
Alloc:8439498
CB:8439498
msg 423
Alloc:84498c8
CB:84498c8
msg 424
Alloc:8459cf8
CB:8459cf8
msg 425
Alloc:846a128
CB:846a128
msg 426
Alloc:847a558
CB:847a558
msg 427
Alloc:848a988
CB:848a988
msg 428
Alloc:849adb8
CB:849adb8
msg 429
Alloc:84ab1e8
CB:84ab1e8
msg 430
Alloc:84bb618
CB:84bb618
msg 431
Alloc:84cba48
CB:84cba48
msg 432
Alloc:84dbe78
CB:84dbe78
msg 433
Alloc:84ec2a8
CB:84ec2a8
msg 434
Alloc:84fc6d8
CB:84fc6d8
msg 435
Alloc:850cb08
CB:850cb08
msg 436
Alloc:851cf38
CB:851cf38
msg 437
Alloc:852d368
CB:852d368
msg 438
Alloc:853d798
CB:853d798
msg 439
Alloc:854dbc8
CB:854dbc8
msg 440
Alloc:855dff8
CB:855dff8
msg 441
Alloc:856e428
CB:856e428
msg 442
Alloc:857e858
CB:857e858
msg 443
Alloc:858ec88
CB:858ec88
msg 444
Alloc:859f0b8
CB:859f0b8
msg 445
Alloc:85af4e8
CB:85af4e8
msg 446
Alloc:85bf918
CB:85bf918
msg 447
Alloc:85cfd48
CB:85cfd48
msg 448
Alloc:85e0178
CB:85e0178
msg 449
Alloc:85f05a8
CB:85f05a8
msg 450
Alloc:86009d8
CB:86009d8
msg 451
Alloc:8610e08
CB:8610e08
msg 452
Alloc:8621238
CB:8621238
msg 453
Alloc:8631668
CB:8631668
msg 454
Alloc:8641a98
CB:8641a98
msg 455
Alloc:8651ec8
CB:8651ec8
msg 456
Alloc:86622f8
CB:86622f8
msg 457
Alloc:8672728
CB:8672728
msg 458
Alloc:8682b58
CB:8682b58
msg 459
Alloc:8692f88
CB:8692f88
msg 460
Alloc:86a33b8
CB:86a33b8
msg 461
Alloc:86b37e8
CB:86b37e8
msg 462
Alloc:86c3c18
CB:86c3c18
msg 463
Alloc:86d4048
CB:86d4048
msg 464
Alloc:86e4478
CB:86e4478
msg 465
Alloc:86f88b0
CB:86f88b0
msg 466
Alloc:8708ce0
CB:8708ce0
msg 467
Alloc:8719110
CB:8719110
msg 468
Alloc:8729540
CB:8729540
msg 469
Alloc:8739970
CB:8739970
msg 470
Alloc:8749da0
CB:8749da0
msg 471
Alloc:875a1d0
CB:875a1d0
msg 472
Alloc:876a600
CB:876a600
msg 473
Alloc:877aa30
CB:877aa30
msg 474
Alloc:878ae60
CB:878ae60
msg 475
Alloc:879b290
CB:879b290
msg 476
Alloc:87ab6c0
CB:87ab6c0
msg 477
Alloc:87bbaf0
CB:87bbaf0
msg 478
Alloc:87d0068
CB:87d0068
msg 479
Alloc:87e0498
CB:87e0498
msg 480
Alloc:87f08c8
CB:87f08c8
msg 481
Alloc:8800cf8
CB:8800cf8
msg 482
Alloc:8811128
CB:8811128
msg 483
Alloc:8821558
CB:8821558
msg 484
Alloc:8831988
CB:8831988
msg 485
Alloc:8841db8
CB:8841db8
msg 486
Alloc:88521e8
CB:88521e8
msg 487
Alloc:8862618
CB:8862618
msg 488
Alloc:8872a48
CB:8872a48
msg 489
Alloc:8882e78
CB:8882e78
msg 490
Alloc:88932a8
CB:88932a8
msg 491
Alloc:88a36d8
CB:88a36d8
msg 492
Alloc:88b3b08
CB:88b3b08
msg 493
Alloc:88c3f38
CB:88c3f38
msg 494
Alloc:88d4368
CB:88d4368
msg 495
Alloc:88e4798
CB:88e4798
msg 496
Alloc:88f4bc8
CB:88f4bc8
msg 497
Alloc:8904ff8
CB:8904ff8
msg 498
Alloc:8915428
CB:8915428
msg 499
Alloc:8925858
CB:8925858
msg 500
Alloc:8935c88
CB:8935c88
msg 501
Alloc:89460b8
CB:89460b8
msg 502
Alloc:89564e8
CB:89564e8
msg 503
Alloc:8968948
CB:8968948
msg 504
Alloc:8978d78
CB:8978d78
msg 505
Alloc:89891a8
CB:89891a8
msg 506
Alloc:89995d8
CB:89995d8
msg 507
Alloc:89a9a08
CB:89a9a08
msg 508
Alloc:89b9e38
CB:89b9e38
msg 509
Alloc:89ca268
CB:89ca268
msg 510
Alloc:89da698
CB:89da698
msg 511
Alloc:89eaac8
CB:89eaac8
msg 512
Alloc:89faef8
CB:89faef8
msg 513
Alloc:8a0b328
CB:8a0b328
msg 514
Alloc:8a1b758
CB:8a1b758
msg 515
Alloc:8a2bb88
CB:8a2bb88
msg 516
Alloc:8a3bfb8
CB:8a3bfb8
msg 517
Alloc:8a4c3e8
CB:8a4c3e8
msg 518
Alloc:8a5c818
CB:8a5c818
msg 519
Alloc:8a6cc48
CB:8a6cc48
msg 520
Alloc:8a7d078
CB:8a7d078
msg 521
Alloc:8a8d4a8
CB:8a8d4a8
msg 522
Free:68180a8
Free:68284d8
Free:6838908
Free:6848d38
Free:685f008
Free:686f438
Free:687f868
Free:688fc98
Free:68a00c8
Free:68b04f8
Free:68c0928
Free:68d0d58
Free:68e1188
Free:6a00068
Free:6a10498
Free:6a208c8
Free:6a30cf8
Free:6a41128
Free:6a51558
Free:6a61988
Free:6a71db8
Free:6a821e8
Free:6a92618
Free:6aa2a48
Free:6ab2e78
Free:6ac32a8
Free:6ad36d8
Free:6ae3b08
Free:6af3f38
Free:6b04368
Free:6b14798
Free:6b24bc8
Free:6b34ff8
Free:6b45428
Free:6b55858
Free:6b65c88
Free:6b760b8
Free:6b864e8
Free:6b96918
Free:6ba6d48
Free:6bb7178
Free:6bc75a8
Free:6bd79d8
Free:6be7e08
Free:6c00068
Free:6c10498
Free:6c208c8
Free:6c30cf8
Free:6c41128
Free:6c51558
Free:6c61988
Free:6c71db8
Free:6c821e8
Free:6c92618
Free:6ca2a48
Free:6cb2e78
Free:6cc32a8
Free:6cd36d8
Free:6ce3b08
Free:6cf3f38
Free:6d04368
Free:6d14798
Free:6d24bc8
Free:6d34ff8
Free:6d45428
Free:6d55858
Free:6d65c88
Free:6d760b8
Free:6d864e8
Free:6d96918
Free:6da6d48
Free:6db7178
Free:6dc75a8
Free:6dd79d8
Free:6de7e08
Free:6df8238
Free:6e08668
Free:6e18a98
Free:6e28ec8
Free:6e392f8
Free:6e49728
Free:6e59b58
Free:6e69f88
Free:6e7a3b8
Free:6e8a7e8
Free:6e9ac18
Free:6eab048
Free:6ebb478
Free:6ecb8a8
Free:6edbcd8
Free:6eec108
Free:6efc538
Free:6f0c968
Free:6f1cd98
Free:6f2d1c8
Free:6f3d5f8
Free:6f4da28
Free:6f5de58
Free:6f6e288
Free:6f7e6b8
Free:6f8eae8
Free:6f9ef18
Free:6faf348
Free:6fbf778
Free:6fcfba8
Alloc:68de008
Alloc:68180a8
Alloc:68284d8
Alloc:6838908
Alloc:6848d38
Alloc:6a00068
Alloc:6a10498
Alloc:6a208c8
Alloc:6a30cf8
Alloc:6a41128
Alloc:6a51558
Alloc:6a61988
Alloc:685f008
Alloc:686f438
Alloc:687f868
Alloc:688fc98
Alloc:68a00c8
Alloc:68b04f8
Alloc:68c0928
Alloc:6a80008
Alloc:6a90438
Alloc:6aa0868
Alloc:6ab0c98
Alloc:6ac10c8
Alloc:6ad14f8
Alloc:6ae1928
Alloc:6af1d58
Alloc:6b02188
Alloc:6b125b8
Alloc:6b229e8
Alloc:6b32e18
Alloc:6b43248
Alloc:6b53678
Alloc:6b63aa8
Alloc:6b73ed8
Alloc:6b84308
Alloc:6b94738
Alloc:6ba4b68
Alloc:6bb4f98
Alloc:6bc53c8
Alloc:6bd57f8
Alloc:6be5c28
Alloc:6c01008
Alloc:6c11438
Alloc:6c21868
Alloc:6c31c98
Alloc:6c420c8
Alloc:6c524f8
Alloc:6c62928
Alloc:6c72d58
Alloc:6c83188
Alloc:6c935b8
Alloc:6ca39e8
Alloc:6cb3e18
Alloc:6cc4248
Alloc:6cd4678
Alloc:6ce4aa8
Alloc:6cf4ed8
Alloc:6d05308
Alloc:6d15738
Alloc:6d25b68
Alloc:6d35f98
Alloc:6d463c8
Alloc:6d567f8
Alloc:6d66c28
Alloc:6d77058
Alloc:6d87488
Alloc:6d978b8
Alloc:6da7ce8
Alloc:6db8118
Alloc:6dc8548
Alloc:6dd8978
Alloc:6de8da8
Alloc:6df91d8
Alloc:6e09608
Alloc:6e19a38
Alloc:6e29e68
Alloc:6e3a298
Alloc:6e4a6c8
Alloc:6e5aaf8
Alloc:6e6af28
Alloc:6e7b358
Alloc:6e8b788
Alloc:6e9bbb8
Alloc:6eabfe8
Alloc:6ebc418
Alloc:6ecc848
Alloc:6edcc78
Alloc:6eed0a8
Alloc:6efd4d8
Alloc:6f0d908
Alloc:6f1dd38
Alloc:6f2e168
Alloc:6f3e598
Alloc:6f4e9c8
Alloc:6f5edf8
Alloc:6f6f228
Alloc:6f7f658
Alloc:6f8fa88
Alloc:6f9feb8
Alloc:6fb02e8
Alloc:6fc0718
Alloc:8a9d8d8
Alloc:8aadd08
Alloc:8abe138
Alloc:8ace568
Alloc:8ade998
Alloc:8aeedc8
Alloc:8aff1f8
Alloc:8b0f628
Alloc:8b1fa58
Alloc:8b2fe88
Alloc:8b402b8
Alloc:8b526f0
Alloc:8b62b20
Alloc:8b72f50
Alloc:8b83380
Alloc:8b937b0
Alloc:8ba3be0
Alloc:8bb4010
Alloc:8bc4440
Alloc:8bd4870
Alloc:8be4ca0
Alloc:8bf50d0
Alloc:8c05500
Alloc:8c15930
Alloc:8c25d60
Alloc:8c36190
Alloc:8c465c0
Alloc:8c569f0
Alloc:8c66e20
Alloc:8c77250
Alloc:8c87680
Alloc:8c97ab0
Alloc:8ca7ee0
Alloc:8cb8310
Alloc:8cc8740
Alloc:8cd8b70
Alloc:8ce8fa0
Alloc:8cf93d0
Alloc:8d09800
Alloc:8d19c30
Alloc:8d2a060
Alloc:8d3a490
Alloc:8d4a8c0
Alloc:8d5acf0
Alloc:8d6b120
Alloc:8d7b550
Alloc:8d8b980
Alloc:8d9bdb0
Alloc:8dac1e0
Alloc:8dbc610
Alloc:8dcca40
Alloc:8ddce70
Alloc:8ded2a0
Alloc:8dfd6d0
Alloc:8e0db00
Alloc:8e1df30
Alloc:8e2e360
Alloc:8e3e790
Alloc:8e4ebc0
Alloc:8e5eff0
Alloc:8e6f420
Alloc:8e7f850
Alloc:8e8fc80
Alloc:8ea00b0
Alloc:8eb04e0
Alloc:8ec0910
Alloc:8ed0d40
Alloc:8ee1170
Alloc:8ef15a0
Alloc:8f019d0
Alloc:8f11e00
Alloc:8f22230
Alloc:8f32660
Alloc:8f42a90
Alloc:8f52ec0
Alloc:8f632f0
Alloc:8f73720
Alloc:8f83b50
Alloc:8f93f80
Alloc:8fa43b0
Alloc:8fb47e0
Alloc:8fc4c10
Alloc:8fd5040
Alloc:8fe5470
Alloc:8ff58a0
Alloc:9005cd0
Alloc:9016100
Alloc:9026530
Alloc:9036960
Alloc:9046d90
Alloc:90571c0
Alloc:90675f0
Alloc:9077a20
Alloc:9087e50
Alloc:9098280
Alloc:90a86b0
Alloc:90b8ae0
Alloc:90c8f10
Alloc:90d9340
Alloc:90e9770
Alloc:90f9ba0
Alloc:9109fd0
Alloc:911a400
Alloc:912c838
Alloc:913cc68
Alloc:914d098
Alloc:915d4c8
Alloc:916d8f8
Alloc:917dd28
Alloc:918e158
Alloc:919e588
Alloc:91ae9b8
Alloc:91bede8
Alloc:91cf218
Alloc:91df648
Alloc:91efa78
Alloc:91ffea8
Alloc:92102d8
Alloc:9220708
Alloc:9230b38
Alloc:9240f68
Alloc:9251398
Alloc:92617c8
Alloc:9271bf8
Alloc:9282028
Alloc:9292458
Alloc:92a2888
Alloc:92b2cb8
Alloc:92c30e8
Alloc:92d3518
Alloc:92e3948
Alloc:92f3d78
Alloc:93041a8
Alloc:93145d8
Alloc:9324a08
Alloc:9334e38
Alloc:9345268
Alloc:9355698
Alloc:9365ac8
Alloc:9375ef8
Alloc:9386328
Alloc:9396758
Alloc:93a6b88
Alloc:93b6fb8
Alloc:93c73e8
Alloc:93d7818
Alloc:93e7c48
Alloc:93f8078
Alloc:94084a8
Alloc:94188d8
Alloc:9428d08
Alloc:9439138
Alloc:9449568
Alloc:9459998
Alloc:9469dc8
Alloc:947a1f8
Alloc:948a628
Alloc:949aa58
Alloc:94aae88
Alloc:94bb2b8
Alloc:94cb6e8
Alloc:94dbb18
Alloc:94ebf48
Alloc:94fc378
Alloc:950c7a8
Alloc:951cbd8
Alloc:952d008
Alloc:953d438
Alloc:954d868
Alloc:955dc98
Alloc:956e0c8
Alloc:957e4f8
Alloc:958e928
Alloc:959ed58
Alloc:95af188
Alloc:95bf5b8
Alloc:95cf9e8
Alloc:95dfe18
Alloc:95f0248
Alloc:9600678
Alloc:9610aa8
Alloc:9620ed8
Alloc:9631308
Alloc:9641738
Alloc:9651b68
Alloc:9661f98
Alloc:96723c8
Alloc:96827f8
Alloc:9692c28
Alloc:96a3058
Alloc:96b3488
Alloc:96c38b8
Alloc:96d3ce8
Alloc:96e4118
Alloc:96f4548
Alloc:9704978
Alloc:9714da8
Alloc:97251d8
Alloc:9735608
Alloc:9745a38
Alloc:9755e68
Alloc:9766298
Alloc:97766c8
Alloc:9786af8
Alloc:ba10068
Alloc:ba20498
Alloc:ba308c8
Alloc:ba40cf8
Alloc:ba51128
Alloc:ba61558
Alloc:ba71988
Alloc:ba81db8
Alloc:ba921e8
Alloc:baa2618
Alloc:bab2a48
Alloc:bac2e78
Alloc:bad32a8
Alloc:bae36d8
Alloc:baf3b08
Alloc:bb03f38
Alloc:bb14368
Alloc:bb24798
Alloc:bb34bc8
Alloc:bb44ff8
Alloc:bb55428
Alloc:bb65858
Alloc:bb75c88
Alloc:bb860b8
Alloc:bb964e8
Alloc:bba6918
Alloc:bbb6d48
Alloc:bbc7178
Alloc:bbd75a8
Alloc:bbe79d8
Alloc:bbf7e08
Alloc:bc08238
Alloc:bc18668
Alloc:bc28a98
Alloc:bc38ec8
Alloc:bc492f8
Alloc:bc59728
Alloc:bc69b58
Alloc:bc79f88
Alloc:bc8a3b8
Alloc:bc9a7e8
Alloc:bcaac18
Alloc:bcbb048
Alloc:bccb478
Alloc:bcdb8a8
Alloc:bcebcd8
Alloc:bcfc108
Alloc:bd0c538
Alloc:bd1c968
Alloc:bd2cd98
Alloc:bd3d1c8
Alloc:bd4d5f8
Alloc:bd5da28
Alloc:bd6de58
Alloc:bd7e288
Alloc:bd8e6b8
Alloc:bd9eae8
Alloc:bdaef18
Alloc:bdbf348
Alloc:bdcf778
Alloc:bddfba8
Alloc:bdeffd8
Alloc:be00408
Alloc:be10838
Alloc:be20c68
Alloc:be31098
Alloc:be414c8
Alloc:be518f8
Alloc:be61d28
Alloc:be72158
Alloc:be82588
Alloc:be929b8
Alloc:bea2de8
Alloc:beb3218
Alloc:bec3648
Alloc:bed3a78
Alloc:bee3ea8
Alloc:bef42d8
Alloc:bf04708
Alloc:bf14b38
Alloc:bf24f68
Alloc:bf35398
Alloc:bf457c8
Alloc:bf55bf8
Alloc:bf66028
Alloc:bf76458
Alloc:bf86888
Alloc:bf96cb8
Alloc:bfa70e8
Alloc:bfb7518
Alloc:bfc7948
Alloc:bfd7d78
Alloc:bfe81a8
Alloc:bff85d8
Alloc:c008a08
Alloc:c018e38
Alloc:c029268
Alloc:c039698
Alloc:c049ac8
Alloc:c059ef8
Alloc:c06a328
Alloc:c07a758
Alloc:c08ab88
Alloc:c09afb8
Alloc:c0ab3e8
Alloc:c0bb818
Alloc:c0cbc48
Alloc:c0dc078
Alloc:c0ec4a8
Alloc:c0fc8d8
Alloc:c10cd08
Alloc:c11d138
Alloc:c12d568
Alloc:c13d998
Alloc:c14ddc8
Alloc:c15e1f8
Alloc:c16e628
Alloc:c17ea58
Alloc:c18ee88
Alloc:c19f2b8
Alloc:c1af6e8
Alloc:c1bfb18
Alloc:c1cff48
Alloc:c1e0378
Alloc:c1f07a8
Alloc:c200bd8
Alloc:c211008
Alloc:c221438
Alloc:c231868
Alloc:c241c98
Alloc:c2520c8
Alloc:c2624f8
Alloc:c272928
Alloc:c282d58
Alloc:c293188
Alloc:c2a35b8
Alloc:c2b39e8
Alloc:c2c3e18
Alloc:c2d4248
Alloc:c2e4678
Alloc:c2f4aa8
Alloc:c304ed8
Alloc:c315308
Alloc:c325738
Alloc:c335b68
Alloc:c345f98
Alloc:c3563c8
Alloc:c3667f8
Alloc:c376c28
Alloc:c387058
Alloc:c397488
Alloc:c3a78b8
Alloc:c3b7ce8
Alloc:c3c8118
Alloc:c3d8548
Alloc:c3e8978
Alloc:c3f8da8
Alloc:c4091d8
Alloc:c419608
Alloc:c429a38

@blindson76 blindson76 changed the title Thread-Safe Function Thread-Safe Function wtih ArrayBuffer Nov 8, 2021
@mhdawson
Copy link
Member

@mhdawson mhdawson commented Nov 12, 2021

We discussed in the Node-api team meeting today and the first thought is that it might be running into a Out of Memory, allocating in a tight loop may mean that the GC cannot keep up. Have you tried using a longer time in

std::this_thread::sleep_for(std::chrono::milliseconds(10)); ?

Is the code in a repo or somewhere we could run?

@blindson76
Copy link
Author

@blindson76 blindson76 commented Nov 12, 2021

We discussed in the Node-api team meeting today and the first thought is that it might be running into a Out of Memory, allocating in a tight loop may mean that the GC cannot keep up. Have you tried using a longer time in

std::this_thread::sleep_for(std::chrono::milliseconds(10)); ?

Is the code in a repo or somewhere we could run?

Thank you for your attention.
I tried it with different delays and buffer length, but it didn't make any difference. I'v put the sample code to this repo.

@gabrielschulhof
Copy link
Contributor

@gabrielschulhof gabrielschulhof commented Dec 17, 2021

If the thread keeps working after the Node.js instance terminates, you might be able to use a cleanup hook to abort the thread-safe function during Node.js shutdown. You're secondary thread will then handle the napi_closing return status to exit the thread.

@deepakrkris
Copy link

@deepakrkris deepakrkris commented Dec 22, 2021

@blindson76 I am triaging this issue,
trying to run this in my mac (by changing the build settings) and getting the following error

[ 33%] Building CXX object CMakeFiles/wrapped.dir/src/Wrapped.cc.o
In file included from /Users/t_rajad/projects/AA-NAPI/triage/addonapi/issue#1102/for-test/src/Wrapped.cc:5:
/Users/t_rajad/projects/AA-NAPI/triage/addonapi/issue#1102/for-test/src/Wrapped.h:36:17: error: field has incomplete type 'std::thread'
    std::thread nativeThread;
                ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk/usr/include/c++/v1/__threading_support:729:24: note: forward declaration of 'std::thread'
class _LIBCPP_TYPE_VIS thread;
                       ^
/Users/t_rajad/projects/AA-NAPI/triage/addonapi/issue#1102/for-test/src/Wrapped.cc:77:40: warning: format specifies type 'unsigned int' but the argument has type 'char *' [-Wformat]
            fprintf(stderr, "CB:%x\n", data);
                                ~~     ^~~~

/Users/t_rajad/projects/AA-NAPI/triage/addonapi/issue#1102/for-test/src/Wrapped.cc:94:31: error: no member named 'sleep_for' in namespace 'std::this_thread'
            std::this_thread::sleep_for(std::chrono::milliseconds(delay));
            ~~~~~~~~~~~~~~~~~~^
/Users/t_rajad/projects/AA-NAPI/triage/addonapi/issue#1102/for-test/src/Wrapped.cc:73:20: error: 'std::thread' is an incomplete type
    nativeThread = std::thread([this]{

@deepakrkris
Copy link

@deepakrkris deepakrkris commented Dec 31, 2021

@blindson76 I created a simplified version based on https://github.com/nodejs/node-addon-api/blob/main/doc/threadsafe_function.md , and this seems to work fine for me,
both the js and worker threads are active

#include <chrono>
#include <thread>
#include <napi.h>
#include <queue>

using namespace Napi;

std::thread nativeThread;
ThreadSafeFunction tsfn;

Value Start( const CallbackInfo& info )
{
  Napi::Env env = info.Env();

  if ( info.Length() < 3 )
  {
    throw TypeError::New( env, "Expected two arguments" );
  }
  else if ( !info[0].IsFunction() )
  {
    throw TypeError::New( env, "Expected first arg to be function" );
  }
  else if ( !info[1].IsNumber() )
  {
    throw TypeError::New( env, "Expected second arg to be number" );
  }
  else if ( !info[2].IsNumber() )
  {
    throw TypeError::New( env, "Expected third arg to be number" );
  }

  uint32_t length;
  uint32_t delay;

  length = info[1].As<Number>().Int32Value();
  delay = info[2].As<Number>().Int32Value();

  // Create a ThreadSafeFunction
  tsfn = ThreadSafeFunction::New(
      env,
      info[0].As<Function>(),  // JavaScript function called asynchronously
      "Resource Name",         // Name
      0,                       // Unlimited queue
      1,                       // Only one thread will use this initially
      []( Napi::Env ) {        // Finalizer used to clean threads up
        nativeThread.join();
      } );

  // Create a native thread
  nativeThread = std::thread( [length, delay] {
    auto callback = [length, delay](Napi::Env env, Napi::Function jsCallback, int *value ) {
      // Transform native data into JS data, passing it to the provided
      // `jsCallback` -- the TSFN's JavaScript function.

      fprintf(stderr, "CB:%x\n", value);
      ArrayBuffer buf = ArrayBuffer::New(env, value, length, [](void *hint, void* data){
                fprintf(stderr, "Free:%x\n", data);
                free(data);
      });
      jsCallback.Call( { buf } );
    };

    int i=0;
    int *test;
    while(true){
        int* buffer = new int( clock() );
        test = buffer;
        test[0] = i++;
        napi_status status = tsfn.BlockingCall( test, callback );
        std::this_thread::sleep_for(std::chrono::milliseconds(delay));
        if ( status != napi_ok )
        {
            // Handle error
            break;
        }
    }

    // Release the thread-safe function
    tsfn.Release();
  } );

  return Boolean::New(env, true);
}

Napi::Object Init( Napi::Env env, Object exports )
{
  exports.Set( "start", Function::New( env, Start ) );
  return exports;
}

NODE_API_MODULE(example, Init)

@deepakrkris
Copy link

@deepakrkris deepakrkris commented Dec 31, 2021

js test file for the above is

const clock = require('./build/Release/example.node');

clock.start(function (buffer) {
    let dataView = new DataView(buffer);
    console.log("JavaScript callback called with arguments", dataView.getUint8(0));
}, 1200, 1000);

@blindson76
Copy link
Author

@blindson76 blindson76 commented Jan 4, 2022

@blindson76 I created a simplified version based on https://github.com/nodejs/node-addon-api/blob/main/doc/threadsafe_function.md , and this seems to work fine for me, both the js and worker threads are active

#include <chrono>
#include <thread>
#include <napi.h>
#include <queue>

using namespace Napi;

std::thread nativeThread;
ThreadSafeFunction tsfn;

Value Start( const CallbackInfo& info )
{
  Napi::Env env = info.Env();

  if ( info.Length() < 3 )
  {
    throw TypeError::New( env, "Expected two arguments" );
  }
  else if ( !info[0].IsFunction() )
  {
    throw TypeError::New( env, "Expected first arg to be function" );
  }
  else if ( !info[1].IsNumber() )
  {
    throw TypeError::New( env, "Expected second arg to be number" );
  }
  else if ( !info[2].IsNumber() )
  {
    throw TypeError::New( env, "Expected third arg to be number" );
  }

  uint32_t length;
  uint32_t delay;

  length = info[1].As<Number>().Int32Value();
  delay = info[2].As<Number>().Int32Value();

  // Create a ThreadSafeFunction
  tsfn = ThreadSafeFunction::New(
      env,
      info[0].As<Function>(),  // JavaScript function called asynchronously
      "Resource Name",         // Name
      0,                       // Unlimited queue
      1,                       // Only one thread will use this initially
      []( Napi::Env ) {        // Finalizer used to clean threads up
        nativeThread.join();
      } );

  // Create a native thread
  nativeThread = std::thread( [length, delay] {
    auto callback = [length, delay](Napi::Env env, Napi::Function jsCallback, int *value ) {
      // Transform native data into JS data, passing it to the provided
      // `jsCallback` -- the TSFN's JavaScript function.

      fprintf(stderr, "CB:%x\n", value);
      ArrayBuffer buf = ArrayBuffer::New(env, value, length, [](void *hint, void* data){
                fprintf(stderr, "Free:%x\n", data);
                free(data);
      });
      jsCallback.Call( { buf } );
    };

    int i=0;
    int *test;
    while(true){
        int* buffer = new int( clock() );
        test = buffer;
        test[0] = i++;
        napi_status status = tsfn.BlockingCall( test, callback );
        std::this_thread::sleep_for(std::chrono::milliseconds(delay));
        if ( status != napi_ok )
        {
            // Handle error
            break;
        }
    }

    // Release the thread-safe function
    tsfn.Release();
  } );

  return Boolean::New(env, true);
}

Napi::Object Init( Napi::Env env, Object exports )
{
  exports.Set( "start", Function::New( env, Start ) );
  return exports;
}

NODE_API_MODULE(example, Init)

I think this is not same case with mine. In my version, thread-safe function used in a "Wrapped Object" instance method. In a standalone function everything works fine as a remember.

@github-actions
Copy link

@github-actions github-actions bot commented Apr 5, 2022

This issue is stale because it has been open many days with no activity. It will be closed soon unless the stale label is removed or a comment is made.

@github-actions github-actions bot added the stale label Apr 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants
X Tutup