Discussion:
msvc wrt locking errors...
(too old to reply)
Chris M. Thomasson
2024-02-06 07:10:23 UTC
Permalink
This is not giving me a warning, but an assertion. I am still wondering
why Bonita has to suppress all of those warnings wrt this thread ala MSVC:

https://groups.google.com/g/comp.lang.c++/c/pFsb3JI11-k

Here is some bugged code I just created, wrt MSVC version 17.8.5. No
warnings, but an assertion:

Loading Image...

code:
____________________________________
#include <iostream>
#include <functional>
#include <thread>
#include <mutex>


namespace ct
{
struct mutex_test
{
std::mutex m_mutex;

void
works()
{
m_mutex.lock();
m_mutex.unlock();
}


void
foobar()
{
m_mutex.lock();
//m_mutex.unlock(); YIKES!
}
};
}


int
main()
{
std::cout << "ct_threads... ;^)\n" << std::endl;

{
ct::mutex_test test;

test.works();
test.foobar(); // humm... Kabboom!
}

return 0;
}
____________________________________


Btw, I am going to port my new XCHG based code from Relacy race detector
into actual C++ code where we all can play with it. Getting to a point
where I need to use it for a project.
Chris M. Thomasson
2024-02-06 08:33:57 UTC
Permalink
Post by Chris M. Thomasson
This is not giving me a warning, but an assertion. I am still wondering
https://groups.google.com/g/comp.lang.c++/c/pFsb3JI11-k
[...]

Sometime tomorrow I can work on this. I need to compile the bugged test
code with the warning level bumped up.
Chris M. Thomasson
2024-02-07 20:47:40 UTC
Permalink
Post by Chris M. Thomasson
This is not giving me a warning, but an assertion. I am still wondering
https://groups.google.com/g/comp.lang.c++/c/pFsb3JI11-k
[...]

Okay, made some progress. Here is a little example of where the warnings
pop up. "Sketchy" use of std::unique_lock, well, imvvho that is:
___________________________________________
#include <iostream>
#include <functional>
#include <thread>
#include <mutex>
#include <cassert>


namespace ct
{
struct mutex_test
{
std::mutex m_mutex;

void
bar(std::unique_lock<std::mutex>& lock)
{
assert(lock);
lock.unlock();
}

void
foo()
{
std::unique_lock<std::mutex> lock(m_mutex);

// lock will be unlocked after this call!
bar(lock);

lock.lock(); // Yup...
}
};
}


int
main()
{
std::cout << "ct_threads... ;^)\n" << std::endl;

{
ct::mutex_test test;

test.foo();
}

return 0;
}
___________________________________________


Interesting wrt MSVC.

Loading...