type pthread_t / pthread_create()

本文详细介绍了在Linux系统中如何使用POSIX线程接口创建线程,并提供了具体的代码示例。解析了pthread_create函数各参数的意义及使用方法,并讨论了restrict修饰符的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

pthread_t:

 This type is basically an integer (On hawk, it is just defined as an unsigned int. Its use is as a thread identifier.


Linux系统下的多线程遵循POSIX线程接口,称为pthread。

#include <pthread.h>

int pthread_create(pthread_t *restrict tidp,
const pthread_attr_t *restrict attr,
void *(*start_rtn)(void*),
void *restrict arg);

Returns: 0 if OK, error number on failure

C99 中新增加了 restrict 修饰的指针: 由 restrict 修饰的指针是最初唯一对指针所指向的对象进行存取的方法,仅当第二个指针基于第一个时,才能对对象进行存取。对对象的存取都限定于基于由 restrict 修饰的指针表达式中。 由 restrict 修饰的指针主要用于函数形参,或指向由 malloc() 分配的内存空间。restrict 数据类型不改变程序的语义。 编译器能通过作出 restrict 修饰的指针是存取对象的唯一方法的假设,更好地优化某些类型的例程。

第一个参数为指向线程标识符的指针。
第二个参数用来设置线程属性。
第三个参数是线程运行函数的起始地址。
最后一个参数是运行函数的参数。

下面这个程序中,我们的函数thr_fn不需要参数,所以最后一个参数设为空指针。第二个参数我们也设为空指针,这样将生成默认属性的线程。当创建线程成功时,函数返回0,若不为0则说明创建线程失败,常见的错误返回代码为EAGAIN和EINVAL。前者表示系统限制创建新的线程,例如线程数目过多了;后者表示第二个参数代表的线程属性值非法。创建线程成功后,新创建的线程则运行参数三和参数四确定的函数,原来的线程则继续运行下一行代码。

#include<stdio.h>
#include<pthread.h>
#include<string.h>
#
include<sys/types.h>
#
include<unistd.h>


pthread_t ntid;

void printids(const char *s){
pid_t pid;
  pthread_t tid;

  pid = getpid();
  tid = pthread_self();
  printf("%s pid %u tid %u (0x%x)\n",s,(unsigned int)pid,(unsigned int)tid,(unsigned 
int
)tid);
}

void *thr_fn(void *arg){
  printids("new thread:");
  return ((void *)0);
}

int main(){
  int err;

  err = pthread_create(&ntid,NULL,thr_fn,NULL);
  if(err != 0){
   printf("can't create thread: %s\n",strerror(err));
   return 1;
  }

  printids("main thread:");
  sleep(1);
  return 0;
}

把APUE2上的一个程序修改一下,然后编译。
结果报错:
pthread.c:(.text+0x85):对‘pthread_create’未定义的引用


由于pthread库不是Linux系统默认的库,连接时需要使用库libpthread.a,所以在使用pthread_create创建线程时,在编译中要加-lpthread参数:
gcc -o pthread -lpthread pthread.c
ubuntu@raspberrypi:~$ cat ~/PX4-Autopilot/build/px4_sitl_default/build_gazebo-classic/CMakeFiles/CMakeError.log Performing C SOURCE FILE Test CMAKE_HAVE_LIBC_PTHREAD failed with the following output: Change Dir: /home/ubuntu/PX4-Autopilot/build/px4_sitl_default/build_gazebo-classic/CMakeFiles/CMakeTmp Run Build Command(s):/bin/ninja cmTC_778f9 && [1/2] Building C object CMakeFiles/cmTC_778f9.dir/src.c.o [2/2] Linking C executable cmTC_778f9 FAILED: cmTC_778f9 : && /bin/cc -DCMAKE_HAVE_LIBC_PTHREAD CMakeFiles/cmTC_778f9.dir/src.c.o -o cmTC_778f9 && : /bin/ld: CMakeFiles/cmTC_778f9.dir/src.c.o: in function `main': src.c:(.text+0x48): undefined reference to `pthread_create' /bin/ld: src.c:(.text+0x50): undefined reference to `pthread_detach' /bin/ld: src.c:(.text+0x5c): undefined reference to `pthread_join' collect2: error: ld returned 1 exit status ninja: build stopped: subcommand failed. Source file was: #include <pthread.h> void* test_func(void* data) { return data; } int main(void) { pthread_t thread; pthread_create(&thread, NULL, test_func, NULL); pthread_detach(thread); pthread_join(thread, NULL); pthread_atfork(NULL, NULL, NULL); pthread_exit(NULL); return 0; } Determining if the function pthread_create exists in the pthreads failed with the following output: Change Dir: /home/ubuntu/PX4-Autopilot/build/px4_sitl_default/build_gazebo-classic/CMakeFiles/CMakeTmp Run Build Command(s):/bin/ninja cmTC_1816c && [1/2] Building C object CMakeFiles/cmTC_1816c.dir/CheckFunctionExists.c.o [2/2] Linking C executable cmTC_1816c FAILED: cmTC_1816c : && /bin/cc -DCHECK_FUNCTION_EXISTS=pthread_create CMakeFiles/cmTC_1816c.dir/CheckFunctionExists.c.o -o cmTC_1816c -lpthreads && : /bin/ld: cannot find -lpthreads collect2: error: ld returned 1 exit status ninja: build stopped: subcommand failed.
最新发布
08-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值