Thanks for using Compiler Explorer
Sponsors
Jakt
C++
Ada
Analysis
Android Java
Android Kotlin
Assembly
C
C3
Carbon
C++ (Circle)
CIRCT
Clean
CMake
CMakeScript
COBOL
C++ for OpenCL
MLIR
Cppx
Cppx-Blue
Cppx-Gold
Cpp2-cppfront
Crystal
C#
CUDA C++
D
Dart
Elixir
Erlang
Fortran
F#
GLSL
Go
Haskell
HLSL
Hook
Hylo
IL
ispc
Java
Julia
Kotlin
LLVM IR
LLVM MIR
Modula-2
Nim
Objective-C
Objective-C++
OCaml
OpenCL C
Pascal
Pony
Python
Racket
Ruby
Rust
Snowball
Scala
Solidity
Spice
SPIR-V
Swift
LLVM TableGen
Toit
TypeScript Native
V
Vala
Visual Basic
Vyper
WASM
Zig
Javascript
GIMPLE
Ygen
circle source #1
Output
Compile to binary object
Link to binary
Execute the code
Intel asm syntax
Demangle identifiers
Verbose demangling
Filters
Unused labels
Library functions
Directives
Comments
Horizontal whitespace
Debug intrinsics
Compiler
Build 128
Build 129
Build 131
Latest
Options
Source code
#feature on safety #include <https://raw.githubusercontent.com/cppalliance/safe-cpp/master/libsafecxx/single-header/std2.h?token=$(date%20+%s)> #include <chrono> using namespace std2; // mutex is sync, so arc<mutex<string>> is send. void entry_point(arc<mutex<string>> data, int thread_id) safe { // Lock the data through the mutex. // When lock_guard goes out of scope, the mutex is released. auto lock_guard = data->lock(); // Get a mutable borrow to the string data. When lock_guard goes out of // scope, this becomes a dangling pointer. The borrow checker prevents // us from accessing through dangling pointers. string^ s = mut lock_guard.borrow(); // Append a fire and print the new shared state. s.append("🔥"); // Drop the lock before printing the shared state. This makes the borrow // `s` a "dangling pointer," in the sense that it depends on an expired // lifetime. That will raise a borrowck error on `println(*s)`, which // attempts to access shared state outside of the lock. // drp lock_guard; // Drop the data before printing shared state. This decrement's arc's // reference count and could potentially free the string data. This // raises a borrowck error on `println(*s)` because the borrow on `data` // from `data->lock()` is kept live by the use of `println(*s)` via the // borrow `mut lock_guard.borrow()`. // drp data; // Print the updated shared state. println(*s); // Sleep 1s before returning. // For Compiler Explorer sleep 10 milliseconds per thread so we don't time out // the server. unsafe { std::this_thread::sleep_for(std::chrono::milliseconds(10)); } } int main() safe { // arc - Shared ownership. // mutex - Shared mutable access. arc<mutex<string>> shared_data(string("Hello world - ")); // Launch 10 threads. vector<thread> threads { }; for(int i : 10) mut threads.push_back(thread(entry_point, cpy shared_data, i)); // The consuming into_iterator produced by `rel threads` lets us relocate // elements out of the vector for the consuming thread::join call. for(thread t : rel threads) t rel.join(); }
Become a Patron
Sponsor on GitHub
Donate via PayPal
Source on GitHub
Mailing list
Installed libraries
Wiki
Report an issue
How it works
Contact the author
CE on Mastodon
CE on Bluesky
About the author
Statistics
Changelog
Version tree