-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpthread-sample.c
More file actions
44 lines (42 loc) · 1.24 KB
/
pthread-sample.c
File metadata and controls
44 lines (42 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
* 工学基礎シリーズ オペレーティングシステム (オーム社)
* (3.1.9)
*
* Pthreadによるマルチスレッドプログラムのサンプル (p.55)
* コンパイル方法:
* - Linux: cc pthread-sample.c -lpthread -o pthread-sample
* - macOS: cc pthread-sample.c -o pthread-sample
*/
// マルチスレッドプログラムのサンプル
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
// スレッドとして実行する関数の定義
static char *thread_func(void *arg)
{
printf("sub: sleep 1sec\n");
sleep(1); /* sleep for 1sec. */
printf("sub: done\n");
return "bye!";
}
extern int main(int argc, char **argv)
{
pthread_t th;
printf("main: creating a thread\n");
// thread_funcから実行開始するスレッドを生成する.引数はNULL.
int rc = pthread_create(&th, NULL, (void*(*)(void*))&thread_func, NULL);
if (rc) {
perror("pthread_create");
exit(1);
}
printf("main: waiting for its termination...\n");
char *val;
// 生成したスレッドが終了するのを待機し,戻り値をvalに受け取る.
rc = pthread_join(th, (void**)&val);
if (rc) {
perror("pthread_join");
exit(1);
}
printf("main: finished (%s)\n", val);
}