Created
May 31, 2018 05:20
-
-
Save ccckmit/4ff39b38e1aa621797dc2db2484badd9 to your computer and use it in GitHub Desktop.
Thread Demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <pthread.h> // 引用 pthread 函式庫 | |
#include <unistd.h> | |
#include <stdlib.h> | |
#include <stdio.h> | |
void *print_george(void *argu) { // 每隔一秒鐘印出一次 George 的函數 | |
while (1) { | |
printf("George\n"); | |
sleep(1); | |
} | |
return NULL; | |
} | |
void *print_mary(void *argu) { // 每隔一秒鐘印出一次 Mary 的函數 | |
while (1) { | |
printf("Mary\n"); | |
sleep(2); | |
} | |
return NULL; | |
} | |
int main() { // 主程式開始 | |
pthread_t thread1, thread2; // 宣告兩個執行緒 | |
pthread_create(&thread1, NULL, &print_george, NULL); // 執行緒 print_george | |
pthread_create(&thread2, NULL, &print_mary, NULL); // 執行緒 print_mary | |
while (1) { // 主程式每隔一秒鐘 | |
printf("----------------\n"); // 就印出分隔行 | |
sleep(1); // 停止一秒鐘 | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment