pthreads #pthreads

Similar documents
cocos2d-x #cocos2d-x

wix #wix

スレッド

xslt #xslt

ohp03.dvi

r07.dvi

スレッド

ohp07.dvi

fx-9860G Manager PLUS_J

/ SCHEDULE /06/07(Tue) / Basic of Programming /06/09(Thu) / Fundamental structures /06/14(Tue) / Memory Management /06/1

C言語によるアルゴリズムとデータ構造

西川町広報誌NETWORKにしかわ2011年1月号

Microsoft PowerPoint ppt [互換モード]

r03.dvi

Ver.1 1/17/2003 2

Microsoft Word - Win-Outlook.docx

,,,,., C Java,,.,,.,., ,,.,, i

pptx

slide5.pptx

L3 Japanese (90570) 2008

MIDI_IO.book

A/B (2018/10/19) Ver kurino/2018/soft/soft.html A/B


1 # include < stdio.h> 2 # include < string.h> 3 4 int main (){ 5 char str [222]; 6 scanf ("%s", str ); 7 int n= strlen ( str ); 8 for ( int i=n -2; i

第5回お試しアカウント付き並列プログラミング講習会

untitled

エレクトーンのお客様向けiPhone/iPad接続マニュアル

超初心者用

Microsoft Word - PCM TL-Ed.4.4(特定電気用品適合性検査申込のご案内)

Microsoft PowerPoint ppt [互換モード]

インターネット接続ガイド v110


tuat1.dvi

untitled

橡Pro PDF

iPhone/iPad接続マニュアル

LC304_manual.ai

<4D F736F F F696E74202D CEA8D758DC E396BC8E8C F92758E8C81458C E8C81458F9593AE8E8C>


C. S2 X D. E.. (1) X S1 10 S2 X+S1 3 X+S S1S2 X+S1+S2 X S1 X+S S X+S2 X A. S1 2 a. b. c. d. e. 2

I. Backus-Naur BNF : N N 0 N N N N N N 0, 1 BNF N N 0 11 (parse tree) 11 (1) (2) (3) (4) II. 0(0 101)* (

NSR-500 Installation Guide

Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []: An optional company name []: Using con

I. Backus-Naur BNF S + S S * S S x S +, *, x BNF S (parse tree) : * x + x x S * S x + S S S x x (1) * x x * x (2) * + x x x (3) + x * x + x x (4) * *

はじめに

137. Tenancy specific information (a) Amount of deposit paid. (insert amount of deposit paid; in the case of a joint tenancy it should be the total am

国際恋愛で避けるべき7つの失敗と解決策

1.ppt


\615L\625\761\621\745\615\750\617\743\623\6075\614\616\615\606.PS

第16回ニュージェネレーション_cs4.indd

鹿大広報149号

RX600 & RX200シリーズ アプリケーションノート RX用仮想EEPROM

program.dvi

ユーザーズマニュアル

Introduction Purpose This training course demonstrates the use of the High-performance Embedded Workshop (HEW), a key tool for developing software for

一 先 行 研 究 と 問 題 の 所 在 19


lagged behind social progress. During the wartime Chonaikai did cooperate with military activities. But it was not Chonaikai alone that cooperated. Al

平成29年度英語力調査結果(中学3年生)の概要

1st-session key

:30 12:00 I. I VI II. III. IV. a d V. VI


VE-GP32DL_DW_ZA


きずなプロジェクト-表紙.indd

Level 3 Japanese (90570) 2011


untitled


1 return main() { main main C 1 戻り値の型 関数名 引数 関数ブロックをあらわす中括弧 main() 関数の定義 int main(void){ printf("hello World!!\n"); return 0; 戻り値 1: main() 2.2 C main

CONTENTS Public relations brochure of Higashikawa March No.749 2

XMPによる並列化実装2

II ( ) prog8-1.c s1542h017%./prog8-1 1 => 35 Hiroshi 2 => 23 Koji 3 => 67 Satoshi 4 => 87 Junko 5 => 64 Ichiro 6 => 89 Mari 7 => 73 D

memo

A5 PDF.pwd

Answers Practice 08 JFD1

-2-

C

VE-GD21DL_DW_ZB

GP05取説.indb

鹿大広報146号

FAX-760CLT

untitled

自分の天職をつかめ

joho07-1.ppt

,

:30 12:00 I. I VI II. III. IV. a d V. VI

目次 1. レッスンで使える表現 レッスンでお困りの際に使えるフレーズからレッスンの中でよく使われるフレーズまで 便利な表現をご紹介させていただきます ご活用方法として 講師に伝えたいことが伝わらない場合に下記の通りご利用ください 1 該当の表現を直接講師に伝える 2 該当の英語表現を Skype

〈論文〉興行データベースから「古典芸能」の定義を考える

mstrcpy char *mstrcpy(const char *src); mstrcpy malloc (main free ) stdio.h fgets char *fgets(char *s, int size, FILE *stream); s size ( )

A 28 TEL Take-Two Interactive Software and its subsidiaries. All rights reserved. 2K Sports, the 2K


Microsoft PowerPoint ppt [互換モード]

null element [...] An element which, in some particular description, is posited as existing at a certain point in a structure even though there is no

ohp08.dvi

memo ii

ex01.dvi

[1] #include<stdio.h> main() { printf("hello, world."); return 0; } (G1) int long int float ± ±

Transcription:

pthreads #pthreads

1 1: pthreads 2 2 Examples 2 2 pthreads "Hello World" 2 2 3 2: pthreads 5 5 Examples 5 2T1T2 5 3: 8 8 8 Examples 9 / 9 11

You can share this PDF with anyone you feel could benefit from it, downloaded the latest version from: pthreads It is an unofficial and free pthreads ebook created for educational purposes. All the content is extracted from Stack Overflow Documentation, which is written by many hardworking individuals at Stack Overflow. It is neither affiliated with Stack Overflow nor official pthreads. The content is released under Creative Commons BY-SA, and the list of contributors to each chapter are provided in the credits section at the end of this book. Images may be copyright of their respective owners unless otherwise specified. All trademarks and registered trademarks are the property of their respective company owners. Use the content presented in this book at your own risk; it is not guaranteed to be correct nor accurate, please send your feedback and corrections to info@zzzprojects.com https://riptutorial.com/ja/home 1

1: pthreads をいめる このセクションでは pthreadsのと なぜがそれをいたいのかをします また pthreadsのきなテーマについてもし するトピックにリンクするがあります pthreadsのドキュメンテーションはしいものなので それらのトピックのバージョンをするがあります Examples インストールまたはセットアップ pthread をセットアップまたはインストールするためのしい pthreads をつの "Hello World" #include <pthread.h> #include <stdio.h> #include <string.h> /* function to be run as a thread always must have the same signature: it has one void* parameter and returns void */ void *threadfunction(void *arg) printf("hello, World!\n"); /*printf() is specified as thread-safe as of C11*/ return 0; int main(void) pthread_t thread; int createerror = pthread_create(&thread, NULL, threadfunction, NULL); /*creates a new thread with default attributes and NULL passed as the argument to the start routine*/ if (!createerror) /*check whether the thread creation was successful*/ pthread_join(thread, NULL); /*wait until the created thread terminates*/ return 0; fprintf("%s\n", strerror(createerror), stderr); return 1; をスレッドにす #include <stdio.h> #include <pthread.h> void *thread_func(void *arg) printf("i am thread #%d\n", *(int *)arg); return NULL; https://riptutorial.com/ja/home 2

int main(int argc, char *argv[]) pthread_t t1, t2; int i = 1; int j = 2; /* Create 2 threads t1 and t2 with default attributes which will execute function "thread_func()" in their own contexts with specified arguments. */ pthread_create(&t1, NULL, &thread_func, &i); pthread_create(&t2, NULL, &thread_func, &j); /* This makes the main thread wait on the death of t1 and t2. */ pthread_join(t1, NULL); pthread_join(t2, NULL); printf("in main thread\n"); return 0; コンパイル $ gcc -pthread -o hello hello.c これはします I am thread #1 I am thread #2 In main thread スレッドのをす void * にされたなデータへのポインタをして スレッドにをしたりをすことができます #include <stdio.h> #include <stdlib.h> #include <pthread.h> struct thread_args int a; double b; ; struct thread_result long x; double y; ; void *thread_func(void *args_void) struct thread_args *args = args_void; /* The thread cannot return a pointer to a local variable */ https://riptutorial.com/ja/home 3

struct thread_result *res = malloc(sizeof *res); res->x = 10 + args->a; res->y = args->a * args->b; return res; int main() pthread_t threadl; struct thread_args in =.a = 10,.b = 3.141592653 ; void *out_void; struct thread_result *out; pthread_create(&threadl, NULL, thread_func, &in); pthread_join(threadl, &out_void); out = out_void; printf("out -> x = %ld\tout -> b = %f\n", out->x, out->y); free(out); return 0; くの りをこのようにすはありません たとえば structのスペースをしてをすことも データへのポインタをスレッドにしてそこにされたをすこともできます オンラインでpthreadsをいめるをむ https://riptutorial.com/ja/pthreads/topic/5669/pthreadsをいめる https://riptutorial.com/ja/home 4

2: pthreads における き マルチスレッドアプリケーションをする するもなの 1 つはです そこで あなたはどのようにそれらをしていますかあなたはどうやってそれらをいますか Examples 2 つのスレッド T1 と T2 があるとします どのようにそれらをするのですかじ / リソース / メモリがのスレッドからアクセスで なくともスレッドの / リソース / メモリのがされているは がするがあります スレッドがvariable / resource / memoryののをしていて のスレッドがじものをみもうとすると されたがされないためです すべてのスレッドが / リソース / メモリのをみんでいるだけの はしません プログラムはにしんでいる #include <stdio.h> #include <pthread.h> int x= 0; void* fun(void* in) int i; for ( i = 0; i < 10000000; i++ ) x++; int main() pthread_t t1, t2; printf("point 1 >> X is: %d\n", x); pthread_create(&t1, NULL, fun, NULL); pthread_create(&t2, NULL, fun, NULL); pthread_join(t1, NULL); pthread_join(t2, NULL); printf("point 2 >> X is: %d\n", x); return 0; ののは https://riptutorial.com/ja/home 5

Point 1 >> X is: 0 Point 2 >> X is: 9925047 あなたのはわります しかし かにそれは2にはならないでしょう のスレッドがじループをし グローバルint x; をつのでint x; for ( i = 0; i < 10000000; i++ ) x++; だから x は Point 2 >> X is: 9925047 は 20,000,000 でなければなりません しかしそうではありません x のは x がみまれてからきされるまでのにのスレッドによってされるがあります スレッドが x のをしたが それをまだしていないとしよう のスレッドはじの x をりすこともでき ますスレッドがまだされていないため そして ともじ x + 1 を x にします スレッド 1 みみ x は 7 スレッド 1x に 1 をえ は 8 になりました スレッド 2 みみ x は 7 スレッド 18 スレッド 2x に 1 をえ は 8 になりました スレッド 2x を 8 で どのようにそれらをするのですか レースは リソースまたはにアクセスするコードのにあるのロックをすることでできます はされたプログラムです のがしました #include <stdio.h> #include <pthread.h> int x= 0; //Create mutex pthread_mutex_t test_mutex; void* fun(void* in) int i; for ( i = 0; i < 10000000; i++ ) //Lock mutex before going to change variable https://riptutorial.com/ja/home 6

pthread_mutex_lock(&test_mutex); x++; //Unlock mutex after changing the variable pthread_mutex_unlock(&test_mutex); int main() pthread_t t1, t2; printf("point 1 >> X is: %d\n", x); //Initlize mutex pthread_mutex_init(&test_mutex, NULL); pthread_create(&t1, NULL, fun, NULL); pthread_create(&t2, NULL, fun, NULL); pthread_join(t1, NULL); pthread_join(t2, NULL); //Destroy mutex after use pthread_mutex_destroy(&test_mutex); printf("point 2 >> X is: %d\n", x); return 0; はのとおりです Point 1 >> X is: 0 Point 2 >> X is: 20000000 ここで えは 20,000,000 とてきます レースコンディションエラーのないプログラムは にくのをします mutex ロックとアンロックにはがあるためです オンラインで pthreads におけるをむ https://riptutorial.com/ja/pthreads/topic/8243/pthreads におけ る https://riptutorial.com/ja/home 7

3: き は スレッドがのスレッドでこったことをつにです えば 1 つまたはのスレッドおよび 1 つのスレッドをするプロデューサ / コンシューマシナリオでは しいデータがであることをスレッドにするためにをすることができる なプロセス きプロデューサ / コンシューマのでは queuecond はにミューテックスプロデューサ / コンシューマ のでは queuemutex にされ に の にもするがあります queue.empty をプロデューサ / コンシュ ーマのでします これをしくすると にしいデータがけていないことがされます には プロセスはのようにするがあります シグナルスレッドの 1. ミューテックスをロックする 2. データとをする 3. にをる 4. ミューテックスのロックをする スレッドの 1. ミューテックスをロックする 2. データがができていないりループしてwhileループします 3. whileループでは pthread_cond_wait() をつか 4. whileループがすると しいデータがされ mutexがロックされていることがされます 5. データでかをする 6. ミューテックスのロックをし りしてください このスキームをすると シグナリングおよびスレッドがスケジュールされているでも のスレッドはデータをうことはありませんなデータをしてにすることはありません これは シグナルスレッドのステップをでし のスレッドのステップにして mutex およびのをすることによって できます pthread_cond_wait とミューテックス のをにするためには mutexをロックしたでpthread_cond_wait() をびすがあります びされると pthread_cond_wait() はスレッドをスリープにするにmutexのロックをし らかのでってくるに mutexがロックされます これはまた のスレッドがmutexをロックしている pthread_cond_wait() はmutexのロックがされるのをって のスレッドがにmutexをできるまで ロックしようとするのスレッドにミューテックス の https://riptutorial.com/ja/home 8

また をっている while ループがな if のわりにできるようにえるかもしれません しかし Posix では pthread_cond_wait() がにシグナルをすことなくにいわゆる "" ウェイクアップをうことがで きるため while ループがです したがって コードは pthread_cond_wait() がにされたか またはこれらのののためにされたかどうかをするためにをするがあります Examples プロデューサー / の pthread_mutex_t queuemutex; pthread_cond_t queuecond; Queue queue; void Initialize() //Initialize the mutex and the condition variable pthread_mutex_init(&queuemutex, NULL); pthread_cond_init(&queuecond, NULL); void Producer() //First we get some new data Data *newdata = MakeNewData(); //Lock the queue mutex to make sure that adding data to the queue happens correctly pthread_mutex_lock(&queuemutex); //Push new data to the queue queue.push(newdata); //Signal the condition variable that new data is available in the queue pthread_cond_signal(&queuecond); //Done, unlock the mutex pthread_mutex_unlock(&queuemutex); void Consumer() //Run the consumer loop while(1) //Start by locking the queue mutex pthread_mutex_lock(&queuemutex); //As long as the queue is empty, while(queue.empty()) // - wait for the condition variable to be signalled //Note: This call unlocks the mutex when called and //relocks it before returning! pthread_cond_wait(&queuecond, &queuemutex); //As we returned from the call, there must be new data in the queue - get it, Data *newdata = queue.front(); // - and remove it from the queue queue.pop(); https://riptutorial.com/ja/home 9

//Now unlock the mutex pthread_mutex_unlock(&queuemutex); // - and process the new data ProcessData(newData); オンラインでをむ https://riptutorial.com/ja/pthreads/topic/8614/ https://riptutorial.com/ja/home 10

クレジット S. No Contributors 1 pthreads をいめる Armali, ArturFH, caf, Community, cse, EOF, nachiketkulk 2 pthreads における cse, Mohan 3 sonicwave https://riptutorial.com/ja/home 11