std :: thread执行上下文(c ++ 14)

时间:2015-10-29 14:09:21

标签: multithreading pointers c++11 c++14 stdthread

当std :: thread调用的函数的输入/输出变量在执行期间更改值时,会出现问题...

功能:

static int func(stThread_t *&pStThread)

参数

pStThread:这是一个结构,它有一个指向std :: thread和其他变量(一些标志)的指针

typedef struct stThread {
   stThread() noexcept {...};    
   stThread(const stThread &cRigth) noexcept {...};    
   stThread & operator = (const stThread &cRigth) noexcept {...};

   std::thread *pThread;
   volatile bool bBegin;
   volatile bool bEnd;

 } stThread_t;

函数 func 打印参数的std :: thread的地址 pStThread和线程id

  1785280之前的

func this_id 21968

在做了this_thread :: sleep 2秒后,再次打印

  

func afer ... this_id 21968

   static int func(stThread_t *&pStThread) {

      std::thread::id this_id = std::this_thread::get_id();

      long long p_begin = (long long)pStThread;
      std::cout << "func before " << std::to_string(p_begin) << " this_id " << this_id << "\n";
      std::cout.flush();

      pStThread->bBegin = true;

      std::this_thread::sleep_for(std::chrono::milliseconds(2000));

      this_id = std::this_thread::get_id();
      long long p_end = (long long)pStThread;
      std::cout << "func afer " << std::to_string(p_end) << " this_id " << this_id << "\n";
      std::cout.flush();

      pStThread->bEnd = true;

      return 1;
   };

指向std :: thread的指针的地址已更改(已删除,已删除..?)

pStThread是一个指针struct stThread_t

列表的push_back
std::list<stThread_t*> listOfThreads;
listOfThreads.push_back(pStThread);

我读过有关std :: move的内容,但是无法使用指针

最后有一个线程“垃圾收集器”,它试图擦除所有待执行的线程。

这里的完整代码

#include <string>
#include <list>
#include <vector>
#include <map>
#include <thread>
#include <mutex>
#include <atomic>

#include <iostream>

typedef struct stThread {
   stThread() noexcept {
      pThread = NULL;
      bBegin = false;
      bEnd = false;
   };

   stThread(const stThread &cRigth) noexcept {
      this->pThread = cRigth.pThread;
      this->bBegin = (bool)cRigth.bBegin;
      this->bEnd = (bool)cRigth.bEnd;
   };

   stThread & operator = (const stThread &cRigth) noexcept {
      this->pThread = cRigth.pThread;
      this->bBegin = (bool)cRigth.bBegin;
      this->bEnd = (bool)cRigth.bEnd;

      return *this;
   };

   std::thread *pThread;
   volatile bool bBegin;
   volatile bool bEnd;

} stThread_t;

class CMain
{
public:
   typedef std::list<stThread_t*> MyList_threads;
   MyList_threads listOfThreads;

public:
   CMain() {

      std::cout << std::boolalpha << "Ex1 is move-constructible? "
         << std::is_move_constructible<stThread_t>::value << '\n'
         << "Ex1 is trivially move-constructible? "
         << std::is_trivially_move_constructible<stThread_t>::value << '\n'
         << "Ex1 is nothrow move-constructible? "
         << std::is_nothrow_move_constructible<stThread_t>::value << '\n'
         << "Ex2 is trivially move-constructible? "
         << std::is_trivially_move_constructible<stThread_t>::value << '\n'
         << "Ex2 is nothrow move-constructible? "
         << std::is_nothrow_move_constructible<stThread_t>::value << '\n';
   };

   static int func(stThread_t *&pStThread) {

      std::thread::id this_id = std::this_thread::get_id();

      long long p_begin = (long long)pStThread;
      std::cout << "func before " << std::to_string(p_begin) << " this_id " << this_id << "\n";
      std::cout.flush();

      pStThread->bBegin = true;

      std::this_thread::sleep_for(std::chrono::milliseconds(2000));

      this_id = std::this_thread::get_id();
      long long p_end = (long long)pStThread;
      std::cout << "func afer " << std::to_string(p_end) << " this_id " << this_id << "\n";
      std::cout.flush();

      pStThread->bEnd = true;

      return 1;
   };

   int _createThreads() {
      for (int iIdx = 0; (iIdx < 5); iIdx++) {
         stThread_t *pStThread = new stThread_t;

         pStThread->pThread = new std::thread(&CMain::func,
            std::ref(pStThread));

         if (pStThread) {
            do {
               std::this_thread::sleep_for(std::chrono::milliseconds(100));
            } while (!pStThread->bBegin);
            listOfThreads.push_back(pStThread);

            std::string sLog;
            sLog = "\nlistOfThreads.push_back " + std::to_string((long long)pStThread) + "\n";
            std::cout << sLog;
            std::cout.flush();
         }

         std::this_thread::sleep_for(std::chrono::milliseconds(1));
      }
      return 1;
   };

   int _main() {

      _createThreads();

      std::thread thread_collector([=]() {
         bool bEnd = false;
         MyList_threads::iterator it;
         it = listOfThreads.end();

         do {
            stThread_t *pStThread = NULL;

            if (it == listOfThreads.end()) {
               it = listOfThreads.begin();
               if (it == listOfThreads.end()) bEnd = true;
            }
            else it++;

            if (it != listOfThreads.end()) {
               if ((*it)->bEnd) {
                  pStThread = *it;

                  listOfThreads.erase(it);
                  it = listOfThreads.begin();
               }
            }

            if (pStThread) {
               if (pStThread->pThread) {
                  if (pStThread->pThread->joinable()) {
                     pStThread->pThread->join();

                     std::cout << " element deleted  " << std::to_string((long long)pStThread) << "\n";
                     std::cout.flush();
                  }
                  delete pStThread->pThread;
                  pStThread->pThread = NULL;
               }
               delete pStThread;
            }
            pStThread = NULL;


            std::this_thread::sleep_for(std::chrono::milliseconds(1));

         } while (!bEnd);
      });



      if (thread_collector.joinable()) {
         thread_collector.join();
      }

      return 1;
   };
};

int main()
{
   CMain _main;
   _main._main();

   return 0;
}

1 个答案:

答案 0 :(得分:2)

你有一个相当简单的错误,它与线程无关:

(1)func引用带到stThread_t*

static int func(stThread_t *&pStThread);

(2)您传递了对pStThread

的引用
std::thread(&CMain::func,std::ref(pStThread));

(3)这是一个局部变量,它的生命周期在循环迭代完成后立即结束

  for (int iIdx = 0; (iIdx < 5); iIdx++) {
     stThread_t *pStThread = new stThread_t;
     //...
  }

(4)因此,当函数在销毁后尝试访问对象时,会得到未定义的行为。 (小心!在这里,&#34;对象&#34;指的是有问题的指针,而不是指针指向的对象)

目前还不清楚为什么你坚持通过引用传递pStThread;你的函数实际上并没有修改指针(正好指向了什么),而你似乎并没有打算做任何这样的设备实际上有用的东西。