线程的概念就是可以两个线程一起执行,比如一个程序中可以有2个while循环2个循环互不影响的执行着各自的程序。
1.如果创建Linux线程?
普通的C语言程序两个while循环:
#include "stdio.h"
void fun1()
{
while(1)
{
printf("this is fun 1n");
sleep(1);
}
}
void fun2()
{
while(1)
{
printf("this is fun 2n");
sleep(1);
}
}
int main()
{
fun1();
fun2();
return 0;
}
线程的写法:
线程的编译运行
线程需要包含头文件#include "pthread.h"
pthread_t th1; //创建第一个线程
pthread_t th2;//创建第二个线程
#include "stdio.h"
#include "pthread.h"
void* fun1()
{
while(1)
{
printf("this is fun 1n");
sleep(1);
}
}
void* fun2()
{
while(1)
{
printf("this is fun 2n");
sleep(1);
}
}
int main()
{
pthread_t th1;
pthread_t th2;
pthread_create(&th1,NULL ,fun1,NULL);
pthread_create(&th2,NULL ,fun2,NULL);
while(1);
return 0;
}



