zoukankan      html  css  js  c++  java
  • linux创建线程之pthread_create

    转自:https://www.cnblogs.com/amanlikethis/p/5537175.html

    函数简介

      pthread_create是UNIX环境创建线程函数

    头文件

      #include<pthread.h>

    函数声明

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

    返回值

      若成功则返回0,否则返回出错编号

    参数

      第一个参数为指向线程标识符的指针。

      第二个参数用来设置线程属性。

      第三个参数是线程运行函数的地址。

      最后一个参数是运行函数的参数。

    注意

      在编译时注意加上-lpthread参数,以调用静态链接库。因为pthread并非Linux系统的默认库。 

                                 pthread_join函数

    函数简介

      函数pthread_join用来等待一个线程的结束。

    函数原型为:

      extern int pthread_join __P (pthread_t __th, void **__thread_return);

    参数:

      第一个参数为被等待的线程标识符

      第二个参数为一个用户定义的指针,它可以用来存储被等待线程的返回值。

    注意

        这个函数是一个线程阻塞的函数,调用它的函数将一直等待到被等待的线程结束为止,当函数返回时,被等待线程的资源被收回。如果执行成功,将返回0,如果失败则返回一个错误号。

    1 #include<stdio.h>
     2 #include<stdlib.h>
     3 #include<pthread.h>
     4 
     5 /* 声明结构体 */
     6 struct member
     7 {
     8     int num;
     9     char *name;
    10 };     
    11 
    12 /* 定义线程pthread */
    13 static void * pthread(void *arg)       
    14 {
    15     struct member *temp;
    16     
    17     /* 线程pthread开始运行 */
    18     printf("pthread start!
    ");
    19     
    20     /* 令主线程继续执行 */
    21     sleep(2);
    22     
    23     /* 打印传入参数 */
    24     temp = (struct member *)arg;      
    25     printf("member->num:%d
    ",temp->num);
    26     printf("member->name:%s
    ",temp->name);
    27     
    28     return NULL;
    29 }
    30 
    31 /* main函数 */
    32 int main(int agrc,char* argv[])
    33 {
    34     pthread_t tidp;
    35     struct member *b;
    36 
    37     /* 为结构体变量b赋值 */
    38     b = (struct member *)malloc(sizeof(struct member));           
    39     b->num=1;
    40     b->name="mlq";              
    41 
    42     /* 创建线程pthread */
    43     if ((pthread_create(&tidp, NULL, pthread, (void*)b)) == -1)
    44     {
    45         printf("create error!
    ");
    46         return 1;
    47     }
    48     
    49     /* 令线程pthread先运行 */
    50     sleep(1);
    51     
    52     /* 线程pthread睡眠2s,此时main可以先执行 */
    53     printf("mian continue!
    ");
    54     
    55     /* 等待线程pthread释放 */
    56     if (pthread_join(tidp, NULL))                  
    57     {
    58         printf("thread is not exit...
    ");
    59         return -2;
    60     }
    61     
    62     return 0;
    63 }

    编译与执行结果

        编译与执行结果如下图所示,可以看到主线程main和线程pthread交替执行。也就是说是当我们创建了线程pthread之后,两个线程都在执行,证明创建成功。另外,可以看到创建线程pthread时候,传入的参数被正确打印。

  • 相关阅读:
    Spring学习4_整合Hibernate进行数据库操作
    spring学习3_通过注解简单实现AOP
    Spring学习2_AOP通过XML配置简单实现
    Spring学习1_面向切面( AOP )实现原理
    hibernate学习6_session之clear与flush
    hibernate学习5_session之load与get区别
    hibernate学习4_openSession()与getCurrentSession()区别
    utf8汉字编码16进制对照
    Debug
    服务器设计过程中踩过的坑儿
  • 原文地址:https://www.cnblogs.com/qinshizhi/p/10972649.html
Copyright © 2011-2022 走看看