小编Row*_*era的帖子

const char&作为函数的参数

我需要对以下代码块进行一些澄清.我正在研究类似编码的东西,我想了解它是如何工作的.谷歌上没有太多的提及,我发现的唯一一个技术太难以理解了.

  1. 在以下代码中,线程和非线程函数具有不同的输出.这是为什么?

    #include <boost/thread.hpp>
    #include <iostream>
    #include <stdio.h>
    
    using namespace std;
    
    void fTry1(const char&);
    void fTry2(const char&);
    
    int main()
    {
      boost::thread tTry1, tTry2;
      const char *a = "efgh";
    
      tTry1 = boost::thread(fTry1, *a);
      fTry2(*a);
      tTry1.join();
    
      return 0;
    }
    
    void fTry1(const char& a)
    {
      cout << "Thread" << endl;
      cout << &a << endl;
    }
    
    void fTry2(const char& a)
    {
      cout << "Non-thread" << endl;
      cout << &a << endl;
    }
    
    Run Code Online (Sandbox Code Playgroud)

样本输出:

Non-thread
efgh
Thread
e<garbage data>
Run Code Online (Sandbox Code Playgroud)
  1. 如果我改变以下行

    cout << &a << …
    Run Code Online (Sandbox Code Playgroud)

c++ linux multithreading boost pointers

1
推荐指数
1
解决办法
817
查看次数

标签 统计

boost ×1

c++ ×1

linux ×1

multithreading ×1

pointers ×1