# Multithread deadlock

**URL:** <https://discuss.pytorch.org/t/multithread-deadlock/44159>\
**Category:** C++\
**Created:** [May 2, 2019, 8:12am UTC](https://discuss.pytorch.org/t/multithread-deadlock/44159 "2019-05-02T08:12:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pascal.soveaux](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/p/74df32/32.png) [@pascal.soveaux](https://discuss.pytorch.org/u/pascal.soveaux)\
**Post date:** [May 2, 2019, 8:12am UTC](https://discuss.pytorch.org/t/multithread-deadlock/44159/1 "2019-05-02T08:12:59Z")

</div>

Hello,

I am using v1.0.1/CUDA 10.1 on a 2080. glibc is 2.28.

I read that libtorch is multithread safe but I cannot make it work correctly in a MT C++ app. I have 2 trainers each one in its own thread, no global var, no static var, no shared var… but it always ends in a deadlock.

When deadlocked both threads are near cuLaunchKernel. Is there something I need to do ? Do I have to initialize a context or something?

I also tested with master and a nightly of libtorch but it ends the same.

Thanks,

Pascal

---

<div class="post-metadata">

**Author:** ![pascal.soveaux](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/p/74df32/32.png) [@pascal.soveaux](https://discuss.pytorch.org/u/pascal.soveaux)\
**Post date:** [May 8, 2019, 11:33pm UTC](https://discuss.pytorch.org/t/multithread-deadlock/44159/2 "2019-05-08T23:33:33Z")

</div>

Hello,

Great! it seems that PoolWindow makes it work correctly.

I run into a problem where available\_handles (global) was destructed _before_ PoolWindow (thread\_local) (Valgrind told me).

```auto
available_handles[d_h.first].push_back(d_h.second); // SIGSEGV

```

I dont know how this can happen, I will try to reproduce.

Pascal

---

<div class="post-metadata">

**Author:** ![yf225](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/yf225/32/3418_2.png) [@yf225](https://discuss.pytorch.org/u/yf225)\
**Post date:** [May 9, 2019, 3:39am UTC](https://discuss.pytorch.org/t/multithread-deadlock/44159/3 "2019-05-09T03:39:05Z")

</div>

@pascal.soveaux [https://github.com/pytorch/pytorch/issues/19394](https://github.com/pytorch/pytorch/issues/19394) is likely related. Would you like to comment in the issue about your findings? Thanks!

---

<div class="post-metadata">

**Author:** ![pascal.soveaux](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/p/74df32/32.png) [@pascal.soveaux](https://discuss.pytorch.org/u/pascal.soveaux)\
**Post date:** [May 11, 2019, 9:44pm UTC](https://discuss.pytorch.org/t/multithread-deadlock/44159/4 "2019-05-11T21:44:54Z")

</div>

@yf225 Sorry for the late response. I found that this problem is totally not related with poolwindow. The thread is exited after the “main” (libtorch) thread because both are worker threads in fact.
