C++ 多线程程序中的核心转储:基本\u字符串::\u S\u构造null无效

C++ 多线程程序中的核心转储:基本\u字符串::\u S\u构造null无效,c++,multithreading,pthreads,nsthread,C++,Multithreading,Pthreads,Nsthread,我希望4个线程将进入同一个名为read的函数,并执行函数中的操作(读取、读取后在监视器上打印,并显示所有内容…)。 问题是: terminate called after throwing an instance of 'std::logic_error' what(): basic_string::_S_construct null not valid trAborted (core dumped) 代码是: #include <pthread.h> #include &l

我希望4个线程将进入同一个名为
read
的函数,并执行函数中的操作(读取、读取后在监视器上打印,并显示所有内容…)。 问题是:

terminate called after throwing an instance of 'std::logic_error'
  what():  basic_string::_S_construct null not valid
trAborted (core dumped)
代码是:

#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
#include <iostream>
#include <time.h>
#include <cstdlib>
#include <fstream>
#include <string>
using namespace std;

struct v {
    int id;
    char* ad;

    v(int a, char* t) {
        id = a;
        ad = t;
    }
};

int bank = 1000;
pthread_mutex_t mutex;

void* read(void* argument)
{
    cout << "tr";
    v* d;
    int num;
    d = (v*) argument;
    string l = d->ad;
    int n = d->id;
    string x = "";
    ifstream textfile;
    textfile.open(l.c_str());
    while (!textfile.eof()) {
        textfile >> x;
        if (x == "SUB") {
            pthread_mutex_lock(&mutex);
            textfile >> num;
            bank = bank - num;
            cout << "person num " << n << " sub " << num << " dollars and know in the Bank: " << bank << endl;
            pthread_mutex_unlock(&mutex);
        }
        if (x == "ADD") {
            pthread_mutex_lock(&mutex);
            textfile >> num;
            bank = bank + num;
            cout << "person num " << n << " add " << num << " dollars and know in the Bank: " << bank << endl;
            pthread_mutex_unlock(&mutex);
        }
        if (x == "GET") {
            pthread_mutex_lock(&mutex);
            cout << "person num " << n << " look in the Bank: " << bank << endl;
            pthread_mutex_unlock(&mutex);
        }


    }
    textfile.close();
    return 0;
}

int main(void)
{
    pthread_mutex_init(&mutex, NULL);
    int i = 0, j = 0;
    v data1(1, "file1.dat"), data2(2, "file2.dat"), data3(3, "file3.dat"), data4(4, "file4.dat");
    pthread_t t1, t2, t3, t4;
    i = pthread_create(&t1, NULL, read, (void*)&data1);
    if (i != 0) cout << "error" ;
    j = pthread_create(&t2, NULL, read, (void*)&data2);
    if (j != 0) cout << "error" ;
    i = pthread_create(&t3, NULL, read, (void*)&data3);
    if (i != 0) cout << "error" ;
    j = pthread_create(&t4, NULL, read, (void*)&data4);
    if (j != 0) cout << "error" ;

    pthread_exit(NULL);

    return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
结构v{
int-id;
char*ad;
v(整数a,字符*t){
id=a;
ad=t;
}
};
国际银行=1000;
pthread_mutex_t mutex;
void*读取(void*参数)
{
cout-ad;
int n=d->id;
字符串x=“”;
ifstream文本文件;
textfile.open(l.c_str());
而(!textfile.eof()){
textfile>>x;
如果(x=“子”){
pthread_mutex_lock(&mutex);
textfile>>num;
银行=银行-数量;

cout您正在堆栈上传递线程数据,这几乎不是一个好主意

调用
pthread\u exit
时,包含
dataN
对象的主线程堆栈将被解除分配。如果在
pthread\u exit
之后安排了一个线程,它将对解除分配的对象进行操作


最好的解决方案是通过
new

在堆上分配
数据
对象。通过使用try/catch连续地包装越来越小的程序部分,进行分而治之的操作,直到您确定抛出的语句。另外:什么操作系统、编译器、编译器版本号,也许最重要的是版本号这可能不是答案,但在一些较旧的Gnu libc中,字符串构造函数不是线程安全的(它们在实现的深处共享一个未同步的单例)在您告诉我们错误的行号之前,请不要发布包含核心转储错误的问题。您真的希望我们为您编译和调试此问题吗?告诉我们哪一行是问题所在。(如果您不知道如何做,请在编写超过十行的程序之前学习使用gdb。运行“gdb./myprog”它会在使用stacktrace调用terminate时停止。)@NicholasWilson:我同意(我们需要行号),但这是一个未捕获的异常,因此在终止点堆栈已完全解开。那么gdb会提供任何有用的信息吗?这就是为什么我建议使用分而治之的尝试捕获方法。@徘徊“捕获抛出”如何?另外,我很抱歉语气粗鲁,这不是我想要的。更好的方法是:调用
pthread\u join()在调用
pthread\u exit()
之前,对每个子线程执行