一、线程的查看
(1)首先创建两个线程:
- 分析:下面的程序中创建了两个线程,程序执行起来,main函数所在程序为主线程,在这个主线程中有两个新线程运行
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
void* pthread_run1(void* arg)
{
(void)arg;
while(1)
{
printf("I am thread1,ID: %d\n",pthread_self());
sleep(1);
}
}
void* pthread_run2(void* arg)
{
(void)arg;
while(1)
{
printf("I am thread2,ID: %d\n",pthread_self());
sleep(1);
}
}
int main()
{
pthread_t tid1;
pthread_t tid2;
pthread_create(&tid1,NULL,pthread_run1,NULL);
pthread_create(&tid2,NULL,pthread_run2,NULL);
printf("I am main thread\n");
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
return 0;
}