LWP:light weight process,轻量级的线程,本质仍然是进程。
进程:独立地址空间,拥有PCB(Process Control Block)进程控制块。
线程:也有PCB,但没有独立的地址空间(共享)
区别:在于是否共享地址空间。
独居(进程);合租(线程)。
linux下:
线程:最小的执行单位
进程:最小的分配资源单位,可看成是只有一个进程的线程。
pthread_self函数
获取线程ID。其作用对应进程中getpid()函数。
pthread_t pthread_self(void);
返回值:成功 0;失败 无;
pthread_create函数
创建一个新线程。
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg);
返回值:成功 0;失败 错误号;
linux环境下,所有线程特点,失败均直接返回错误号。
创建线程示例代码pthread_create.c
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
void *thrd_func(void *arg)
{
printf("In thread:thread id=%lu,pid=%u\n",pthread_self(),getpid());
return NULL;
}
int main(void)
{
pthread_t tid;
int ret;
printf("In main 1:thread id=%lu,pid=%u\n",pthread_self(),getpid());
ret=pthread_create(&tid,NULL,thrd_func,NULL);
if(ret!=0){
printf("pthread_create error\n");
exit(1);
}
sleep(1);
printf("In main2:thread id=%lu,pid=%u\n",pthread_self(),getpid());
return 0;
}
gcc -pthread pthread_create.c
./a.out
循环创建多个线程示例代码more_pthread.c
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
void *thrd_func(void *arg)
{
int i=(int)arg;
printf("%dth thread:thread id=%lu,pid=%u\n",i+1,pthread_self(),getpid());
return NULL;
}
int main(void)
{
pthread_t tid;
int ret,i;
for(i=0;i<50;i++){
ret=pthread_create(&tid,NULL,thrd_func,&i);
if(ret!=0){
fprintf(stderr,"pthread_create error:%s\n",strerror(ret));
exit(1);
}
}
sleep(i);
return 0;
}
pthread_exit函数
将单个线程退出。
void pthread_exit(void *retval);
参数:retval表示线程退出状态,通常传NULL
线程退出示例代码pthread_exit.c
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
void *thrd_func(void *arg)
{
printf("In thread:thread id=%lu,pid=%u\n",pthread_self(),getpid());
return NULL;
}
int main(void)
{
pthread_t tid;
int ret;
printf("In main 1:thread id=%lu,pid=%u\n",pthread_self(),getpid());
ret=pthread_create(&tid,NULL,thrd_func,NULL);
if(ret!=0){
fprintf(stderr,"pthread_create error:%s\n",strerror(ret));
exit(1);
}
printf("In main2:thread id=%lu,pid=%u\n",pthread_self(),getpid());
pthread_exit(NULL);
return 0;
}
pthread_join函数
阻塞等待线程退出,获取线程退出状态。其作用对应进程中waitpid()函数。
int pthread_join(pthread_t thread,void **retval);
成功:0;失败:错误号
线程退出示例代码pthread_join.c
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
typedef struct{
char ch;
int var;
char str[64];
}exit_t;
void *thrd_func(void *arg)
{
pthread_exit((void *)1);
}
int main(void)
{
pthread_t tid;
int ret;
int *retval;
printf("In main 1:thread id=%lu,pid=%u\n",pthread_self(),getpid());
ret=pthread_create(&tid,NULL,thrd_func,NULL);
if(ret!=0){
fprintf(stderr,"pthread_create error:%s\n",strerror(ret));
exit(1);
}
pthread_join(tid,(void **)&retval);
printf("--------------%d\n",(int)retval);
pthread_exit((void *)1);
}
pthread_detach函数
实现线程分离
int pthread_detach(pthread_t thread);
成功:0;失败:错误号
pthread_cancel函数
杀死(取消)线程,其作用,对应进程中kill()函数。
int pthread_cancel(pthread_t thread);
成功:0;失败:错误号。
pthread_equal函数
比较两个线程ID是否相等。
int pthread_equal(pthread_t t1,pthread_t t2);