「pthread 2つのスレッドを動かす」の版間の差分

提供: C言語入門
移動: 案内検索
(ページの作成:「pthreadで複数のスレッドを作成することは、1つのスレッドを作成する方法と何も変わりません。1つのスレッドを作成した...」)
 
行12: 行12:
 
=== ソースコード pthread2.c ===
 
=== ソースコード pthread2.c ===
 
<syntaxhighlight lang="c">
 
<syntaxhighlight lang="c">
 +
 
#include <stdio.h>
 
#include <stdio.h>
 
#include <stdlib.h>
 
#include <stdlib.h>
 
#include <err.h>
 
#include <err.h>
 +
#include <errno.h>
 +
#include <string.h>
 
#include <pthread.h>
 
#include <pthread.h>
 
#include <unistd.h>
 
#include <unistd.h>
  
void f1();
+
void f1(void);
void f2();
+
void f2(void);
  
int main(void)
+
int
 +
main(int argc, char *argv[])
 
{
 
{
 
         pthread_t thread1, thread2;
 
         pthread_t thread1, thread2;
行30: 行34:
  
 
         if (ret1 != 0) {
 
         if (ret1 != 0) {
                 err(EXIT_FAILURE, "can not create thread 1");
+
                 err(EXIT_FAILURE, "can not create thread 1: %s", strerror(ret1) );
 
         }
 
         }
 
         if (ret2 != 0) {
 
         if (ret2 != 0) {
                 err(EXIT_FAILURE, "can not create thread 2");
+
                 err(EXIT_FAILURE, "can not create thread 2: %s", strerror(ret2) );
 
         }
 
         }
  
行39: 行43:
 
         ret1 = pthread_join(thread1,NULL);
 
         ret1 = pthread_join(thread1,NULL);
 
         if (ret1 != 0) {
 
         if (ret1 != 0) {
                 err(EXIT_FAILURE, "can not join thread 1");
+
                 errc(EXIT_FAILURE, ret2, "can not join thread 1");
 
         }
 
         }
  
行45: 行49:
 
         ret2 = pthread_join(thread2,NULL);
 
         ret2 = pthread_join(thread2,NULL);
 
         if (ret2 != 0) {
 
         if (ret2 != 0) {
                 err(EXIT_FAILURE, "can not join thread 2");
+
                 errc(EXIT_FAILURE, ret2, "can not join thread 2");
 
         }
 
         }
  
行52: 行56:
 
}
 
}
  
 
+
void
void f1()
+
f1(void)
 
{
 
{
 
         size_t i;
 
         size_t i;
行63: 行67:
 
}
 
}
  
void f2()
+
void
 +
f2(void)
 
{
 
{
 
         size_t i;
 
         size_t i;

2014年5月11日 (日) 15:57時点における版

pthreadで複数のスレッドを作成することは、1つのスレッドを作成する方法と何も変わりません。1つのスレッドを作成したい場合には、pthread 1つのスレッドを動かすをご参照ください。C言語でpthreadを用いて、複数(2つ)のスレッドを実行します。

概要

2つのスレッドを作成するには、pthread_createを2回呼び出すだけ、です。 2つのスレッドを作成したら、2回join(pthread_join)でスレッドを回収しなければなりません。 スレッドが同時に動いていることをわかりやすくするために、スレッドの中で、sleep(usleep)させています。

実行結果から2つめのスレッドの関数が先に終わっていることがわかります。

pthread2.c の例

ソースコード pthread2.c

#include <stdio.h>
#include <stdlib.h>
#include <err.h>
#include <errno.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
 
void f1(void);
void f2(void);
 
int
main(int argc, char *argv[])
{
        pthread_t thread1, thread2;
        int ret1,ret2;
 
        ret1 = pthread_create(&thread1,NULL,(void *)f1,NULL);
        ret2 = pthread_create(&thread2,NULL,(void *)f2,NULL);
 
        if (ret1 != 0) {
                err(EXIT_FAILURE, "can not create thread 1: %s", strerror(ret1) );
        }
        if (ret2 != 0) {
                err(EXIT_FAILURE, "can not create thread 2: %s", strerror(ret2) );
        }
 
        printf("execute pthread_join thread1\n");
        ret1 = pthread_join(thread1,NULL);
        if (ret1 != 0) {
                errc(EXIT_FAILURE, ret2, "can not join thread 1");
        }
 
        printf("execute pthread_join thread2\n");
        ret2 = pthread_join(thread2,NULL);
        if (ret2 != 0) {
                errc(EXIT_FAILURE, ret2, "can not join thread 2");
        }
 
        printf("done\n");
        return 0;
}
 
void
f1(void)
{
        size_t i;
 
        for(i=0; i<8; i++){
                printf("Doing one thing\n");
                usleep(3);
        }
}
 
void
f2(void)
{
        size_t i;
 
        for(i=0; i<4; i++){
                printf("Doing another\n");
                usleep(2);
        }
}

コンパイル

cc  -lpthread pthread2.c -o pthread2

実行例

% ./pthread2
execute pthread_join thread1
Doing one thing
Doing another
Doing one thing
Doing another
Doing one thing
Doing another
Doing one thing
Doing another
Doing one thing
Doing one thing
Doing one thing
Doing one thing
execute pthread_join thread2
done

関連項目