๐—ง๐—ต๐—ฒ ๐—ผ๐—ป๐—ฒ ๐—ฑ๐—ถ๐—ณ๐—ณ๐—ฒ๐—ฟ๐—ฒ๐—ป๐—ฐ๐—ฒ ๐—ฏ๐—ฒ๐˜๐˜„๐—ฒ๐—ฒ๐—ป ๐˜€๐˜๐—ฑ::๐˜๐—ต๐—ฟ๐—ฒ๐—ฎ๐—ฑ ๐—ฎ๐—ป๐—ฑ ๐˜€๐˜๐—ฑ::๐—ท๐˜๐—ต๐—ฟ๐—ฒ๐—ฎ๐—ฑ

Written by

in

Let’s consider a simple function called longTimeFunction. It simply prints a message indicating it has started, waits for three seconds, and then prints a message indicating it has finished.

If we run this function using std::jthread – leveraging the wrapper class’s dtor for convenience – the program will terminate successfully; this is because std::jthread automatically calls join() in its dtor when the thread object is destroyed.

If we use std::thread, however, the program will terminate with an error, as join() is not called automatically in the dtor, requiring us to call it manually.

This is a key difference between the two when terminating a thread by destroying the object. Of course, there are other differences as well.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *