| 48 | } |
| 49 | |
| 50 | static Value TestWithTSFN(const CallbackInfo& info) { |
| 51 | int threadCount = info[0].As<Number>().Int32Value(); |
| 52 | Function cb = info[1].As<Function>(); |
| 53 | |
| 54 | // We pass the test data to the Finalizer for cleanup. The finalizer is |
| 55 | // responsible for deleting this data as well. |
| 56 | TestData* testData = new TestData(Promise::Deferred::New(info.Env())); |
| 57 | |
| 58 | ThreadSafeFunction tsfn = ThreadSafeFunction::New( |
| 59 | info.Env(), |
| 60 | cb, |
| 61 | "Test", |
| 62 | 0, |
| 63 | threadCount, |
| 64 | std::function<decltype(FinalizerCallback)>(FinalizerCallback), |
| 65 | testData); |
| 66 | |
| 67 | for (int i = 0; i < threadCount; ++i) { |
| 68 | // A copy of the ThreadSafeFunction will go to the thread entry point |
| 69 | testData->threads.push_back(std::thread(entryWithTSFN, tsfn, i)); |
| 70 | } |
| 71 | |
| 72 | return testData->deferred.Promise(); |
| 73 | } |
| 74 | |
| 75 | // Task instance created for each new std::thread |
| 76 | class DelayedTSFNTask { |