C++;多线程:在抛出';标准:长度误差'; 我在用Pthon学习C++中的多线程(它可能是旧的,但我只需要一些东西就可以开始)。我稍微修改了代码: #include <pthread.h> #include <iostream> #include <string> #define NUM_THREADS 5 #define DEBUG #undef DEBUG using namespace std; struct thread_data { int thread_id = 0; string message = ""; }; void *printHello(void *threadData) { struct thread_data* data = (struct thread_data*)threadData; int thread_id = data->thread_id; string message = data->message; cout << "From thread " << thread_id << ": " << message << "\n"; pthread_exit(NULL); } int main(int argc, char** argv) { pthread_t threads[NUM_THREADS]; for (int i = 0; i < NUM_THREADS; i++) { struct thread_data* data = new thread_data(); string message = "Special message for thread #" + to_string(i) + "!"; #ifdef DEBUG cout << "DEBUG: " << "i = " << i << endl; #endif data->thread_id = i; data->message = message; cout << "main(): creating thread, " << i << endl; int rc = 0; rc = pthread_create(&threads[i], NULL, printHello, (void *) data); delete data; if (rc) { cout << "Error: unable to create thread: " << rc << "\n"; return -1; } } pthread_exit(NULL); }

C++;多线程:在抛出';标准:长度误差'; 我在用Pthon学习C++中的多线程(它可能是旧的,但我只需要一些东西就可以开始)。我稍微修改了代码: #include <pthread.h> #include <iostream> #include <string> #define NUM_THREADS 5 #define DEBUG #undef DEBUG using namespace std; struct thread_data { int thread_id = 0; string message = ""; }; void *printHello(void *threadData) { struct thread_data* data = (struct thread_data*)threadData; int thread_id = data->thread_id; string message = data->message; cout << "From thread " << thread_id << ": " << message << "\n"; pthread_exit(NULL); } int main(int argc, char** argv) { pthread_t threads[NUM_THREADS]; for (int i = 0; i < NUM_THREADS; i++) { struct thread_data* data = new thread_data(); string message = "Special message for thread #" + to_string(i) + "!"; #ifdef DEBUG cout << "DEBUG: " << "i = " << i << endl; #endif data->thread_id = i; data->message = message; cout << "main(): creating thread, " << i << endl; int rc = 0; rc = pthread_create(&threads[i], NULL, printHello, (void *) data); delete data; if (rc) { cout << "Error: unable to create thread: " << rc << "\n"; return -1; } } pthread_exit(NULL); },c++,multithreading,pointers,pthreads,C++,Multithreading,Pointers,Pthreads,输出为: main(): creating thread, 0 main(): creating thread, 1 From thread 1: Special message for thread #1! main(): creating thread, 2 From thread 2: Special message for thread #2! main(): creating thread, 3 From thread 3: Special message for thread #3!

输出为:

main(): creating thread, 0
main(): creating thread, 1
From thread 1: Special message for thread #1!
main(): creating thread, 2
From thread 2: Special message for thread #2!
main(): creating thread, 3
From thread 3: Special message for thread #3!
main(): creating thread, 4
From thread 4: Special message for thread #4!
terminate called after throwing an instance of 'std::length_error'
  what():  basic_string::_S_create
Aborted (core dumped)
如果我不使用
pthread\u create
创建线程并直接调用printHello函数,则不会发生错误。有时,程序会抛出一个segfault,有时它运行得很顺利

另一个问题是,在线程0中应该有一行代码:线程0的特殊消息从一开始,但没有

此外,有时甚至根本没有出现“线程的特殊消息”

我尝试初始化结构的变量,在第31行使用静态分配的内存(通过不使用
new
,使用堆栈而不是堆)。我试图避免在printHello函数中使用指针,但由于pthread_create的最后一个参数只接受指向函数参数的指针,因此我无法做到这一点

我的第一个怀疑是,当我分配
data->message=message
时,出现了一些错误,因此我尝试将字符串直接分配给
data->message
,但没有成功。但我仍然认为错误一定存在,因为异常是由“basic\u string”抛出的
std::length\u error

或者,当我将
数据
传递给
pthread\u create
时,或者当我在第18行进行转换时,我做错了什么。我的想法是,当我将其传递给函数时,我将其作为指针传递,并将其转换为void指针
void*
。当
printHello
接收到参数时,我将其转换为type
thread\u data*
,这是一个指针,它原来就是指针

到目前为止,我就是在这个时候想到的。如果我的写作中有什么不清楚的地方,请发表评论(英语不是我的第一语言)


提前感谢大家。

创建线程后,您将立即删除
数据。这意味着不能保证
数据
指针在线程尝试访问活动对象时仍然指向该对象


只有当没有人再使用该对象时,才应删除数据。线程完成后(您可以使用
pthread\u join
来实现这一点,例如)。

谢谢,这就是问题所在。我在调用delete之前加入了线程,它工作得很好。@QuanTran:请注意,如果在for循环中加入线程,则不会并行运行线程。您需要在for循环之后加入线程(这也意味着您需要跟踪for循环之外的数据指针)。谢谢,我直到现在才注意到。我实际上是通过编写下载管理器来学习Java中的并发性的,但是因为这个项目比这个小练习要大,所以我创建了线程并将它们连接到不同的for循环中,而在这个小练习中,我只是将它们全部放在一个for循环中。非常感谢:)@QuanTran Delete
thread\u data
作为线程进程返回之前的最后一个操作。
main(): creating thread, 0
main(): creating thread, 1
From thread 1: Special message for thread #1!
main(): creating thread, 2
From thread 2: Special message for thread #2!
main(): creating thread, 3
From thread 3: Special message for thread #3!
main(): creating thread, 4
From thread 4: Special message for thread #4!
terminate called after throwing an instance of 'std::length_error'
  what():  basic_string::_S_create
Aborted (core dumped)