C/C++ 实现一个多线程复制的cp命令。
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*start_routine) (void *), void *arg);
功能:创建线程
thread:用于获取线程ID,是输出型的参数
attr:线程的属性,一般写NULL采用默认方式创建即可
start_routine:线程的入口函数
arg:线程入口函数的参数
返回值:成功返回0,失败返回错误码
pthread_t pthread_self(void);
功能:获取线程ID
int pthread_equal(pthread_t t1, pthread_t t2);
功能:用于比较两线程ID是否相等
注意:有些操作系统的线程ID是以结构体形式实现的,因此不能直接使用==比较
int pthread_join(pthread_t thread, void **retval);
功能:等待指定的线程结束,并获取线程的结束返回值
thread:要等待的线程,如果子线程没有结束,当前线程会阻塞
retval:是一个二级指针,用于存储线程入口函数的返回值
注意:默认情况下主线程结束子线程会一起结束。
使用时需要包含pthread.h头文件,编译时需要添加-pthread参数
代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#