{"id":4618,"date":"2019-01-08T00:42:47","date_gmt":"2019-01-08T08:42:47","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4618"},"modified":"2019-01-08T00:45:47","modified_gmt":"2019-01-08T08:45:47","slug":"multithreading-and-multiprocessing-parallel-computing-1","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/cs\/multithreading-and-multiprocessing-parallel-computing-1\/","title":{"rendered":"Multithreading and Multiprocessing &#8211; Parallel Computing 1"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Multithreading<\/h2>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n#include <stdio.h>\n\n#include <thread>\n\nint a = 1;\n\nvoid run(int s) {\n  a += s;\n}\n\nint main(int argc, char** argv) {\n  std::thread thread1(run, 2);\n  std::thread thread2(run, 5);\n  thread1.join(); \n  thread2.join();\n  printf(\"%d\\n\", a);\n  return 0;\n}\n\/\/ Output:\n\/\/ 8\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Multiprocessing<\/h2>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n#include <stdio.h>\n#include <sys\/types.h>\n#include <unistd.h>\n\nint a = 1;\nvoid run(bool child) { \n  a += child ? 2 : 1;\n}\n\nint main(int argc, char** argv) {\n  a += 1;\n  pid_t pid = fork();\n  run(pid == 0);\n  printf(\"%d\\n\", a);\n  return 0;\n}\n\n\/\/ Output:\n\/\/ 3\n\/\/ 4\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Multithreading #include #include int a = 1; void run(int s) { a += s; } int main(int argc, char** argv) { std::thread thread1(run, 2); std::thread&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[452],"tags":[],"class_list":["post-4618","post","type-post","status-publish","format-standard","hentry","category-cs","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4618","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=4618"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4618\/revisions"}],"predecessor-version":[{"id":4621,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4618\/revisions\/4621"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4618"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4618"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4618"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}