Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 11 Feb 2019 12:15:06 +0000
From:      Bob Bishop <rb@gid.co.uk>
To:        Banta Plan <bantaplan@outlook.de>
Cc:        "freebsd-toolchain@freebsd.org" <freebsd-toolchain@freebsd.org>
Subject:   Re: Problem with mutex.lock()
Message-ID:  <78296B5F-143C-4B4A-9D29-78AD81A1EE3E@gid.co.uk>
In-Reply-To: <VI1PR02MB4671CBB16996C63C4ABB317BD7640@VI1PR02MB4671.eurprd02.prod.outlook.com>
References:  <VI1PR02MB4671CBB16996C63C4ABB317BD7640@VI1PR02MB4671.eurprd02.prod.outlook.com>

next in thread | previous in thread | raw e-mail | index | archive | help
Hi,

> On 11 Feb 2019, at 11:09, Banta Plan <bantaplan@outlook.de> wrote:
>=20
> I compiled this program and it crashes if executed.
>=20
> Compiled with: FreeBSD clang version 6.0.1 (tags/RELEASE_601/final =
335540) (based on LLVM 6.0.1)
>=20
> ## CMakeLists.txt
> cmake_minimum_required(VERSION 3.13)
> project(mainP)
>=20
> set(CMAKE_CXX_STANDARD 17)
>=20
> add_executable(mainP main.cpp)
>=20
> find_package(Threads)
> target_link_libraries(mainP ${CMAKE_THREAD_LIBS_INIT})
>=20
> ## main.cpp
>=20
> #include <iostream>
> #include <thread>
> #include <mutex>
> #include <chrono>
> #include <functional>
>=20
> 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);
>=20
>        std::cout << "Before unlock" << std::endl;
>        m.unlock();
>        std::cout << "After unlock" << std::endl;
>=20
>    }).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=E2=80=99t like that.

>    std::cout << "After lock2" << std::endl;
>=20
>    return 0;
> }
> ##
>=20
> 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
>=20
>=20
>=20
> _______________________________________________
> freebsd-toolchain@freebsd.org mailing list
> https://lists.freebsd.org/mailman/listinfo/freebsd-toolchain
> To unsubscribe, send any mail to =
"freebsd-toolchain-unsubscribe@freebsd.org"
>=20


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








Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?78296B5F-143C-4B4A-9D29-78AD81A1EE3E>