Тёмный

C++ Weekly - Ep 445 - C++11's thread_local 

C++ Weekly With Jason Turner
Подписаться 113 тыс.
Просмотров 9 тыс.
50% 1

☟☟ Awesome T-Shirts! Sponsors! Books! ☟☟
Upcoming Workshop: C++ Best Practices, NDC TechTown, Sept 9-10, 2024
► ndctechtown.co...
Upcoming Workshop: Applied constexpr: The Power of Compile-Time Resources, C++ Under The Sea, October 10, 2024
► cppunderthesea...
Episode details: github.com/lef...
T-SHIRTS AVAILABLE!
► The best C++ T-Shirts anywhere! my-store-d16a2...
WANT MORE JASON?
► My Training Classes: emptycrate.com/...
► Follow me on twitter: / lefticus
SUPPORT THE CHANNEL
► Patreon: / lefticus
► Github Sponsors: github.com/spo...
► Paypal Donation: www.paypal.com...
GET INVOLVED
► Video Idea List: github.com/lef...
JASON'S BOOKS
► C++23 Best Practices
Leanpub Ebook: leanpub.com/cp...
► C++ Best Practices
Amazon Paperback: amzn.to/3wpAU3Z
Leanpub Ebook: leanpub.com/cp...
JASON'S PUZZLE BOOKS
► Object Lifetime Puzzlers Book 1
Amazon Paperback: amzn.to/3g6Ervj
Leanpub Ebook: leanpub.com/ob...
► Object Lifetime Puzzlers Book 2
Amazon Paperback: amzn.to/3whdUDU
Leanpub Ebook: leanpub.com/ob...
► Object Lifetime Puzzlers Book 3
Leanpub Ebook: leanpub.com/ob...
► Copy and Reference Puzzlers Book 1
Amazon Paperback: amzn.to/3g7ZVb9
Leanpub Ebook: leanpub.com/co...
► Copy and Reference Puzzlers Book 2
Amazon Paperback: amzn.to/3X1LOIx
Leanpub Ebook: leanpub.com/co...
► Copy and Reference Puzzlers Book 3
Leanpub Ebook: leanpub.com/co...
► OpCode Puzzlers Book 1
Amazon Paperback: amzn.to/3KCNJg6
Leanpub Ebook: leanpub.com/op...
RECOMMENDED BOOKS
► Bjarne Stroustrup's A Tour of C++ (now with C++20/23!): amzn.to/3X4Wypr
AWESOME PROJECTS
► The C++ Starter Project - Gets you started with Best Practices Quickly - github.com/cpp...
► C++ Best Practices Forkable Coding Standards - github.com/cpp...
O'Reilly VIDEOS
► Inheritance and Polymorphism in C++ - www.oreilly.co...
► Learning C++ Best Practices - www.oreilly.co...

Опубликовано:

 

24 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 35   
@szymoniak75
@szymoniak75 15 дней назад
TLDR thread_local is like static but per thread, the rest of the video isn't strictly relevant
@raymundhofmann7661
@raymundhofmann7661 15 дней назад
I write "tread_local static", as I thought you should and MSVC doesn't complain. It makes it obvious. Better use it sparingly, anyway, it is a kind of global variable.
@kuhluhOG
@kuhluhOG 15 дней назад
@@raymundhofmann7661 yeah, but at least errno was made thread_local in C11 (unlike before that)
@SirVestniK
@SirVestniK 15 дней назад
fmt/format can print anything that can be printed to std::ostream but you need to include for it.
@Zettymaster
@Zettymaster 15 дней назад
i really wished you talked about the potential use cases for this too, since you mainly concentrated on what it does.
@yato3335
@yato3335 15 дней назад
I think the main benefit of this is that you can be 100% sure that mutation of these variables is thread-safe. You can use this for small caches, hash states, RNG states -- something that will be used very frequently, takes up a limited amount of space and doesn't have to be shared between threads
@td5786
@td5786 14 дней назад
@@yato3335 yeah it's great for RNG states, allowing deterministic multithread Monte Carlo computations for instance.
@bo4-x2n
@bo4-x2n 13 дней назад
once you know what it does, the use cases should be common sense tbh.
@corndogblue7
@corndogblue7 10 дней назад
Lol, ok never got to the good part of thread_local so here at least is my main _use case_ for thread local (as I have used it and seen it used). If you have a scenario where you have multiple threads, and at some point you allocate memory on your threaded tasks (most of the time this will happen through std::vector) then you may notice that your threads will block each other, this is because they all need to sip some heap memory and there is only one drinking fountain for that memory and they all have to share it. But if you declare your vector as `thread_local std::vector foo` you get to have your own drinking fountain and you don't block anyone else who needs to drink, and by that I mean allocate memory, sorry for the dumb analogy. So thread_local allows you to do allocation local to your thread, this huge if say you made a CPU side ray tracer that has really high utilization of each thread, and each task on the thread needs to allocate. Without thread_local memory management becomes very complex, and very painful. But at the end of the day just know that it prevents threads from blocking on memory allocation. Ok that's all :D
@ohwow2074
@ohwow2074 15 дней назад
You should have used thread local in the global namespace (file scope). That makes things even more tricky.
@cubeman5303
@cubeman5303 15 дней назад
Yeah, the whole subject of storage class specifiers is scary complicated, though the fun begins in project with multiple translation units and usage of inline definitions.
@milasudril
@milasudril 15 дней назад
The example also demonstrates the delay of spinning up a new thread. That is why fork-join is not an optimal threading pattern.
@PopescuAlexandruCristian
@PopescuAlexandruCristian 15 дней назад
Good luck if you want to use this in a system with dynamicly loaded dlls on windows.
@Roibarkan
@Roibarkan 14 дней назад
5:03 Actually it seems that jthread was only introduced in C++20
@jlmonolith
@jlmonolith 13 дней назад
Would love a deeper dive into how and when thread_local objects get destroyed when a thread ends.
@cppweekly
@cppweekly 10 дней назад
I'm always happy to have topic requests - they are tracked here: github.com/lefticus/cpp_weekly/issues/ Feel free to add your request and vote on the other topics!
@xBiggs
@xBiggs 6 дней назад
That is weird. In C11, thread_local objects must have global storage duration, but in C++ they don't have to?
@TomSchultz78
@TomSchultz78 15 дней назад
We'll have to discuss this tomorrow
@BenjaminWheeler0510
@BenjaminWheeler0510 13 дней назад
All I know is that its lifetime ends at the end of the thread, but before the end of static lifetimes. 😎
@c0d3_m0nk3y
@c0d3_m0nk3y 12 дней назад
How performant are these? Is it ok to use them in high frequency code?
@kuhluhOG
@kuhluhOG 15 дней назад
6:05 Instead of stringstream, how about C++20's osyncstream?
@Subdest
@Subdest 15 дней назад
Usually i use thread locals for caching things like JNIenv.
@BenjaminWheeler0510
@BenjaminWheeler0510 13 дней назад
Why did puts solve the race condition? Is it just a quicker operation than cout?
@StanleyPinchak
@StanleyPinchak 12 дней назад
No the difference is that only a single operator
@BartKus
@BartKus 15 дней назад
I understood and used TLS before this video. After watching it, I have no idea what thread_local does. :)
@WndSks
@WndSks 15 дней назад
I find the way the raw Windows API deals with TLS easier to understand. TlsAlloc creates a memory slot for you in every thread. What you store in this slot is unique per thread.
@td5786
@td5786 14 дней назад
It tells pretty clearly what behavior you get from thread_local, it just doesn't tell you how it's done, as that depends on the platform.
@gjvdspam
@gjvdspam 15 дней назад
ThreadLocal is long existing in Java, in the latest edition they changed it somewhat. What I like about the C++ implementation of jthread is that RAII works also with Threads. I used ThreadLocal in implementations where each http request has it's own thread. Nice for config stuff per thread, without spreading everything under and copying the config everywhere. A singleton per thread. Obviously you have to be now what you're doing, but that's the C++ mantra anyways.
@AlfredoCorrea
@AlfredoCorrea 15 дней назад
so, when you have a mutable static variable (for a cache for example) and somebody comes and tell you, “this is not thread-safe” you simply put a thread_local and you are done?. I can’t explain why but I have the feeling that this is cheating and it is a bad idea in the long run. Does anybody agree or have a better global perspective on this?
@bloodgain
@bloodgain 15 дней назад
As far as I know, that's it. Statics aren't inherently thread-safe, and this gives you individual thread-safe statics instead of shared memory that requires guards. The only downside I can see is that it may not always be reliably static with dynamic libraries (as another comment pointed out with DLLs on Windows). But if you're _relying_ on a static being initialized on a call instead of using it as an optimization, that's already bad design, IMHO. That's reliance on side-effects, when you should be designing toward functional purity and only deviating from it when such an optimization yields high value. If it _needs_ to be initialized, it should be a parameter or wrapped in an object at the calling scope (e.g. a generator/iterator).
@AlfredoCorrea
@AlfredoCorrea 15 дней назад
@@bloodgain I know that some libraries (that I haven’t used) give “lightweight” threads (like HPX) and these don’t (can’t?) have thread_local variables. This is as much as I was able to tell about possible limitations of this feature.
@anon_y_mousse
@anon_y_mousse 13 дней назад
Personally, I prefer more stringent management of threads instead of this floaty nonsense. Either it's running or it's not, and as the programmer I should damn well know which state it's in at any given time. I would hope that more programmers would agree with this sentiment because it's a scary landscape if not.
Далее
C++ Weekly - Ep 446 - ImHex: An Awesome Hex Editor
6:07
DOTA 2 - ИНВОКЕР ИМБА!
24:17
Просмотров 268 тыс.
this is my favorite hardware hacking tool
11:42
Просмотров 132 тыс.
I Hacked Diablo II To Use Modern Graphics
13:16
Просмотров 210 тыс.
The Pointer to Implementation (pImpl) idiom in C++
6:54
Unity Is Changing Course
26:01
Просмотров 141 тыс.
Replace Is Number Saves 440GB A WEEK
9:54
Просмотров 216 тыс.
The unexpected probability result confusing everyone
17:24
I tried Swift and came out a different person
1:56:59
Просмотров 77 тыс.