Problem with mutex.lock()

Bob Bishop rb at gid.co.uk
Mon Feb 11 12:30:37 UTC 2019


Hi,

> On 11 Feb 2019, at 11:09, Banta Plan <bantaplan at outlook.de> 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
> 
> 
> 
> _______________________________________________
> freebsd-toolchain at freebsd.org mailing list
> https://lists.freebsd.org/mailman/listinfo/freebsd-toolchain
> To unsubscribe, send any mail to "freebsd-toolchain-unsubscribe at freebsd.org"
> 


--
Bob Bishop       t: +44 (0)118 940 1243
rb at gid.co.uk     m: +44 (0)783 626 4518







More information about the freebsd-toolchain mailing list