in src/fasttext.cc [779:816]
void FastText::startThreads(const TrainCallback& callback) {
start_ = std::chrono::steady_clock::now();
tokenCount_ = 0;
loss_ = -1;
trainException_ = nullptr;
std::vector<std::thread> threads;
if (args_->thread > 1) {
for (int32_t i = 0; i < args_->thread; i++) {
threads.push_back(std::thread([=]() { trainThread(i, callback); }));
}
} else {
// webassembly can't instantiate `std::thread`
trainThread(0, callback);
}
const int64_t ntokens = dict_->ntokens();
// Same condition as trainThread
while (keepTraining(ntokens)) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
if (loss_ >= 0 && args_->verbose > 1) {
real progress = real(tokenCount_) / (args_->epoch * ntokens);
std::cerr << "\r";
printInfo(progress, loss_, std::cerr);
}
}
for (int32_t i = 0; i < threads.size(); i++) {
threads[i].join();
}
if (trainException_) {
std::exception_ptr exception = trainException_;
trainException_ = nullptr;
std::rethrow_exception(exception);
}
if (args_->verbose > 0) {
std::cerr << "\r";
printInfo(1.0, loss_, std::cerr);
std::cerr << std::endl;
}
}