首页 关于 微信公众号
欢迎关注我的微信公众号

POSIX线程API

POSIX线程是POSIX的线程标准,定义了创建和操纵线程的一套API。实现POSIX线程标准的库常被称作Pthreads,一般用于Unix-like POSIX 系统,如Linux、 Solaris。但是Microsoft Windows上的实现也存在,例如直接使用Windows API实现的第三方库pthreads-w32;而利用Windows的SFU/SUA子系统,则可以使用微软提供的一部分原生POSIX API。

Pthreads定义了一套C语言的类型、函数与常量,它以pthread.h头文件和一个线程库实现。

Pthreads API中大致共有100个函数调用,全都以”pthread_“开头,并可以分为四类:

POSIX的Semaphore API可以和Pthreads协同工作,但这并不是Pthreads的标准。因而这部分API是以”sem_“打头,而非”pthread_“。

功能

创建一个新线程。

简介

  1. #include <pthread.h>
  2. int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
  3. void *(*start_routine) (void *), void *arg);

编译和链接使用 -pthread .

描述

pthread_create()函数在程序中开启一个新的线程。新线程启动执行通过调用 start_routine() 函数,arg 是传递给 start_routine() 的唯一参数。

新的线程被终止于下列方法之一:

attr是设置新创建线程的属性的,该属性可以使用 pthread_attr_init(3) 进行初始化。如果 attr 设置为 NULL,则创建的线程利用的就是默认的属性。

在返回之前,如果成功创建了一个线程,则线程的ID会在 thread指向的缓冲区中存储。

返回值

成功时,返回 0, 失败时返回错误码。

示例

  1. #include <pthread.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6. #include <errno.h>
  7. #include <ctype.h>
  8. /* prototype for thread routine */
  9. void print_message_function ( void *ptr );
  10. /* struct to hold data to be passed to a thread
  11. this shows how multiple data items can be passed to a thread */
  12. typedef struct str_thdata
  13. {
  14. int thread_no;
  15. char message[100];
  16. } thdata;
  17. int main(int argc,char *argv[])
  18. {
  19. pthread_t thread1, thread2; /* thread variables */
  20. thdata data1, data2; /* structs to be passed to threads */
  21. /* initialize data to pass to thread 1 */
  22. data1.thread_no = 1;
  23. strcpy(data1.message, "Hello!");
  24. /* initialize data to pass to thread 2 */
  25. data2.thread_no = 2;
  26. strcpy(data2.message, "Hi!");
  27. /* create threads 1 and 2 */
  28. pthread_create (&thread1, NULL, (void *) &print_message_function, (void *) &data1);
  29. pthread_create (&thread2, NULL, (void *) &print_message_function, (void *) &data2);
  30. /* Main block now waits for both threads to terminate, before it exits
  31. If main block exits, both threads exit, even if the threads have not
  32. finished their work */
  33. pthread_join(thread1, NULL);
  34. pthread_join(thread2, NULL);
  35. /* exit */
  36. exit(0);
  37. } /* main() */
  38. /**
  39. * print_message_function is used as the start routine for the threads used
  40. * it accepts a void pointer
  41. **/
  42. void print_message_function ( void *ptr )
  43. {
  44. thdata *data;
  45. data = (thdata *) ptr; /* type cast to a pointer to thdata */
  46. /* do the work */
  47. printf("Thread %d says %s \n", data->thread_no, data->message);
  48. pthread_exit(0); /* exit */
  49. } /* print_message_function ( void *ptr ) */

功能

终止正在运行的线程。

简介

  1. #include <pthread.h>
  2. void pthread_exit(void *retval);

功能

向一个线程发送取消请求。

简介

  1. #include <pthread.h>
  2. int pthread_cancel(pthread_t thread);

描述

pthread_cancel() 函数向一个线程发送一个取消请求。目标线程什么时候回应以及回不回应取决于线程的两个属性:cancelability state 和 type.

线程的 cancelability state 取决于 pthread_setcancelstate(3),这个属性可以被设置为 enabled 和 disabled. 如果一个线程的 cancelability state 属性是 enabled 的,那么一个此时该线程如果接收到取消请求才有效。

发送成功并不意味着线程会终止。

若是在整个程序退出时,要终止各个线程,应该在成功发送 cancel 指令后,使用 pthread_join()函数,等待指定的线程硬完全退出之后,再继续执行;否则,很容易产生“段错误”。

返回值

成功则返回0,不成功则返回非0 。

功能

等待一个线程的结束,线程间同步的操作。

简介

  1. #include <pthread.h>
  2. int pthread_join(pthread_t thread, void **retval);

描述

pthread_join()函数,以阻塞的方式等待thread指定的线程结束,当函数返回时,被等待的线程资源被收回。如果线程已经结束,那么该函数会立即返回,并且thread指定的线程必须是joinable的。

返回值

成功则返回0,不成功则返回错误码。

####返回值

Blog

Opinion

Project