Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 11 Feb 2019 14:09:59 +0200
From:      Konstantin Belousov <kostikbel@gmail.com>
To:        Banta Plan <bantaplan@outlook.de>
Cc:        "freebsd-toolchain@freebsd.org" <freebsd-toolchain@freebsd.org>
Subject:   Re: Problem with mutex.lock()
Message-ID:  <20190211120959.GY24863@kib.kiev.ua>
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
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
> 
> 
> 
> _______________________________________________
> 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"



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20190211120959.GY24863>