{"id":3711,"date":"2024-03-06T11:42:57","date_gmt":"2024-03-06T11:42:57","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=3711"},"modified":"2024-03-06T11:42:57","modified_gmt":"2024-03-06T11:42:57","slug":"multithreading-in-c","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/multithreading-in-c\/","title":{"rendered":"Multithreading In C"},"content":{"rendered":"\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\" id=\"rank-math-toc\"><p>Table of Contents<\/p><nav><ul><li ><a href=\"#multithreading-in-c\">Multithreading In C<\/a><\/li><li ><a href=\"#what-is-a-thread\">What is a Thread?\u00a0<\/a><\/li><li ><a href=\"#what-are-the-differences-between-process-and-thread\">What are the differences between process and thread?\u00a0<\/a><\/li><li ><a href=\"#why-multithreading\">Why Multithreading?<\/a><\/li><li ><a href=\"#can-we-write-multithreading-muprograms-in-c\">Can we write multithreading Muprograms in C?<\/a><\/li><li ><a href=\"#c-program-to-illustrate-the-use-of-pthread-basic-functions\">C Program to illustrate the use of pthread basic functions<\/a><\/li><li ><a href=\"#a-c-program-to-show-multiple-threads-with-the-global-and-static-variable\">A C Program to show multiple threads with the global and static  variable<\/a><\/li><li ><a href=\"#faq-multithreading-in-c\">FAQ- Multithreading In C<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"multithreading-in-c\">Multithreading In C<\/h2>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-a-thread\"><strong>What is a Thread?&nbsp;<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A thread is like a mini-sequence of actions inside a bigger task. Threads are sometimes called lightweight processes because they share some similarities with processes.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-are-the-differences-between-process-and-thread\"><strong>What are the differences between process and thread?<\/strong>&nbsp;<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Threads aren&#8217;t independent like processes; they share code, data, and OS resources. However, each thread has its own program counter, registers, and stack space, much like processes.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"why-multithreading\"><strong>Why Multithreading?<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Threads are a helpful way to make applications work better together. Think of them like different tasks happening at the same time. For example, in a web browser, each open tab can be a separate task or thread. Programs like Microsoft Word also use threads to do different jobs, like formatting text or handling inputs.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Threads are faster than processes for a few reasons:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1) Creating threads is quick.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2) Switching between threads is fast.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">3) Stopping threads is easy.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">4) Threads can talk to each other quickly.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"can-we-write-multithreading-muprograms-in-c\">Can we write multithreading Muprograms in C?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Multithreading won&#8217;t be supported by the language standard. POSIX Threads (or Pthreads) is considered as a POSIX standard for threads. Moreover, the execution of pthread will be available with gcc compiler.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"c-program-to-illustrate-the-use-of-pthread-basic-functions\">C Program to illustrate the use of pthread basic functions<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The program given below can only compile with the C compilers along with the pthread library.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>C\n#include &lt;stdio.h&gt; \n#include &lt;stdlib.h&gt; \n#include &lt;unistd.h&gt;  \/\/Header file for sleep(). man 3 sleep for details. \n#include &lt;pthread.h&gt; \n  \n\/\/ A normal C function that is executed as a thread  \n\/\/ when its name is specified in pthread_create() \nvoid *myThreadFun(void *vargp) \n{ \n    sleep(1); \n    printf(\"Printing Skillvertex from Thread \\n\"); \n    return NULL; \n} \n   \nint main() \n{ \n    pthread_t thread_id; \n    printf(\"Before Thread\\n\"); \n    pthread_create(&amp;thread_id, NULL, myThreadFun, NULL); \n    pthread_join(thread_id, NULL); \n    printf(\"After Thread\\n\"); \n    exit(0); \n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In the main() function, we create a variable called thread_id, which is of type pthread_t\u2014an integer used to identify the thread. After declaring thread_id, we use the pthread_create() function to make a new thread.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The pthread_create() function takes 4 arguments:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>The first argument is a pointer to thread_id, and this function sets its value.<\/li>\n\n\n\n<li>The second argument specifies attributes. If it&#8217;s NULL, default attributes will be used.<\/li>\n\n\n\n<li>The third argument is the name of the function to be executed for the new thread.<\/li>\n\n\n\n<li>The fourth argument is used to pass arguments to the function, in this case, myThreadFun.<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">The pthread_join() function is like wait() for processes. It makes the calling thread wait until the specified thread, identified by the first argument, finishes its work.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>How to compile above program?<\/strong>&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Inorder to compile a multithreaded program with the help of gcc, we have to connect with the threads library. The command given below is used to compile the program.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>gfg@ubuntu:~\/$ gcc multithread.c -lpthread\ngfg@ubuntu:~\/$ .\/a.out\nBefore Thread\nPrinting GeeksQuiz from Thread \nAfter Thread\ngfg@ubuntu:~\/$ <\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"a-c-program-to-show-multiple-threads-with-the-global-and-static-variable\">A C Program to show multiple threads with the global and static  variable<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">We know that the global and static variables can share data segments. Global and static variables will be stored in the data segment.The example given below will illustrate the same.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt; \n#include &lt;stdlib.h&gt; \n#include &lt;unistd.h&gt; \n#include &lt;pthread.h&gt; \n  \n\/\/ Let us create a global variable to change it in threads \nint g = 0; \n  \n\/\/ The function to be executed by all threads \nvoid *myThreadFun(void *vargp) \n{ \n    \/\/ Store the value argument passed to this thread \n    int *myid = (int *)vargp; \n  \n    \/\/ Let us create a static variable to observe its changes \n    static int s = 0; \n  \n    \/\/ Change static and global variables \n    ++s; ++g; \n  \n    \/\/ Print the argument, static and global variables \n    printf(\"Thread ID: %d, Static: %d, Global: %d\\n\", *myid, ++s, ++g); \n} \nint main() \n{ \n    int i; \n    pthread_t tid; \n  \n    \/\/ Let us create three threads \n    for (i = 0; i &lt; 3; i++) \n        pthread_create(&amp;tid, NULL, myThreadFun, (void *)&amp;tid); \n  \n    pthread_exit(NULL); \n    return 0; \n}\n  <\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>gfg@ubuntu:~\/$ gcc multithread.c -lpthread\ngfg@ubuntu:~\/$ .\/a.out\nThread ID: 3, Static: 2, Global: 2\nThread ID: 3, Static: 4, Global: 4\nThread ID: 3, Static: 6, Global: 6\ngfg@ubuntu:~\/$ <\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Please note that the above example is simple and used to illustrate how threads work. Accessing a global variable in a thread is usually not recommended. What if thread 2 has priority over thread 1 and thread 1 needs to change the variable? In real-world scenarios, if multiple threads need to access a global variable, it&#8217;s better to use a mutex for safe access.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"faq-multithreading-in-c\">FAQ- Multithreading In C<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1700732287181\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1. What is multithreading in C language?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. Multithreading in C means using multiple threads within a single process, where each thread serves a distinct function. It allows concurrent operation, enabling multiple tasks to be executed simultaneously. Additionally, multithreading helps minimize CPU resource consumption.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1700732295928\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2. Does C allow multithreading?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. The Microsoft C\/C++ compiler (MSVC) offers support for building multithreaded applications. It&#8217;s beneficial to use more than one thread when your application has to execute resource-intensive operations that might otherwise make the user interface unresponsive.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1700732302098\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3. What is multithreading also called?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans.Concurrency is frequently used interchangeably with multithreading. Multitasking enables multiple threads to run simultaneously.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Multithreading In C What is a Thread?&nbsp; A thread is like a mini-sequence of actions inside a bigger task. Threads are sometimes called lightweight processes because they share some similarities with processes. What are the differences between process and thread?&nbsp; Threads aren&#8217;t independent like processes; they share code, data, and OS resources. However, each thread &#8230; <a title=\"Multithreading In C\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/multithreading-in-c\/\" aria-label=\"More on Multithreading In C\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":5461,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[614],"class_list":["post-3711","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-programming","tag-multithreading-in-c","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"_links":{"self":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/3711","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/comments?post=3711"}],"version-history":[{"count":10,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/3711\/revisions"}],"predecessor-version":[{"id":7959,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/3711\/revisions\/7959"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/5461"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=3711"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=3711"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=3711"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}