zoukankan      html  css  js  c++  java
  • 线程创建时的问题思考

    问题来由

    有一段代码

     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <stdlib.h>
     4 #include <pthread.h>
     5 #include <unistd.h>
     6 pthread_t ntid;
     7 void printids(const char *s)
     8 {
     9     pid_t pid;
    10     pthread_t tid;
    11     pid = getpid();
    12     tid = pthread_self();
    13     printf("%s pid %u tid %u (0x%x)
    ", s, (unsigned int)pid,
    14     (unsigned int)tid, (unsigned int)tid);
    15 }
    16 void *thr_fn(void *arg)
    17 {
    18     printids(arg);
    19     return NULL;
    20 }
    21 int main(void)
    22 {
    23     int err;
    24     err = pthread_create(&ntid, NULL, thr_fn, "new thread: ");
    25     if (err != 0) {
    26         fprintf(stderr, "can't create thread: %s
    ",
    27         strerror(err));
    28         exit(1);
    29     }
    30     printids("main thread:");
    31     sleep(1);
    32     return 0;
    33 }

    这段代码main函数中创建了一个线程,如果创建失败则输出出错信息。

    printids函数打印当前进程id和线程id

    main函数的线程和新创建的线程都会调用printids,得到线程信息。

    编译运行结果如下:

      $ gcc main.c -lpthread
      $ ./a.out
      main thread: pid 7398 tid 3084450496 (0xb7d8fac0)
      new thread: pid 7398 tid 3084446608 (0xb7d8eb90)

    那么问题来了:

      主线程在一个全局变量 ntid 中保存了新创建的线程的id,如果新创建的线程不调

    pthread_self 而是直接打印这个 ntid ,能不能达到同样的效果?

    思考:

    正常情况下,如果直接使用全局变量的ntid是可以得到新线程的thread_id的,但是如果代码修改一下

    void printids(const char *s) 
    {
       pid_t pid;
       pthread_t tid;
       printf("globel: ntid(%u) ntid(0x%x)  
    ",(unsigned int)ntid,           (unsigned int)ntid);
       pid = getpid();
       tid = pthread_self();
       printf("%s pid %u tid %u (0x%x)
    ", s, (unsigned int)pid,
       (unsigned int)tid, (unsigned int)tid);
    }

    main函数中开启多个线程

    1  for(i=0;i<10;i++)
    2  {
    3     err = pthread_create(&ntid, NULL, thr_fn, "new thread: ");
    4     if (err != 0) {
    5     fprintf(stderr, "can't create thread: %s
    ",
    6     strerror(err));
    7     exit(1);
    8  }

    那么输出结果将会是非常凌乱的。

    globel: ntid(2801985280) ntid(0xa702e700)  
    new thread:  pid 3545 tid 2810377984 (0xa782f700)
    globel: ntid(2793592576) ntid(0xa682d700)  
    new thread:  pid 3545 tid 2793592576 (0xa682d700)
    globel: ntid(2751629056) ntid(0xa4028700)  
    new thread:  pid 3545 tid 2760021760 (0xa4829700)
    globel: ntid(2793592576) ntid(0xa682d700)  
    globel: ntid(2734843648) ntid(0xa3026700)  
    globel: ntid(2768414464) ntid(0xa502a700)  
    new thread:  pid 3545 tid 2734843648 (0xa3026700)
    globel: ntid(2768414464) ntid(0xa502a700)  
    new thread:  pid 3545 tid 2801985280 (0xa702e700)
    globel: ntid(2734843648) ntid(0xa3026700)  
    new thread:  pid 3545 tid 2743236352 (0xa3827700)
    new thread:  pid 3545 tid 2776807168 (0xa582b700)
    new thread:  pid 3545 tid 2785199872 (0xa602c700)
    globel: ntid(2768414464) ntid(0xa502a700)  
    new thread:  pid 3545 tid 2768414464 (0xa502a700)
    globel: ntid(2743236352) ntid(0xa3827700)  
    new thread:  pid 3545 tid 2751629056 (0xa4028700)
    globel: ntid(2734843648) ntid(0xa3026700)  
    main thread: pid 3545 tid 2818803520 (0xa8038740)
    

     有时会连续输出好几个globel变量,之后才输出一个或者多new thread信息。可见在多线程中,这种方法不总是实用的。

      

  • 相关阅读:
    http之100-continue
    WebHeaderCollection类
    python selenium登陆网易云音乐
    python爬虫多线程编程
    python使用unittest模块selenium访问斗鱼获取直播信息
    python之jsonpath的使用
    python 自定义函数
    Android 中万能的 BaseAdapter(Spinner,ListView,GridView) 的使用!
    Android UI设计
    Android杂谈--ListView之BaseAdapter的使用
  • 原文地址:https://www.cnblogs.com/whiteHome/p/4871074.html
Copyright © 2011-2022 走看看