[英]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.