0

I want that 4 threads will come into the same function named read and do what there is in the function (to read, after it to print on the monitor, and to show it all...). The problem:

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

The code is :

#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;
}
Deanie
  • 2,316
  • 2
  • 19
  • 35
Vladi
  • 1,662
  • 19
  • 30
  • Do a divide-and-conquer by wrapping successively smaller and smaller portions of your program with try/catch until you figure out which statement is throwing. Also: what os, compiler, compiler version number, and perhaps most importantly, version of libc? It's probably _not_ the answer, but in some of the older Gnu libcs the string constructors weren't thread safe (they shared an unsyncrhonized singleton somewhere deep in the implementation.) – Wandering Logic May 03 '13 at 15:03
  • 1
    Please don't post questions with core dump errors until you tell us the line number for the error. Do you honestly expect us to compile and debug this for you? Tell us which line is the problem. (If you don't know how to do that, then please learn to use gdb before writing programs longer than ten lines. Run "gdb ./myprog" and it'll stop at the moment terminate is called with a stacktrace.) – Nicholas Wilson May 03 '13 at 17:00
  • @NicholasWilson: I agree (that we need the line number), but this is an uncaught exception, so at the point of termination the stack has been completely unwound. So will gdb give any useful info? That's why I suggested the divide-and-conquer try-catch approach. – Wandering Logic May 03 '13 at 17:48
  • @Wandering How about "catch throw"? Also, I'm sorry for the rude tone, which wasn't what I intended. – Nicholas Wilson May 03 '13 at 18:58

1 Answers1

2

You're passing your threads data on the stack, which is almost never a good idea.

When you call pthread_exit, the main thread's stack, which contains the dataN objects, is deallocated. If one of your threads gets scheduled after pthread_exit, it will operate on the deallocated objects.

The best solution is to allocate your data objects on the heap via new.

Collin Dauphinee
  • 13,664
  • 1
  • 40
  • 71