多线程

来源:互联网 发布:vb .net boolean 判断 编辑:程序博客网 时间:2024/06/05 11:10
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;

}


编译:

gcc -thread.c -o thread -lpthread

执行:

./thread

因为pthread并非Linux系统默认库,为静态链接库,所以在编译时要加上 -lpthread参数。