Skip to Content
21st Century C, 2nd Edition
book

21st Century C, 2nd Edition

by Ben Klemens
May 2025
Intermediate to advanced
408 pages
5h 55m
Japanese
O'Reilly Media, Inc.
Content preview from 21st Century C, 2nd Edition

第12章. 並列スレッド

この作品はAIを使って翻訳されている。ご意見、ご感想をお待ちしている:translation-feedback@oreilly.com

今夜は99回転だ。

グリーン・デイ、"99 Revolutions"

ここ数年で販売されたほぼすべてのコンピューターは、多くの電話機でさえもマルチコアである。キーボードとモニターを備えたコンピューターでこの文章を読み取っているのなら、自分のコンピューターがいくつのコアを搭載しているかは、キーボードとモニターを経由して知ることができるかもしれない:

  • Linux:grep cores /proc/cpuinfo

  • マックだ:sysctl hw.logicalcpu

  • Cygwinだ:env | grep NUMBER_OF_PROCESSORS

シングルスレッド・プログラムでは、ハードウェア・メーカーが与えてくれたリソースをフルに活用できない。幸いなことに、プログラムを並行並列スレッドを持つものに変えるのにそれほど時間はかからない。この章では、以下のことを説明する:

  • 並行Cコードを書くために存在するいくつかの標準と仕様の概要を簡単に説明する。

  • for 、ループをマルチスレッド化するOpenMPコードの一行。

  • OpenMPまたはpthreadsでコンパイルするために必要なコンパイラー・フラグに関する注意事項

  • 魔法の一行を使っても大丈夫な場合について、いくつか考えてみた。

  • map-reduceを実装するには、その1行を別の節で拡張する必要がある。

  • GUIベースのプログラムのUIとバックエンドのように、いくつかの異なるタスクを並列実行するための構文である。

  • C の_Thread_local キーワードは、グローバル静的変数のスレッドプライベートコピーを作成する。

  • クリティカル領域とミューテックス

  • OpenMPにおける原子性変数

  • 連続した一貫性についての簡単なメモと、それを必要とする理由

  • POSIXスレッドとOpenMPとの違い

  • C原子を介した原子スカラー変数

  • C原子を介した原子構造体

この章もまた、標準的なC言語の教科書に欠けているものに基づいている。市場を調査したところ、OpenMPを扱った一般的なCの教科書は1冊も発見できなかった。そのため、ここでは、スレッド理論に関する完全な本を参照する必要がなくなるかもしれない程度に、始めるのに十分な内容を提供する。

しかし、並行プログラミングのトピックに特化した本があり、その多くはC言語で書かれている:The Art of Concurrency:A Thread Monkey's Guide to Writing Parallel Applications;Multicore Application Programming: for Windows, Linux, and Oracle Solaris;Introduction to Parallel Computing (2nd Edition)を参照のこと。キャッシュ最適化の詳細や、便利なOpenMPプラグマの網羅的なリスト(これはインターネット検索エンジンで十分だ)については触れない。

環境

スレッド機構が標準Cの一部となったのは2011年12月の改訂からである。そのため、いくつかの選択肢がある:

  • POSIXスレッド。pthreads標準は1995年頃のPOSIX v1で定義された。pthread_create 関数は、ある形式の関数を各スレッドに代入することで動作する。したがって、適切な関数インタフェースと、通常はそれに付随する構造体を書かなければならない。 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Learn C Programming - Second Edition

Learn C Programming - Second Edition

Jeff Szuhay
Tiny C Projects

Tiny C Projects

Daniel Gookin

Publisher Resources

ISBN: 9798341650848