/*
**创建多个线程
*/
#include<pthread.h>
#include<stdio.h>
#include<stdlib.h>
void *myThread1(void)
{
int i;
for (i=0; i<3; i++)
{
printf("This is the 1st pthread,created by fang.\n");
sleep(1);//Let this thread to sleep 1 second,and then continue to run
}
}
void *myThread2(void)
{
int i;
for (i=0; i<3; i++)
{
printf("This is the 2st pthread,created by fang.\n");
sleep(1);
}
}
int main()
{
pthread_t id1, id2;
int i, ret;
/**创建线程1*/
ret = pthread_create( &id1, NULL, (void *)(myThread1), NULL );
if( ret )
{
perror("pthread_create");
exit(1);
}
/**创建线程2*/
ret = pthread_create( &id2, NULL, (void *)(myThread2), NULL );
if( ret )
{
perror("pthread_create");
exit(1);
}
pthread_join(id1, NULL); //等待线程1结束
pthread_join(id2, NULL); //等待线程2结束
return 0;
}