简体   繁体   English

在不同线程中运行的函数产生奇怪的输出

[英]Function running in a different thread produces weird outputs

Hello I am new to multithreading in C++. 您好,我是C ++中多线程的新手。 I am using a thread class available in C++11 in order to run a function in a different thread but somehow the output I am getting from the function is very awkward. 我正在使用C ++ 11中可用的线程类,以便在其他线程中运行函数,但是从某种程度上我从函数中获得的输出非常尴尬。 This might be because different threads might be trying to execute the same variable at the same time and thus causing conflicts. 这可能是因为不同的线程可能试图同时执行相同的变量,从而导致冲突。 Please suggest how should I modify my code so that I can get correct output. 请提出如何修改代码的建议,以便获得正确的输出。 I am posting a sample code of what I am trying to do. 我正在发布我想做的示例代码。 This is not the original code but it just shows the flow of my original code since my original code is too long to be posted, But the problem remains same for both the cases. 这不是原始代码,而只是显示我原始代码的流程,因为我的原始代码太长而无法发布,但是在两种情况下问题仍然相同。

    #include<iostream>
    #include<thread>    

    using namespace std;

    typedef struct {
         int thread_id;
         char *message;
    }threadData;

    int display(threadData *tData){
         threadData *my_data;
         my_data = (threadData *) tData;

         cout << "Thread ID: " << my_data -> thread_id << endl; 
         cout << "Message: " << my_data -> message << endl; 

         return 0;
    }

  int main(){

      threadData *data;

      data = (threadData *)malloc(sizeof(threadData));
      data->thread_id = 12;
      data->message = "This is the message"; 
      for (int i = 0; i<10; i++)
      {
          std::thread t1(display, data);    
          t1.detach();
      }
      return 0;
  }

Output: 输出:

    Thread ID: 12
    Message: This is the messageThread ID: 
    12
    Message: This is the message
    Thread ID: 12
    Message: This is the message
    Thread ID: 12
    Message: This is the message
    Thread ID: 12
    Message: This is the message

What I read is the for loop expected to run 10 times, but it only ran 4 times, the reason is because in the main function you didn't wait for all the thread done, so the main process exit before other threads having a chance to run. 我读到的是for循环应该运行10次,但只运行4次,原因是因为在main函数中您没有等待所有线程完成,所以主进程在其他线程有机会之前退出跑步。 'main' need to sleep for a while to wait for all the thread done their work. “主要”需要睡眠一会儿,以等待所有线程完成工作。

And I didn't see a race condition here because all the thread just reading, no one writing to threadData. 而且我在这里没有看到竞争状况,因为所有线程都在读取,而没有人写入threadData。

As thread does not guaruntee which runs first, you need to protect access to the shared resource. 由于线程不保证首先运行,因此您需要保护对共享资源的访问。 The easiest way to do so, is through a mutex. 最简单的方法是通过互斥锁。

std::mutex g_i_mutex;  // protects g_i

typedef struct {
     int thread_id;
     string message;
}threadData;

int display(threadData *tData)
{
 std::lock_guard<std::mutex> lock(g_i_mutex);
 threadData *my_data;
 my_data = (threadData *) tData;

 cout << "Thread ID: " << my_data -> thread_id << endl; 
 cout << "Message: " << my_data -> message << endl; 

 return 0;

} }

Output: 输出:

Thread ID: 12 Message: This is the message Thread ID: 12 Message: This is the message Thread ID: 12 Message: This is the message Thread ID: 12 Message: This is the message 线程ID:12消息:这是消息线程ID:12消息:这是消息线程ID:12消息:这是消息线程ID:12消息:这是消息

I'd suggest you read a bit more about threading concepts. 我建议您阅读更多有关线程概念的知识。 The concepts behind it are not simple and just getting a ready made solution will not help you in the long run. 它背后的概念并不简单,从长远来看,仅仅获得现成的解决方案对您没有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用char指针函数和std :: string调用线程会产生不同的结果 - Calling thread with char pointer function and std::string produces different results 使用不同的参数函数运行std :: thread - Running std::thread with different argument function 为什么在 C++ 中调试和运行输出不同? - Why debugging and running outputs are different in C++? 在线程中运行函数 - Running a function in a thread 为什么这个简单的函数的编译器输出如此不同? - Why are compiler outputs so different for this simple function? C ++字符串比较运算符&gt;根据两个字符串的比较方式产生不同的输出 - c++ string compare operator > produces different outputs depending on how two strings are compared 将正在运行的线程中的函数移动到新线程? - Moving a function in a running thread to a new thread? 将字符串指针传递给在 C++ 和 Xcode 11.1 的不同线程上运行的 function - Passing a string pointer to a function running on different thread in C++ and Xcode 11.1 使用在不同线程中运行的成员回调 function 中的成员 shared_ptr(ROS 主题订阅) - Using member shared_ptr from a member callback function running in different thread (ROS topic subscription) “ shld”指令产生奇怪的值 - “shld” instruction produces weird value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM