pthread一些函数的总结使用

第一次使用pthread,遇到的问题还真不少,现在我一一记录一下:

   1.关于编译时出现 对‘pthread_create’未定义的引用 之类的错误的解决:由于pthread库不是Linux系统默认的库,连接时需要使用库libpthread.a,所以在使用pthread_create创建线程时,在编译中要加-lpthread参数:
    gcc -o pthread -lpthread pthread.c

  特别的,如果这样还没解决的话:

  按照上面编译了一下,还是一样的提示.

  后面man gcc
  才知道Usage: gcc [options] file...
  因此需要将库链接放在末尾。
  xs@vm:~/Desktop$ gcc -o pthread pthread.c -lpthread

   2.关于pthread里的一些函数.

   pthread_join函数:

   函数pthread_join用来等待一个线程的结束。
   函数定义: int pthread_join(pthread_t thread, void **retval);
   描述 :
   pthread_join()函数,以阻塞的方式等待thread指定的线程结束。当函数返回时,被等待线程的资源被收回。如果进程已经结束,那么该函数会立即返回。并且thread指定的线程必须是joinable的。

   参数 :
   thread: 线程标识符,即线程ID,标识唯一线程。
   retval: 用户定义的指针,用来存储被等待线程的返回值。
   返回值 : 0代表成功。 失败,返回的则是错误号。

   看下面一段程序:

[cpp]  view plain  copy
 print ?
  1. #include <pthread.h>  
  2. #include <unistd.h>  
  3. #include <stdio.h>  
  4. void *thread(void *str)  
  5. {  
  6.     int i;  
  7.     for (i = 0; i < 10; ++i)  
  8.     {  
  9.         sleep(2);  
  10.         printf( "This in the thread : %d\n" , i );  
  11.     }  
  12.     return NULL;  
  13. }  
  14.   
  15. int main()  
  16. {  
  17.     pthread_t pth;  
  18.     int i;  
  19.     int ret = pthread_create(&pth, NULL, thread, (void *)(i));  
  20.       
  21.     pthread_join(pth, NULL);  
  22.     for (i = 0; i < 10; ++i)  
  23.     {  
  24.         sleep(1);  
  25.         printf( "This in the main : %d\n" , i );  
  26.     }  
  27.       
  28.     return 0;  
  29. }  


  如果我们注释掉"pthread_join(pth, NULL);"这一行:

  运行结果如下:


   也就是说:子线程还没有执行完毕,main函数已经退出,那么子线程也就退出了!

   如果我们不注释掉那一行,那么运行结果如下:


  这说明:pthread_join函数的调用者在等待子线程退出后才继续执行!


  pthread_create函数:

 声明:

  int pthread_create(pthread_t *thread,

                    const pthread_attr_t *restrict_attr,

                    void*(*start_rtn)(void*),

                    void *restrict arg);

   参数:

   第一个参数*thread为指向线程标识符的指针。
   第二个参数*restrict_attr用来设置线程属性,上面也可以用NULL,表示使用默认的属性。
   第三个参数是线程运行函数的起始地址。
   最后一个参数是运行函数的参数,NULL表示无参数。
   另外,在编译时注意加上-lpthread参数,以调用链接库。因为pthread并非Linux系统的默认库,而是posix线程库,在Linux中将其作为一个库来使用,因此加上 -lpthread(或-pthread)以显示的链接该库。函数在执行错误时的错误信息将作为返回值返回,并不修改系统全局变量errno,当然也无法使用perror()打印错误信息。

    

   pthread_t:pthread_t用于声明线程ID!

   类型定义:
   typedef unsigned long int pthread_t;
   //come from /usr/include/bits/pthread.h
   sizeof (pthread_t) =4;

   

    pthread_attr_init函数:

    声明:int pthread_attr_init(pthread_attr_t*attr);

   返回值:返回0,表示函数初始化对象成功。失败时返回一个错误代码。
   参数:指向一个线程属性的指针。

   

    下面一个程序是书上的:

[cpp]  view plain  copy
 print ?
  1. /*小小的一个程序,折腾人个半死*/  
  2. #include <pthread.h>  
  3. #include <unistd.h>  
  4. #include <stdio.h>  
  5.   
  6. int sum;  
  7. void *runner (void *param);  
  8.   
  9. int main(int argc, char *argv[])  
  10. {  
  11.     pthread_t tid;/*线程标示符*/  
  12.     pthread_attr_t attr;  
  13.   
  14.     if (argc != 2)/*如果参数不为2个*/  
  15.     {  
  16.         fprintf (stderr, "usage:a.out<integer value>\n");/*报错*/  
  17.         return -1;  
  18.     }  
  19.     if (atoi(argv[1] ) < 0)  
  20.     {  
  21.         fprintf (stderr, "%d must be <= 0\n", atoi(argv[1]));  
  22.         return -1;  
  23.     }  
  24.     pthread_attr_init(&attr); /*初始化,得到默认的属性值*/  
  25.     pthread_create(&tid, &attr, runner, argv[1]);/*创建一个线程*/  
  26.     pthread_join(tid, NULL);/*等待子线程执行完毕*/  
  27.   
  28.     printf ("sum = %d\n", sum);  
  29.     return 0;  
  30. }  
  31.   
  32. void *runner(void *param)/*子线程将会执行这个函数*/  
  33. {  
  34.     int i, upper = atoi(param);  
  35.     sum = 0;  
  36.     for (i = 1; i <= upper; i++)  
  37.     {  
  38.         sum += i;  
  39.     }  
  40.     pthread_exit(0);  
  41. }  
pthread_mutex_init函数是用来初始化线程锁的。其函数原型为:int pthread_mutex_init(pthread_mutex_t * mutex, const pthread_mutexattr_t * attr)。 在初始化时,我们可以选择是否设置特定的属性。另外,我们也可以使用宏定义PTHREAD_MUTEX_INITIALIZER来初始化线程锁。 此外,还可以使用pthread_mutex_trylock函数来尝试进行加锁操作,如果加锁失败则立即返回,而不会阻塞线程。这在一些特定的场景中可能会有用。 总结起来,pthread_mutex_init函数用于初始化线程锁,pthread_mutex_lock函数用于加锁,pthread_mutex_unlock函数用于解锁,pthread_mutex_trylock函数用于尝试加锁。这些函数可以帮助我们实现线程间的同步和互斥操作。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [【pthread使用】](https://blog.csdn.net/weixin_43273308/article/details/129911882)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [pthread_mutex_init()函数详解](https://blog.csdn.net/Primeprime/article/details/105617856)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值