Fix deadlock issues in ThreadPool (#29885)

Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/29885

### Summary

Currently, we have a deadlock issue on iOS when running Resnet50. The problem happens when the task being run in the ThreadPool wants to call `getNumThread()` who will try to acquire the same mutex. And thus cause the deadlock situation. The fix is just remove the guard for `_numThreads`, as it's not likely to change after initialization.

### Test Plan

1. Generate a Resnet50 model using trace_model.py
2. Run `ios/TestApp/bootstrap.sh` to do the benchmark

cc shoumikhin AshkanAliabadi

Test Plan: Imported from OSS

Differential Revision: D18533505

Pulled By: xta0

fbshipit-source-id: 2a069d20b59833ec8b02ff05515c3739a85a15de
This commit is contained in:
Tao Xu 2019-11-15 19:26:17 -08:00 committed by Facebook Github Bot
parent 0a33c3f1a1
commit b730d04ed2
2 changed files with 1 additions and 2 deletions

View File

@ -214,7 +214,7 @@ int get_num_threads() {
#else
caffe2::ThreadPool* pool = caffe2::mobile_threadpool();
// caffe2::ThreadPool::getNumThreads() counts the current thread.
return !pool ? 1 /* current thread */ : pool->getNumThreads();
return !pool || in_parallel_region() ? 1 /* current thread */ : pool->getNumThreads();
#endif // C10_MOBILE
}

View File

@ -84,7 +84,6 @@ ThreadPool::ThreadPool(int numThreads)
ThreadPool::~ThreadPool() {}
int ThreadPool::getNumThreads() const {
std::lock_guard<std::mutex> guard(executionMutex_);
return numThreads_;
}