On 2/7/2024 8:15 AM, Bonita Montero wrote:
> Am 06.02.2024 um 21:07 schrieb Chris M. Thomasson:
>> On 2/6/2024 5:01 AM, Bonita Montero wrote:
>>> Am 06.02.2024 um 07:38 schrieb Chris M. Thomasson:
>>>> On 2/5/2024 10:12 PM, Bonita Montero wrote:
>>>>> Am 05.02.2024 um 20:09 schrieb Chris M. Thomasson:
>>>>>
>>>>>> Can you create a little self-contained example that shows these
>>>>>> warnings?
>>>>>
>>>>> Make the pragmas commented and look what the IDE shows as warnings.
>>>>>
>>>>
>>>> Think about getting a warning free compile. Think about what these
>>>> warnings actually mean. If you find a bug, report it to the MSVC team.
>>>
>>> Try this ...
>>>
>>> void fn( unique_lock<mutex> &lock )
>>> {
>>> assert(lock);
>>> lock.unlock();
>>> }
>>>
>>> ... and you'll get a warning.
>>>
>>
>> Fwiw, I get no warning with:
>>
>> {
>> std::unique_lock<std::mutex> lock(m_mutex);
>> assert(lock);
>> lock.unlock();
>> //lock.unlock(); // this throws an exception.
>> }
>
> That's sth. totally different. My function has the pre-condition
> that it inherits the lock of the calling function and sometimes
> it unlocks it.
Ahhh. I got them with:
__________________________________
namespace ct
{
struct mutex_test
{
std::mutex m_mutex;
void
bar(std::unique_lock<std::mutex>& lock)
{
assert(lock); // better be locked!
lock.unlock();
}
void
foo()
{
std::unique_lock<std::mutex> lock(m_mutex);
bar(lock); // beware. unlocks the damn thing!
lock.lock(); // okay...
}
};
}
__________________________________
Give the warnings:
__________________________________
Severity Code Description Project File Line Suppression State Details
Warning C26115 Failing to release lock 'this->m_mutex' in function
'ct::mutex_test::foo'. ct_threads
D:\ct_dev\projects\ct_threads\ct_threads\ct_main.cpp 26
Warning C26111 Caller failing to release lock 'this->m_mutex' before
calling function 'std::unique_lock<std::mutex>::lock'. ct_threads
D:\ct_dev\projects\ct_threads\ct_threads\ct_main.cpp 26
Warning C26110 Caller failing to hold lock 'lock' before calling
function 'std::unique_lock<std::mutex>::unlock'. ct_threads
D:\ct_dev\projects\ct_threads\ct_threads\ct_main.cpp 18
__________________________________
You just have to be very careful with this type of logic. These warnings
elude to that.
[...]