例1:进程与线程并行输出
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <pthread.h>
void *thread_routine(void *arg)
{
printf("I am a thread, my thread id is : %dn", pthread_self());
int i;
for ( i = 0; i < 5; i++)
{
printf("thread i = %dn", i);
fflush(stdout);
sleep(1);
}
printf("I am a thread, I will exitn");
}
int main()
{
pthread_t tid;
int ret = 0;
ret = pthread_create(&tid, NULL, thread_routine, NULL);
if (ret != 0)
{
printf("pthread_create errorn");
exit(0);
}
int i;
for ( i = 0; i < 5; i++)
{
printf("main i = %dn", i);
fflush(stdout);
sleep(1);
}
sleep(2);