Skip to content

Instantly share code, notes, and snippets.

@ccckmit
Created May 31, 2018 12:15
Show Gist options
  • Save ccckmit/e54a2a34b98cdb97c5d12c02ca02b65f to your computer and use it in GitHub Desktop.
Save ccckmit/e54a2a34b98cdb97c5d12c02ca02b65f to your computer and use it in GitHub Desktop.
#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