{"id":10357,"date":"2024-03-14T11:56:56","date_gmt":"2024-03-14T11:56:56","guid":{"rendered":"https:\/\/www.silicloud.com\/blog\/how-to-use-pthread_create-in-linux\/"},"modified":"2025-08-03T22:31:31","modified_gmt":"2025-08-03T22:31:31","slug":"how-to-use-pthread_create-in-linux","status":"publish","type":"post","link":"https:\/\/www.silicloud.com\/blog\/how-to-use-pthread_create-in-linux\/","title":{"rendered":"pthread_create Linux Explained"},"content":{"rendered":"<p>In Linux, the pthread_create function is used to create a new thread. The syntax is as follows:<\/p>\n<pre class=\"post-pre\"><code><span class=\"hljs-type\">int<\/span> <span class=\"hljs-title function_\">pthread_create<\/span><span class=\"hljs-params\">(<span class=\"hljs-type\">pthread_t<\/span> *thread, <span class=\"hljs-type\">const<\/span> <span class=\"hljs-type\">pthread_attr_t<\/span> *attr, <span class=\"hljs-type\">void<\/span> *(*start_routine) (<span class=\"hljs-type\">void<\/span> *), <span class=\"hljs-type\">void<\/span> *arg)<\/span>;\r\n<\/code><\/pre>\n<p>The meanings of the parameters are as follows:<\/p>\n<ol>\n<li>thread: used for storing the ID of a new thread.<\/li>\n<li>Attribute: Thread attribute, typically set to NULL.<\/li>\n<li>start_routine: The entry function of a thread, which is the starting address where a new thread begins execution.<\/li>\n<li>arg: The argument passed to the start_routine function.<\/li>\n<\/ol>\n<p>Below is a simple example that demonstrates how to use the pthread_create function to create a new thread.<\/p>\n<pre class=\"post-pre\"><code><span class=\"hljs-meta\">#<span class=\"hljs-keyword\">include<\/span> <span class=\"hljs-string\">&lt;stdio.h&gt;<\/span><\/span>\r\n<span class=\"hljs-meta\">#<span class=\"hljs-keyword\">include<\/span> <span class=\"hljs-string\">&lt;pthread.h&gt;<\/span><\/span>\r\n\r\n<span class=\"hljs-type\">void<\/span> *<span class=\"hljs-title function_\">myThread<\/span><span class=\"hljs-params\">(<span class=\"hljs-type\">void<\/span> *arg)<\/span> {\r\n    <span class=\"hljs-type\">int<\/span> *num = (<span class=\"hljs-type\">int<\/span> *)arg;\r\n    <span class=\"hljs-built_in\">printf<\/span>(<span class=\"hljs-string\">\"Thread ID: %ld, Number: %d\\n\"<\/span>, pthread_self(), *num);\r\n    <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-literal\">NULL<\/span>;\r\n}\r\n\r\n<span class=\"hljs-type\">int<\/span> <span class=\"hljs-title function_\">main<\/span><span class=\"hljs-params\">()<\/span> {\r\n    <span class=\"hljs-type\">pthread_t<\/span> tid;\r\n    <span class=\"hljs-type\">int<\/span> number = <span class=\"hljs-number\">10<\/span>;\r\n    \r\n    pthread_create(&amp;tid, <span class=\"hljs-literal\">NULL<\/span>, myThread, &amp;number);\r\n    \r\n    pthread_join(tid, <span class=\"hljs-literal\">NULL<\/span>);\r\n    \r\n    <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-number\">0<\/span>;\r\n}\r\n<\/code><\/pre>\n<p>In the example above, a thread function named myThread is first defined, then a new thread is created in the main function main using the pthread_create function and parameter number is passed to the new thread. Finally, the pthread_join function is used to wait for the new thread to finish execution.<\/p>\n<p>It is important to note that when creating a new thread using the pthread_create function, you need to link the pthread library. You can compile the program using the following command:<\/p>\n<pre class=\"post-pre\"><code>gcc -o myprogram myprogram.c -lpthread\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In Linux, the pthread_create function is used to create a new thread. The syntax is as follows: int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg); The meanings of the parameters are as follows: thread: used for storing the ID of a new thread. Attribute: Thread attribute, typically set to NULL. start_routine: [&hellip;]<\/p>\n","protected":false},"author":12,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[381,12897,492,12884,12883],"class_list":["post-10357","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-c-programming","tag-linux-threading","tag-multithreading","tag-posix-threads","tag-pthread_create"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.5 (Yoast SEO v21.5) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>pthread_create Linux Explained - Blog - Silicon Cloud<\/title>\n<meta name=\"description\" content=\"Learn pthread_create syntax and parameters for Linux thread creation. Complete guide with examples.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.silicloud.com\/blog\/how-to-use-pthread_create-in-linux\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"pthread_create Linux Explained\" \/>\n<meta property=\"og:description\" content=\"Learn pthread_create syntax and parameters for Linux thread creation. Complete guide with examples.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.silicloud.com\/blog\/how-to-use-pthread_create-in-linux\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog - Silicon Cloud\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-03-14T11:56:56+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-08-03T22:31:31+00:00\" \/>\n<meta name=\"author\" content=\"Liam\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:site\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Liam\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-use-pthread_create-in-linux\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-use-pthread_create-in-linux\/\"},\"author\":{\"name\":\"Liam\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/23786905eb7b377f45ddb01c17da7671\"},\"headline\":\"pthread_create Linux Explained\",\"datePublished\":\"2024-03-14T11:56:56+00:00\",\"dateModified\":\"2025-08-03T22:31:31+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-use-pthread_create-in-linux\/\"},\"wordCount\":179,\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"keywords\":[\"C++ Programming\",\"Linux threading\",\"multithreading\",\"POSIX threads\",\"pthread_create\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-use-pthread_create-in-linux\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/how-to-use-pthread_create-in-linux\/\",\"name\":\"pthread_create Linux Explained - Blog - Silicon Cloud\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\"},\"datePublished\":\"2024-03-14T11:56:56+00:00\",\"dateModified\":\"2025-08-03T22:31:31+00:00\",\"description\":\"Learn pthread_create syntax and parameters for Linux thread creation. Complete guide with examples.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-use-pthread_create-in-linux\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.silicloud.com\/blog\/how-to-use-pthread_create-in-linux\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-use-pthread_create-in-linux\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.silicloud.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"pthread_create Linux Explained\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"name\":\"Silicon Cloud Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\",\"name\":\"Silicon Cloud Blog\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"contentUrl\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"width\":1024,\"height\":1024,\"caption\":\"Silicon Cloud Blog\"},\"image\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\",\"https:\/\/twitter.com\/SiliCloudGlobal\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/23786905eb7b377f45ddb01c17da7671\",\"name\":\"Liam\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/8d37ed3e7f770dde8bf069ba0b4298688028c3abaacf1131742fc1352d174ebd?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/8d37ed3e7f770dde8bf069ba0b4298688028c3abaacf1131742fc1352d174ebd?s=96&d=mm&r=g\",\"caption\":\"Liam\"},\"sameAs\":[\"http:\/\/Wilson\"],\"url\":\"https:\/\/www.silicloud.com\/blog\/author\/liamwilson\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"pthread_create Linux Explained - Blog - Silicon Cloud","description":"Learn pthread_create syntax and parameters for Linux thread creation. Complete guide with examples.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.silicloud.com\/blog\/how-to-use-pthread_create-in-linux\/","og_locale":"en_US","og_type":"article","og_title":"pthread_create Linux Explained","og_description":"Learn pthread_create syntax and parameters for Linux thread creation. Complete guide with examples.","og_url":"https:\/\/www.silicloud.com\/blog\/how-to-use-pthread_create-in-linux\/","og_site_name":"Blog - Silicon Cloud","article_publisher":"https:\/\/www.facebook.com\/SiliCloudGlobal\/","article_published_time":"2024-03-14T11:56:56+00:00","article_modified_time":"2025-08-03T22:31:31+00:00","author":"Liam","twitter_card":"summary_large_image","twitter_creator":"@SiliCloudGlobal","twitter_site":"@SiliCloudGlobal","twitter_misc":{"Written by":"Liam","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.silicloud.com\/blog\/how-to-use-pthread_create-in-linux\/#article","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/how-to-use-pthread_create-in-linux\/"},"author":{"name":"Liam","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/23786905eb7b377f45ddb01c17da7671"},"headline":"pthread_create Linux Explained","datePublished":"2024-03-14T11:56:56+00:00","dateModified":"2025-08-03T22:31:31+00:00","mainEntityOfPage":{"@id":"https:\/\/www.silicloud.com\/blog\/how-to-use-pthread_create-in-linux\/"},"wordCount":179,"publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"keywords":["C++ Programming","Linux threading","multithreading","POSIX threads","pthread_create"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.silicloud.com\/blog\/how-to-use-pthread_create-in-linux\/","url":"https:\/\/www.silicloud.com\/blog\/how-to-use-pthread_create-in-linux\/","name":"pthread_create Linux Explained - Blog - Silicon Cloud","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/#website"},"datePublished":"2024-03-14T11:56:56+00:00","dateModified":"2025-08-03T22:31:31+00:00","description":"Learn pthread_create syntax and parameters for Linux thread creation. Complete guide with examples.","breadcrumb":{"@id":"https:\/\/www.silicloud.com\/blog\/how-to-use-pthread_create-in-linux\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.silicloud.com\/blog\/how-to-use-pthread_create-in-linux\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.silicloud.com\/blog\/how-to-use-pthread_create-in-linux\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.silicloud.com\/blog\/"},{"@type":"ListItem","position":2,"name":"pthread_create Linux Explained"}]},{"@type":"WebSite","@id":"https:\/\/www.silicloud.com\/blog\/#website","url":"https:\/\/www.silicloud.com\/blog\/","name":"Silicon Cloud Blog","description":"","publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.silicloud.com\/blog\/#organization","name":"Silicon Cloud Blog","url":"https:\/\/www.silicloud.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","contentUrl":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","width":1024,"height":1024,"caption":"Silicon Cloud Blog"},"image":{"@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/SiliCloudGlobal\/","https:\/\/twitter.com\/SiliCloudGlobal"]},{"@type":"Person","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/23786905eb7b377f45ddb01c17da7671","name":"Liam","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/8d37ed3e7f770dde8bf069ba0b4298688028c3abaacf1131742fc1352d174ebd?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/8d37ed3e7f770dde8bf069ba0b4298688028c3abaacf1131742fc1352d174ebd?s=96&d=mm&r=g","caption":"Liam"},"sameAs":["http:\/\/Wilson"],"url":"https:\/\/www.silicloud.com\/blog\/author\/liamwilson\/"}]}},"_links":{"self":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/10357","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/comments?post=10357"}],"version-history":[{"count":2,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/10357\/revisions"}],"predecessor-version":[{"id":154113,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/10357\/revisions\/154113"}],"wp:attachment":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/media?parent=10357"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/categories?post=10357"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/tags?post=10357"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}