void *func_1(void *args)
{
while(1)
{
Sleep(1);
printf("this is func_1!\n");
}
}
void *func_2(void *args)
{
while(1)
{
Sleep(2);
printf("this is func_2!\n");
}
}
void main()
{
/* unsigned int */
pthread_t pid1, pid2;
/* 创建线程函数,func_1:线程运行函数的起始地址 */
if(pthread_create(&pid1, NULL, func_1, NULL))
{
return -1;
}
if(pthread_create(&pid2, NULL, func_2, NULL))
{
return -1;
}
while(1)
{
Sleep(3);
}
return;
{
while(1)
{
Sleep(1);
printf("this is func_1!\n");
}
}
void *func_2(void *args)
{
while(1)
{
Sleep(2);
printf("this is func_2!\n");
}
}
void main()
{
/* unsigned int */
pthread_t pid1, pid2;
/* 创建线程函数,func_1:线程运行函数的起始地址 */
if(pthread_create(&pid1, NULL, func_1, NULL))
{
return -1;
}
if(pthread_create(&pid2, NULL, func_2, NULL))
{
return -1;
}
while(1)
{
Sleep(3);
}
return;
}
编译:
gcc -thread.c -o thread -lpthread
执行:
./thread
因为pthread并非Linux系统默认库,为静态链接库,所以在编译时要加上 -lpthread参数。