<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Hoàng Văn Khoa</title>
    <description>The latest articles on DEV Community by Hoàng Văn Khoa (@khoa).</description>
    <link>https://dev.to/khoa</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F931174%2F7b26ea53-235e-4dd6-8cb2-623f9bc621ac.png</url>
      <title>DEV Community: Hoàng Văn Khoa</title>
      <link>https://dev.to/khoa</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/khoa"/>
    <language>en</language>
    <item>
      <title>Introduction to multithreading in C/C++</title>
      <dc:creator>Hoàng Văn Khoa</dc:creator>
      <pubDate>Mon, 26 Sep 2022 10:24:25 +0000</pubDate>
      <link>https://dev.to/khoa/introduction-to-multithreading-in-cc-53aj</link>
      <guid>https://dev.to/khoa/introduction-to-multithreading-in-cc-53aj</guid>
      <description>&lt;p&gt;This is the first in a series of blog posts about multithreading in C/C++. The series contains not only my notes but also some bits of advice I made to myself. If you find them helpful, that is so great. If you find incorrect information, please let me know.&lt;br&gt;
I assumed you have some basic understanding of the thread of execution in general and some experience in C/C++ programming.&lt;/p&gt;

&lt;h2&gt;
  
  
  Hello Multithreading World
&lt;/h2&gt;

&lt;p&gt;The Modern C++ Standards (C++11 and later) bring many exciting features into the C++ language and standard library that make coding in C++ easier, more productive, and more secure. One of those features is the C++ Standard Library's built-in support for multithreading. Before C++11, developers have to write C-style code using platform-specific C APIs to enable multithreading in their code.&lt;/p&gt;

&lt;p&gt;Mentioning multithread C APIs, there are supports for multithreading in C. But they are not a part of the C Standard Library. I can name two here: &lt;a href="https://learn.microsoft.com/en-us/windows/win32/api/" rel="noopener noreferrer"&gt;Win32 API&lt;/a&gt; and &lt;a href="https://pubs.opengroup.org/onlinepubs/9699919799.2018edition/" rel="noopener noreferrer"&gt;POSIX&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;While Win32 API is Windows-specific, POSIX is an industry standard that many systems implement. For example, &lt;a href="https://www.apple.com/macos/monterey/" rel="noopener noreferrer"&gt;macOS&lt;/a&gt; is a &lt;a href="https://www.opengroup.org/openbrand/register/index2.html" rel="noopener noreferrer"&gt;POSIX-certified system&lt;/a&gt;. Linux, however, isn't a POSIX-certified system. But it can be qualified as &lt;em&gt;mostly POSIX-compliant&lt;/em&gt;. In automotive, a standard that requires its implementations must comply with POSIX is the &lt;a href="https://www.autosar.org/fileadmin/user_upload/standards/adaptive/17-03/AUTOSAR_SWS_OperatingSystemInterface.pdf" rel="noopener noreferrer"&gt;AutoSAR Adaptive Platform&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Currently, as an automotive software engineer, I have two options to enable multithreading in my code:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;C code and POSIX Threads (commonly known as Pthreads)&lt;/li&gt;
&lt;li&gt;C++ code and C++ Thread&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I can always write C-style code in C++ programs, but I will avoid that style of programming as much as possible. Mixing C-style code in a C++ program makes the code harder to understand and debug.&lt;/p&gt;

&lt;p&gt;The next sections will show an overview of multithreading in the two standards.&lt;/p&gt;

&lt;h2&gt;
  
  
  Pthreads
&lt;/h2&gt;

&lt;p&gt;The support for multithreading of a POSIX system consists of 3 components:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The header file &lt;a href="https://pubs.opengroup.org/onlinepubs/9699919799.2018edition/" rel="noopener noreferrer"&gt;&lt;code&gt;pthread.h&lt;/code&gt;&lt;/a&gt; is the declaration of the API.&lt;/li&gt;
&lt;li&gt;The implementation of the API.&lt;/li&gt;
&lt;li&gt;A C compiler and linker.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example, macOS is a POSIX-certified system. The implementation for Pthread is &lt;a href="https://github.com/apple-oss-distributions/libpthread" rel="noopener noreferrer"&gt;&lt;code&gt;lib_pthread&lt;/code&gt;&lt;/a&gt;. The macOS SDK, which developers can download for free, provides the header file &lt;code&gt;pthread.h&lt;/code&gt; and &lt;code&gt;clang&lt;/code&gt;, the compiler and linker. I'd recommend downloading &lt;a href="https://apps.apple.com/vn/app/xcode/id497799835?mt=12" rel="noopener noreferrer"&gt;Xcode&lt;/a&gt;, which has the SDK included.&lt;/p&gt;

&lt;p&gt;Another example that can be mentioned is Linux, a mostly POSIX-compliant system. The implementation is the Native POSIX Threads Library (NPTL), which is a part of &lt;a href="https://www.gnu.org/software/libc/" rel="noopener noreferrer"&gt;The GNU C Library (glibc)&lt;/a&gt;. By obtaining GCC, you have an implementation, the header file, the compiler and the linker. Most Linux distros have the package &lt;code&gt;gcc&lt;/code&gt;. Follow your package manager manual to install it. For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;On Ubuntu, run &lt;code&gt;sudo apt-get install gcc&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;On Fedora, run &lt;code&gt;sudo dnf install -y gcc&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Grab the tools you need for your system, and take a look a the following &lt;a href="https://gist.github.com/khoa-io/e0b388ccfcc6f5b450ebbc4a791c2cdc" rel="noopener noreferrer"&gt;hello world example&lt;/a&gt;:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
.

&lt;p&gt;In which:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;helloWorld()&lt;/code&gt;: The function which shall be executed in the new thread.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;pthread_t&lt;/code&gt;: Thread identifier type, needed by most Pthreads APIs.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;pthread_create()&lt;/code&gt;: Creates and launches a new thread of execution. It also gives us an identifier and then returns an exit code.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;pthread_join()&lt;/code&gt;: The calling thread waits for the thread (specified by an identifier) to terminate.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To compile the example, you can use either &lt;code&gt;clang&lt;/code&gt; or &lt;code&gt;gcc&lt;/code&gt;. You may want to use the system default C Compiler by using the &lt;code&gt;cc&lt;/code&gt; command line utility:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
.

&lt;p&gt;As you may notice, I use some compiler options:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;-Wall -Wextra&lt;/code&gt;: The compiler produces more warnings about potential defects.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-Werror&lt;/code&gt;: The compiler treats all warnings as errors.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The combination of these options prevents you from making mistakes in the early stage. You may find them annoying sometimes. But paying attention to the warnings, thinking about their meaning, and thinking of how to avoid them will make you a better programmer.&lt;/p&gt;

&lt;p&gt;You may also notice that I don't ignore the exit codes that &lt;code&gt;pthread_create()&lt;/code&gt; and &lt;code&gt;pthread_join()&lt;/code&gt; return. In the example, the compiled program will return &lt;code&gt;0&lt;/code&gt; if the execution is successful, or an error code that POSIX defines in &lt;code&gt;errno.h&lt;/code&gt;. Experience users can look up the error code in &lt;code&gt;errno.h&lt;/code&gt; and find out what was wrong. That's very basic error handling. Remember, don't skip error handling. The more time you spend on writing error handling code, the less time you will have to spend on defect investigation. Defects on multithreaded code are very hard to find, so keep that in mind.&lt;/p&gt;

&lt;p&gt;That's the hello world of Pthreads. Next, I will introduce the C++ Thread.&lt;/p&gt;

&lt;h2&gt;
  
  
  C++ Thread
&lt;/h2&gt;

&lt;p&gt;Unlike the C Standard, &lt;em&gt;modern&lt;/em&gt; C++ Standards (since C++11) specify built-in supports for multithreading in the C++ Standard Library. These supports include the classes for managing threads, protecting shared data, synchronizing between threads, and low-level atomic operations, which makes coding multithread easier and safer. I think the best part is now you can have a consistent C++ programming experience while working with threads, regardless of the runtime system.&lt;/p&gt;

&lt;p&gt;Note that the C++ Standard is a technical specification. The actual implementation is done by compiler vendors, like GCC, LLVM, Microsoft C++, etc. On a POSIX system, C++ Thread is implemented using Pthreads. In this case, C++ Thread is a wrapper that wraps underlying POSIX APIs. This action comes with a cost, called the &lt;em&gt;abstraction penalty&lt;/em&gt;. In most cases, developers are happy to pay this cost. But if the utmost in performance is what you're after, then consider writing your code in C, with the only support for multithreading being either platform-specific or Pthreads. Other than that, if you're programming in C++, I'd recommend C++ Thread.&lt;/p&gt;

&lt;p&gt;You can start writing the hello world example with a C++ compiler that supports C++11 or later:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
.

&lt;p&gt;In which:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The class &lt;code&gt;std::thread&lt;/code&gt; is declared in the header file &lt;code&gt;thread&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The function which will be executed in the new thread is defined in the class &lt;code&gt;BackgroundTask&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The new thread will be created and launched by the constructor of &lt;code&gt;std::thread&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;std::thread::join()&lt;/code&gt; member function causes the calling thread waits for &lt;code&gt;myThread&lt;/code&gt; to terminate.&lt;/li&gt;
&lt;li&gt;Instead of C-style error code handling, C++ provides exception handling mechanism.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Both the constructor and the member function &lt;code&gt;join()&lt;/code&gt; of the class &lt;code&gt;std::thread&lt;/code&gt; can throw &lt;code&gt;std::system_error&lt;/code&gt; exception. Although C++ compilers don't require catching every thrown exception, I think it's a good practice that you understand every exception a function can throw, and handle them properly. In the example, I just print out the information about the exception that &lt;code&gt;std::system_error::what()&lt;/code&gt; provides, and returns &lt;code&gt;-1&lt;/code&gt;, indicating a failure.&lt;/p&gt;

&lt;p&gt;To compile the example, refer to the following command line for your compiler (&lt;code&gt;g++&lt;/code&gt; or &lt;code&gt;clang++&lt;/code&gt;):&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
.

&lt;p&gt;The meaning of the compiler options is the same as &lt;code&gt;gcc&lt;/code&gt; and &lt;code&gt;clang&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So, that's hello world to Pthreads and C++ Thread. In the next post, I will introduce to you how to launch a thread, using Pthreads and C++ Thread.&lt;/p&gt;

</description>
      <category>posix</category>
      <category>c</category>
      <category>cpp</category>
    </item>
    <item>
      <title>Launching a thread in C/C++</title>
      <dc:creator>Hoàng Văn Khoa</dc:creator>
      <pubDate>Fri, 23 Sep 2022 10:47:37 +0000</pubDate>
      <link>https://dev.to/khoa/launching-a-thread-in-cc-3cpi</link>
      <guid>https://dev.to/khoa/launching-a-thread-in-cc-3cpi</guid>
      <description>&lt;p&gt;&lt;em&gt;Note: Since I decided to write a series about multithreading in C/C++, I’m going to rewrite this article.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Talking about multithreading in C/C++, there are two &lt;em&gt;standards&lt;/em&gt; that specify the APIs for multithreading:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://pubs.opengroup.org/onlinepubs/9699919799.2018edition/" rel="noopener noreferrer"&gt;POSIX Threads&lt;/a&gt;: commonly known as Pthreads, is a part of the POSIX Standard.&lt;/li&gt;
&lt;li&gt;ISO C++ (since &lt;a href="https://www.iso.org/standard/50372.html" rel="noopener noreferrer"&gt;C++11&lt;/a&gt;): yes, C++ now has &lt;em&gt;built-in&lt;/em&gt; support for multithreading, I'm going to call it &lt;em&gt;C++ Thread&lt;/em&gt; for short.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In C, you can use Pthreads. In C++, you have a choice between Pthreads and C++ Thread. The &lt;em&gt;implementations&lt;/em&gt; for the two standards can be found on most major systems, from PC (macOS, FreeBSD, Linux, ...) to automotive (AutoSAR Adaptive Platform, ...). If you're an Android platform developer and working on AOSP, you might have been working with them already. If your system doesn't support Pthreads, you have an option to use platform-specific thread APIs in C, or you can write in C++ and do multithreading using C++ Thread.&lt;/p&gt;

&lt;p&gt;In this post, I'm going to show you some basic examples of both standards to create and launch a new thread. I also remind you to handle the error which might happen. I assumed that you already know how to compile these examples. If you don't, leave a comment.&lt;/p&gt;

&lt;h2&gt;
  
  
  Launching a thread using Pthreads
&lt;/h2&gt;

&lt;p&gt;We call the function &lt;code&gt;pthread_create&lt;/code&gt; to create a new POSIX thread and launch it. The declaration of the function is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;pthread.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;pthread_create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pthread_t&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="kr"&gt;thread&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;pthread_attr_t&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;attr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;start_routine&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;arg&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By presenting it this way, I mean that &lt;code&gt;pthread_create&lt;/code&gt; is declared in the header file &lt;code&gt;pthread.h&lt;/code&gt; and it needs 4 parameters to create a new thread:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;pthread_t *thread&lt;/code&gt; is the pointer to the thread identifier which we are about to receive, so it cannot be &lt;code&gt;NULL&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;const pthread_attr_t *attr&lt;/code&gt; is the attribute object. If it's &lt;code&gt;NULL&lt;/code&gt;, the default attributes are used. The newly created thread will have these attributes set by &lt;code&gt;pthread_create&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;void *(*start_routine)(void *)&lt;/code&gt; is the function to be executed in the new thread, so it cannot be &lt;code&gt;NULL&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;void *arg&lt;/code&gt; allows us to pass an argument to the &lt;code&gt;start_routine&lt;/code&gt; function. Because its type is declared as &lt;code&gt;void *&lt;/code&gt;, you can pass any value which has the same size as &lt;code&gt;size_t&lt;/code&gt;, or a pointer to any type of object. If &lt;code&gt;arg&lt;/code&gt; is a pointer, you must make sure the data that &lt;code&gt;arg&lt;/code&gt; points to remains valid during the execution of the &lt;code&gt;start_routine&lt;/code&gt; function.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;pthread_create&lt;/code&gt; returns &lt;code&gt;0&lt;/code&gt; if it successfully created a new thread. Otherwise, an error number will be returned to indicate the error. These numbers are defined in &lt;code&gt;errno.h&lt;/code&gt;. The error numbers that &lt;code&gt;pthread_create&lt;/code&gt; can return are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;EAGAIN&lt;/code&gt;: The system lacked the necessary resources to create a new thread, or the process reaches its quota (&lt;code&gt;PTHREAD_THREADS_MAX&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;EPERM&lt;/code&gt;: The caller does not have permission to set the required scheduling parameters or scheduling policy.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;EINVAL&lt;/code&gt;: Invalid attribute(s).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It's bad practice if you don't check and handle the error number that &lt;code&gt;pthread_create&lt;/code&gt; returns. But for the sake of complexity, in the following example, I'd leave the error handling for the caller of the &lt;code&gt;launchMyThread()&lt;/code&gt; function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;pthread.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="n"&gt;pthread_t&lt;/span&gt; &lt;span class="n"&gt;myThread&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="cm"&gt;/**
 * `myThread`'s routine
 */&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nf"&gt;doSomething&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;arg&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="n"&gt;arg&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="cm"&gt;/* To avoid a compiler warning that said argument `arg` is unused */&lt;/span&gt;
  &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"[%s:%d %s]&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;__FILE__&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;__LINE__&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;__FUNCTION__&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="cm"&gt;/* In this example, `doSomething` just returns `0` to indicate a successful execution */&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="cm"&gt;/**
 * Launch myThread
 * @return 0        : success
 *         non-zero : failure, see errno.h
 */&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;launchMyThread&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="cm"&gt;/* `myThread`'s initial attributes */&lt;/span&gt;
  &lt;span class="n"&gt;pthread_attr_t&lt;/span&gt; &lt;span class="n"&gt;attr&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;exitCode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="n"&gt;pthread_attr_init&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;attr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="cm"&gt;/* one of `myThread`'s attributes will be joinable, meaning the calling thread must wait for it to return */&lt;/span&gt;
  &lt;span class="n"&gt;pthread_attr_setdetachstate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;attr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;PTHREAD_CREATE_JOINABLE&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="n"&gt;exitCode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pthread_create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;myThread&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;attr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;doSomething&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="cm"&gt;/* the caller must handle the error */&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;exitCode&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are two Pthreads functions that I have yet to mention: &lt;code&gt;pthread_attr_init()&lt;/code&gt;, &lt;code&gt;pthread_attr_setdetachstate&lt;/code&gt;. They will be covered in an upcoming post.&lt;br&gt;
Now, move on to the next section to see how to achieve the same using C++ Thread.&lt;/p&gt;
&lt;h2&gt;
  
  
  Launching a thread using C++ Thread
&lt;/h2&gt;

&lt;p&gt;C++ provides the &lt;code&gt;std::thread&lt;/code&gt; class (defined in &lt;code&gt;&amp;lt;thread&amp;gt;&lt;/code&gt;) to manage the thread of execution. To create a thread, we construct an object of the &lt;code&gt;std::thread&lt;/code&gt; class. The constructors look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kr"&gt;thread&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;noexcept&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Default constructor&lt;/span&gt;
&lt;span class="k"&gt;template&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;typename&lt;/span&gt; &lt;span class="nc"&gt;Callable&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="k"&gt;typename&lt;/span&gt; &lt;span class="nc"&gt;Args&lt;/span&gt;&lt;span class="p"&gt;...&amp;gt;&lt;/span&gt;
&lt;span class="k"&gt;explicit&lt;/span&gt; &lt;span class="nf"&gt;thread&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Callable&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;func&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Args&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Second constructor&lt;/span&gt;
&lt;span class="kr"&gt;thread&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kr"&gt;thread&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;delete&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// No copy constructor&lt;/span&gt;
&lt;span class="kr"&gt;thread&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kr"&gt;thread&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;noexcept&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Move constructor&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first constructor constructs an object, but it's not associated with any thread of execution. It is there for constructing an object without anything to execute.&lt;br&gt;
The second one constructs an object that's associated with a thread of execution because it has enough information to do so:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Callable&amp;amp;&amp;amp; func&lt;/code&gt;: an &lt;a href="https://learn.microsoft.com/en-us/cpp/standard-library/is-invocable-classes" rel="noopener noreferrer"&gt;callable object&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Args&amp;amp;&amp;amp;... args&lt;/code&gt;: some arguments. They're optional. You may pass no argument.
&lt;code&gt;func&lt;/code&gt; and each element of &lt;code&gt;args&lt;/code&gt; must be &lt;a href="https://learn.microsoft.com/en-us/cpp/standard-library/is-move-constructible-class" rel="noopener noreferrer"&gt;&lt;code&gt;MoveConstructible&lt;/code&gt;&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
A &lt;code&gt;MoveConstructible&lt;/code&gt; (since C++11) object is a object that can be constructed from a &lt;a href="https://learn.microsoft.com/en-us/cpp/cpp/lvalues-and-rvalues-visual-cpp" rel="noopener noreferrer"&gt;rvalue&lt;/a&gt; argument.&lt;br&gt;&lt;br&gt;
The &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt; operator, in this case, is the &lt;a href="https://learn.microsoft.com/en-us/cpp/cpp/rvalue-reference-declarator-amp-amp" rel="noopener noreferrer"&gt;&lt;em&gt;rvalue reference declarator&lt;/em&gt;&lt;/a&gt;.&lt;br&gt;&lt;br&gt;
But what exactly are they? What do they do here? Well, I will cover this in another post called &lt;strong&gt;Move Semantics in C++&lt;/strong&gt;.  &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's take a look at the following example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;system_error&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;thread&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BackgroundTask&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nl"&gt;public:&lt;/span&gt;
  &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;doSomething&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;doAnotherThing&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="k"&gt;operator&lt;/span&gt;&lt;span class="p"&gt;()()&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;doSomething&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;doAnotherThing&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;launchMyThread&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;BackgroundTask&lt;/span&gt; &lt;span class="n"&gt;task&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="c1"&gt;// `task` is copied into `myThread`'s storage, so make sure the copy behaves&lt;/span&gt;
    &lt;span class="c1"&gt;// equivalently to the original&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kr"&gt;thread&lt;/span&gt; &lt;span class="n"&gt;myThread&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;task&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;system_error&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;what&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is not the simplest example of launching a thread using C++ Thread. But it sure shows some differences between C-style code and C++ style. First, it constructs a &lt;code&gt;BackgroundTask&lt;/code&gt; object, which is callable because the class overrides the function call operator &lt;code&gt;()&lt;/code&gt;. Of course, you can also pass a function, or a lambda expression, anything that is callable. The callable object is copied into the storage belonging to the newly created thread of execution and invoked from there. Therefore, you need to ensure that the copy behaves equivalently to the original, or the result may not be what you expect.&lt;/p&gt;

&lt;p&gt;The example also shows that is optional to pass arguments. But if you need to, you can pass more than one argument, unlike &lt;code&gt;pthread_create&lt;/code&gt;, which only allows you to pass only one argument. To receive arguments, the class &lt;code&gt;BackgroundTask&lt;/code&gt; needs to override the function call operator with parameter(s):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;string&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;system_error&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;thread&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BackgroundTask&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nl"&gt;public:&lt;/span&gt;
  &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="k"&gt;operator&lt;/span&gt;&lt;span class="p"&gt;()(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string_view&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"Hello "&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;launchMyThread&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;BackgroundTask&lt;/span&gt; &lt;span class="n"&gt;task&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kr"&gt;thread&lt;/span&gt; &lt;span class="n"&gt;myThread&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;task&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Multithreading World"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;system_error&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;what&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It's also bad practice if you don't do the error handling, in this case, exception handling. The second constructor throws a &lt;code&gt;std::system_error&lt;/code&gt; exception if it's unable to start the new thread, or if there's an exception while copying &lt;code&gt;func&lt;/code&gt; or &lt;code&gt;args&lt;/code&gt; into local storage.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;📝 Never forget to handle errors or exceptions. Spend more time writing error/exception handling and you will spend less time investigating bugs.  &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So that's how you create and launch a new thread of execution, using Pthreads/C++ Thread. There's more about multithreading in C/C++. Stay tuned for updates!&lt;/p&gt;

</description>
      <category>posix</category>
      <category>c</category>
      <category>cpp</category>
    </item>
  </channel>
</rss>
