Problem with mutex.lock()

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Problem with mutex.lock()

Banta Plan
I compiled this program and it crashes if executed.

Compiled with: FreeBSD clang version 6.0.1 (tags/RELEASE_601/final 335540) (based on LLVM 6.0.1)

## CMakeLists.txt
cmake_minimum_required(VERSION 3.13)
project(mainP)

set(CMAKE_CXX_STANDARD 17)

add_executable(mainP main.cpp)

find_package(Threads)
target_link_libraries(mainP ${CMAKE_THREAD_LIBS_INIT})

## main.cpp

#include <iostream>
#include <thread>
#include <mutex>
#include <chrono>
#include <functional>

int main() {
    using namespace std::chrono_literals;
    std::mutex m;
    m.lock();
    std::thread([&] {
        std::cout << "Before sleepFor" << std::endl;
        std::this_thread::sleep_for(2s);

        std::cout << "Before unlock" << std::endl;
        m.unlock();
        std::cout << "After unlock" << std::endl;

    }).detach();
    std::cout << "Before lock2" << std::endl;
    m.lock();
    std::cout << "After lock2" << std::endl;

    return 0;
}
##

This program compiles and runs under MacOS (Clang Version: Apple LLVM version 10.0.0 (clang-1000.11.45.5)) and
manjaro arm64 with gcc (GCC) 8.2.1 20180831



_______________________________________________
[hidden email] mailing list
https://lists.freebsd.org/mailman/listinfo/freebsd-toolchain
To unsubscribe, send any mail to "[hidden email]"
Reply | Threaded
Open this post in threaded view
|

Re: Problem with mutex.lock()

Konstantin Belousov
On Mon, Feb 11, 2019 at 11:09:29AM +0000, Banta Plan wrote:

> I compiled this program and it crashes if executed.
>
> Compiled with: FreeBSD clang version 6.0.1 (tags/RELEASE_601/final 335540) (based on LLVM 6.0.1)
>
> ## CMakeLists.txt
> cmake_minimum_required(VERSION 3.13)
> project(mainP)
>
> set(CMAKE_CXX_STANDARD 17)
>
> add_executable(mainP main.cpp)
>
> find_package(Threads)
> target_link_libraries(mainP ${CMAKE_THREAD_LIBS_INIT})
>
> ## main.cpp
>
> #include <iostream>
> #include <thread>
> #include <mutex>
> #include <chrono>
> #include <functional>
>
> int main() {
>     using namespace std::chrono_literals;
>     std::mutex m;
>     m.lock();
>     std::thread([&] {
>         std::cout << "Before sleepFor" << std::endl;
>         std::this_thread::sleep_for(2s);
>
>         std::cout << "Before unlock" << std::endl;
>         m.unlock();
This unlock is performed on the mutex that the calling thread does not
hold the lock on.  The behaviour for such case is undefined, from my reading
of C++17 std.

FreeBSD pthread mutexes check this condition.

>         std::cout << "After unlock" << std::endl;
>
>     }).detach();
>     std::cout << "Before lock2" << std::endl;
>     m.lock();
>     std::cout << "After lock2" << std::endl;
>
>     return 0;
> }
> ##
>
> This program compiles and runs under MacOS (Clang Version: Apple LLVM version 10.0.0 (clang-1000.11.45.5)) and
> manjaro arm64 with gcc (GCC) 8.2.1 20180831
>
>
>
> _______________________________________________
> [hidden email] mailing list
> https://lists.freebsd.org/mailman/listinfo/freebsd-toolchain
> To unsubscribe, send any mail to "[hidden email]"
_______________________________________________
[hidden email] mailing list
https://lists.freebsd.org/mailman/listinfo/freebsd-toolchain
To unsubscribe, send any mail to "[hidden email]"
Reply | Threaded
Open this post in threaded view
|

Re: Problem with mutex.lock()

Bob Bishop
In reply to this post by Banta Plan
Hi,

> On 11 Feb 2019, at 11:09, Banta Plan <[hidden email]> wrote:
>
> I compiled this program and it crashes if executed.
>
> Compiled with: FreeBSD clang version 6.0.1 (tags/RELEASE_601/final 335540) (based on LLVM 6.0.1)
>
> ## CMakeLists.txt
> cmake_minimum_required(VERSION 3.13)
> project(mainP)
>
> set(CMAKE_CXX_STANDARD 17)
>
> add_executable(mainP main.cpp)
>
> find_package(Threads)
> target_link_libraries(mainP ${CMAKE_THREAD_LIBS_INIT})
>
> ## main.cpp
>
> #include <iostream>
> #include <thread>
> #include <mutex>
> #include <chrono>
> #include <functional>
>
> int main() {
>    using namespace std::chrono_literals;
>    std::mutex m;
>    m.lock();

[**]

>    std::thread([&] {
>        std::cout << "Before sleepFor" << std::endl;
>        std::this_thread::sleep_for(2s);
>
>        std::cout << "Before unlock" << std::endl;
>        m.unlock();
>        std::cout << "After unlock" << std::endl;
>
>    }).detach();
>    std::cout << "Before lock2" << std::endl;
>    m.lock();

This lock is called by the main thread which already holds the lock [** above]. Apparently it doesn’t like that.

>    std::cout << "After lock2" << std::endl;
>
>    return 0;
> }
> ##
>
> This program compiles and runs under MacOS (Clang Version: Apple LLVM version 10.0.0 (clang-1000.11.45.5)) and
> manjaro arm64 with gcc (GCC) 8.2.1 20180831
>
>
>
> _______________________________________________
> [hidden email] mailing list
> https://lists.freebsd.org/mailman/listinfo/freebsd-toolchain
> To unsubscribe, send any mail to "[hidden email]"
>


--
Bob Bishop       t: +44 (0)118 940 1243
[hidden email]     m: +44 (0)783 626 4518





_______________________________________________
[hidden email] mailing list
https://lists.freebsd.org/mailman/listinfo/freebsd-toolchain
To unsubscribe, send any mail to "[hidden email]"
Reply | Threaded
Open this post in threaded view
|

AW: Problem with mutex.lock()

Banta Plan
In reply to this post by Banta Plan
I think you can reduce the problem to:
##
int main() {
    std::mutex m;
    m.lock();
    m.lock();

    return 0;
}
##

This should deadlock. But this will crash the program.
________________________________
Von: Banta Plan
Gesendet: Montag, 11. Februar 2019 12:09
An: [hidden email]
Betreff: Problem with mutex.lock()

I compiled this program and it crashes if executed.

Compiled with: FreeBSD clang version 6.0.1 (tags/RELEASE_601/final 335540) (based on LLVM 6.0.1)

## CMakeLists.txt
cmake_minimum_required(VERSION 3.13)
project(mainP)

set(CMAKE_CXX_STANDARD 17)

add_executable(mainP main.cpp)

find_package(Threads)
target_link_libraries(mainP ${CMAKE_THREAD_LIBS_INIT})

## main.cpp

#include <iostream>
#include <thread>
#include <mutex>
#include <chrono>
#include <functional>

int main() {
    using namespace std::chrono_literals;
    std::mutex m;
    m.lock();
    std::thread([&] {
        std::cout << "Before sleepFor" << std::endl;
        std::this_thread::sleep_for(2s);

        std::cout << "Before unlock" << std::endl;
        m.unlock();
        std::cout << "After unlock" << std::endl;

    }).detach();
    std::cout << "Before lock2" << std::endl;
    m.lock();
    std::cout << "After lock2" << std::endl;

    return 0;
}
##

This program compiles and runs under MacOS (Clang Version: Apple LLVM version 10.0.0 (clang-1000.11.45.5)) and
manjaro arm64 with gcc (GCC) 8.2.1 20180831



_______________________________________________
[hidden email] mailing list
https://lists.freebsd.org/mailman/listinfo/freebsd-toolchain
To unsubscribe, send any mail to "[hidden email]"
Reply | Threaded
Open this post in threaded view
|

Re: Problem with mutex.lock()

Konstantin Belousov
On Mon, Feb 11, 2019 at 02:04:38PM +0000, Banta Plan wrote:

> I think you can reduce the problem to:
> ##
> int main() {
>     std::mutex m;
>     m.lock();
>     m.lock();
>
>     return 0;
> }
> ##
>
> This should deadlock.
Where is it specified that the program should deadlock ?
The behaviour for this case is undefined.

> But this will crash the program.
Which is not disallowed by standard.
_______________________________________________
[hidden email] mailing list
https://lists.freebsd.org/mailman/listinfo/freebsd-toolchain
To unsubscribe, send any mail to "[hidden email]"
Reply | Threaded
Open this post in threaded view
|

Re: Problem with mutex.lock()

Banta Plan
Just to wrap this up.

std::mutex behaves as it should. Still most of the other OSs implemented
it in a diverent way.

And just as a short help.

If somebody really wants to deadlock himself, it can be done with
std::condition_variables.

_______________________________________________
[hidden email] mailing list
https://lists.freebsd.org/mailman/listinfo/freebsd-toolchain
To unsubscribe, send any mail to "[hidden email]"