zoukankan      html  css  js  c++  java
  • 线程间通信--线程创建与销毁

    1. pthread_create(&a_thread, NULL, thread_function, (void *)message);
    pthread_create(&a_thread, NULL, thread_function, (void *)num);
    原型: #include <pthread.h>
    int pthread_create(pthread_t *restrict thread,
    const pthread_attr_t *restrict attr,
    void *(*start_routine)(void*), void *restrict arg); thread: 创建成功返回的线程ID attr: 线程属性, 该参数一般为NULL, 为默认的线程属性 start_routine: 回调函数指针, 创建线程成功, 该线程所以执行的函数 arg: 函调函数的入参. 若为void, 则写NULL.

    2. pthread_join(a_thread, &thread_result);
    pthread_join - wait for thread termination
    #include <pthread.h>
    int pthread_join(pthread_t thread, void **value_ptr);
    thread: 主线程所要等待的副线程ID结束(也就是该副线程是由主线程的基础上创建的), 该函数由于收集线程终止, 
    然后释放该线程所占有的资源.
    value_ptr: 该线程thread所调用的函数中, 由 pthread_exit(void *value_ptr)指定传回. 但是还有一点疑问:
    
    3. pthread_exit("Thank you for thr CPU time");
    pthread_exit((void*)num);
    pthread_exit - thread termination #include <pthread.h> void pthread_exit(void *value_ptr); 该函数是在线程创建函数pthread_create函数中, 由自定义处理函数start_routine中显示定义传回的参数. 即pthread_exit所接收的参数即传出到pthread_join()中最后一个参数的值.

     1 /*******************************************************************************
     2 * 版权所有:
     3 * 模 块 名:
     4 * 文 件 名:pthread1.c
     5 * 实现功能:
     6 * 作    者:XYZ
     7 * 版    本:V1.0
     8 * 日    期:2013.07.24
     9 * 联系方式:xiao13149920@foxmail.com
    10 ********************************************************************************/
    11 // This is pthread1.c pthread_create, pthread_join, pthread_exit
    12 #include<stdio.h>
    13 #include<unistd.h>
    14 #include<stdlib.h>
    15 #include<string.h>
    16 #include<pthread.h>
    17 
    18 //#define CHAR
    19 
    20 void *thread_function(void *arg);
    21 
    22 char message[] = "Hello world";
    23 int num = 10;
    24 
    25 int main()
    26 {
    27     int res;
    28     pthread_t a_thread;
    29     void *thread_result;
    30     num = 10; 
    31 #ifdef  CHAR
    32     res = pthread_create(&a_thread, NULL, thread_function, (void *)message);
    33 #else
    34     res = pthread_create(&a_thread, NULL, thread_function, (void *)num);
    35 #endif
    36     if (res != 0)
    37     {
    38         perror("Thread create failed");
    39         exit(EXIT_FAILURE);
    40     }
    41 
    42     printf("Wating for thread to finsh...
    ");
    43     res = pthread_join(a_thread, &thread_result);
    44     if (res != 0)
    45     {
    46         perror("Thread join failed");
    47         exit(EXIT_FAILURE);
    48     }
    49 #ifdef CHAR
    50     printf("Thread joined, it returned %s
    ", (char *)thread_result);
    51     printf("Message is now %s
    ", message);
    52 #else
    53     printf("Thread joined, it returned %d
    ", (int)thread_result);
    54     printf("num is now %d
    ", num);
    55 #endif
    56     exit(EXIT_SUCCESS);
    57 }
    58 
    59 void *thread_function(void *arg)
    60 {
    61 #ifdef CHAR
    62     printf("Thread_function is running. Argument was %s
    ", (char *)arg);
    63     sleep(3);
    64     strcpy(message, "Bye!");
    65     pthread_exit("Thank you for the CPU time");  // 注释该行,返回值是 “Bye“
    66 #else
    67     printf("Thread_function is running. Argument was %d
    ", (int)arg);
    68     sleep(3);
    69     num = 100;
    70     pthread_exit((void*)num);                        // 注释该行,返回值是 0
    71 #endif
    72 }
    View Code

    需要注意的是:

    1. 注释 line:65, 返回的是message的值"Bye", 但是

    2. 注释 line:70, 返回的是0, 而不是num的值

    所以, 我们写线程函数的时候, 切记一定要调用pthread_exit((void*)value_ptr)退出.

  • 相关阅读:
    Scramble String
    Burst Balloons
    Coins in a Line III
    maven初识
    Java类加载器初识
    HTTP协议
    Map和Set的联系
    Thread类与Runnable接口
    Throwable和Exception的区别
    SpringMVC的@ResponseBody注解简介
  • 原文地址:https://www.cnblogs.com/xiao13149920yanyan/p/3213807.html
Copyright © 2011-2022 走看看