create.c
#include<stdio.h>
#include<unistd.h>
#include<pthread.h>
void* Entry1(void* arg)
{
(void)arg;
while(1)
{
printf("hello world1!\n");
sleep(1);
}
return NULL;
}
void* Entry2(void* arg)
{
(void)arg;
while(1)
{
printf("hello world2!\n");
sleep(1);
}
return NULL;
}
int main()
{
pthread_t tid1, tid2;
pthread_create(&tid1, NULL, Entry1, NULL);
pthread_create(&tid2, NULL, Entry2, NULL);
while(1)
{
printf("Main is here.\n");
sleep(1);
}
return 0;
}
运行结果:三个线程通过cpu的调度交替运行。
join.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<pthread.h>
void *thread1(void* arg)
{