# Error in \<TReentrantRWLock::WriteUnLock\>: Write lock already released TThread member function

**URL:** <https://root-forum.cern.ch/t/error-in-treentrantrwlock-writeunlock-write-lock-already-released-tthread-member-function/43663>\
**Category:** ROOT\
**Tags:** cling\
**Created:** [February 22, 2021, 5:38pm UTC](https://root-forum.cern.ch/t/error-in-treentrantrwlock-writeunlock-write-lock-already-released-tthread-member-function/43663 "2021-02-22T17:38:25Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ferhue](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ferhue/32/29462_2.png) [@ferhue](https://root-forum.cern.ch/u/ferhue)\
**Post date:** [February 22, 2021, 5:38pm UTC](https://root-forum.cern.ch/t/error-in-treentrantrwlock-writeunlock-write-lock-already-released-tthread-member-function/43663/1 "2021-02-22T17:38:25Z")

</div>

Hello,  
I have an CMake standalone application using ROOT TThreads.

I have followed what is suggested by GSI, point 6.1 [How to Use the ROOT TThread](https://web-docs.gsi.de/~go4/HOWTOthreads/howtothreadsbody.html#tth_sEc6.1) to use a custom class with a member function.

When I start the thread, I get this warning:  
`Error in <TReentrantRWLock::WriteUnLock>: Write lock already released for 0x560b57ac93f8`

What does it mean? Is it a problem? The program works fine.

The issue arises when I call the inside the static void\* handle function a non-static member function of the MyThreadClass (derived from TThread) as follows:

```auto
void *MyThreadClass::handle(void *ptr) {
    MyThreadClass* const thread = static_cast<MyThreadClass *>(ptr);
    thread->MyFunction();
    return nullptr;
}

```

which is simlar to [How to Use the ROOT TThread](https://web-docs.gsi.de/~go4/HOWTOthreads/howtothreadsbody.html#tth_sEc6.1)  
or [c++ - Constructing thread executing a member function of a derived class - Stack Overflow](https://stackoverflow.com/questions/24206878/constructing-thread-executing-a-member-function-of-a-derived-class/24209679#24209679)

MyThreadClass\* thread has the address 0x560b57ac93f8, matching with the warning message.

Thanks for the help!

* * *

_ROOT Version:_ 6.23/01  
_Platform:_ Ubuntu 18  
_Compiler:_ gcc

* * *

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [February 22, 2021, 6:46pm UTC](https://root-forum.cern.ch/t/error-in-treentrantrwlock-writeunlock-write-lock-already-released-tthread-member-function/43663/2 "2021-02-22T18:46:21Z")

</div>

Maybe @eguiraud can have an idea…

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [February 22, 2021, 7:53pm UTC](https://root-forum.cern.ch/t/error-in-treentrantrwlock-writeunlock-write-lock-already-released-tthread-member-function/43663/3 "2021-02-22T19:53:31Z")

</div>

Hi,  
TThread is legacy/deprecated in favor of std::thread, I’m afraid I don’t know much about its internals. We need @pcanal . With that said:

> [@ferhue](#):
>
> Is it a problem?

It looks like a bug in either TThread or ROOT’s TReentrantRWLock: it might not be a problem for your program but we definitely don’t want that to happen in general, in ROOT. It would be nice if you could provide a reproducer.

Cheers,  
Enrico

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [February 22, 2021, 8:00pm UTC](https://root-forum.cern.ch/t/error-in-treentrantrwlock-writeunlock-write-lock-already-released-tthread-member-function/43663/4 "2021-02-22T20:00:08Z")

</div>

When in the process did you call `ROOT::EnableThreadSafety()` ?

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [February 22, 2021, 8:04pm UTC](https://root-forum.cern.ch/t/error-in-treentrantrwlock-writeunlock-write-lock-already-released-tthread-member-function/43663/5 "2021-02-22T20:04:09Z")

</div>

> [@ferhue](#):
>
> MyThreadClass\* thread has the address 0x560b57ac93f8, matching with the warning message.

That is _very_ weird. The object type printing the message is `TReentrantRWLock` which (unless you inherited from that internal type!) is not the case of your thread object. This would indicate that there is somewhere a memory error … (maybe the static case is wrong … humm still odd … maybe you could provide a complete running (and thus failing) example)

---

<div class="post-metadata">

**Author:** ![ferhue](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ferhue/32/29462_2.png) [@ferhue](https://root-forum.cern.ch/u/ferhue)\
**Post date:** [February 22, 2021, 9:58pm UTC](https://root-forum.cern.ch/t/error-in-treentrantrwlock-writeunlock-write-lock-already-released-tthread-member-function/43663/6 "2021-02-22T21:58:28Z")

</div>

I must have misread the address, because I checked the address, and they do not match now. Sorry about that.

Here is what my debugger shows about how this function is reached when I click on the ‘Start’ TGButton:

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/3/b/3b510955b2c419e402d9791904c3246ab9f45f23.png)

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/9/a/9a9453246010f5242c3d263979675291707f8c44.png)

I will try to generate a minimum working example.

---

<div class="post-metadata">

**Author:** ![ferhue](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ferhue/32/29462_2.png) [@ferhue](https://root-forum.cern.ch/u/ferhue)\
**Post date:** [February 22, 2021, 10:05pm UTC](https://root-forum.cern.ch/t/error-in-treentrantrwlock-writeunlock-write-lock-already-released-tthread-member-function/43663/7 "2021-02-22T22:05:52Z")

</div>

> [@pcanal](#):
>
> ROOT::EnableThreadSafety()

This solves it! Thanks so much.

I did not know about this ‘magic’ statement. I did not find it in the manual I was following: [Chapter: Threads](https://root.cern.ch/root/htmldoc/guides/users-guide/Threads.html)

Maybe it can be added to it ? See also [TThread documentation outdated · Issue #7216 · root-project/root · GitHub](https://github.com/root-project/root/issues/7216)

Is there any rule when to call it? At program beginning?

Thanks again!

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [February 22, 2021, 10:20pm UTC](https://root-forum.cern.ch/t/error-in-treentrantrwlock-writeunlock-write-lock-already-released-tthread-member-function/43663/8 "2021-02-22T22:20:14Z")

</div>

It must be called before the start of any threads. Calling it at the program beginning is indeed a safe place.

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [March 8, 2021, 10:21pm UTC](https://root-forum.cern.ch/t/error-in-treentrantrwlock-writeunlock-write-lock-already-released-tthread-member-function/43663/9 "2021-03-08T22:21:00Z")

</div>

Closed on March 8, 2021, 10:21pm UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
