1,thread_test.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
// 线程A
void* print_a(void *a)
{
int i;
for(i=0; i < 10; i++)
{
sleep(1);
puts("aa");
}
return NULL;
}
// 线程B
void* print_b(void* b)
{
int i;
for(i=0; i<20; i++)
{
sleep(1);
puts("bb");
}
}
int main()
{
pthread_t t0;
pthread_t t1;
// 创建线程A
if(pthread_create(&t0, NULL, print_a, NULL) == -1)
{
puts("fail to create pthread t0");
exit(1);
}
// 创建线程B
if(pthread_create(&t1, NULL, print_b, NULL) == -1)
{
puts("fail to create pthread t1");
exit(1);
}
// 等待线程结束
void* result;
if(pthread_join(t0, &result) == -1)
{
puts("fail to recollect t0");
exit(1);
}
if(pthread_join(t1, &result) == -1)
{
puts("fail to recollect t1");
exit(1);
}
return 0;
}
2,编译
gcc -lpthread -o thread_test thread_test.c
3,执行
./thread_test
bb
aa
bb
aa
bb
aa
bb
aa
bb
aa
bb
...