在Linux下,使用pthread库来创建和操作线程,pthread库是linux系统自带的一个库,这个库文件在/usr/lib/libpthread.so
使用时首先在Makefile里面包含库 LFLAGES += -lphread
引用头文件
#include<unistd.h>//unix标准头文件
#include<pthread.h>
程序主体:
1)定义线程的主函数
void * Thread_Main(void *content)
{
while(1)
{
//CODE
msleep(1)
}
return NULL;
}
2)调用pthread_create
int mian()
{
pthread_t handle;//pthread_t 定义在pthread.h里面,用来指定一个线程对象。
pthread_create(&handle,//线程句柄
NULL,
Thread_Main,//线程入口函数
NULL)//第四个参数,即线程的参数,context的值
getchar();//不能让主线程退出
return 0;