Thread: vector<string> trouble in pthread

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    110

    vector<string> trouble in pthread

    I'm trying to compare strings from a vector with a global string, but
    I can't get it right. When I print/display the vector is looks all
    right, a small test file. But when I try to display the hits in the
    threads I can't display the strings sitting the vector.
    Code:
      int i;
      for (vector<string>::size_type i = myStart; i <= myEnd; i++) {
        //Search forward
        if(ref.find(searchVector.at(i)) != std::string::npos){
          printf("FORWARD : My id: %d, myStart: %d, myEnd: %d, Sträng:
    \n", tid, myStart, myEnd, searchVector.at(i).c_str() );  //prints no
    string
          myHits[i] = ref.find(searchVector.at(i));
        } else if (ref.find(reverseString(searchVector.at(i))) !=
    std::string::npos ){
          printf("BACKWARD: My id: %d, myStart: %d, myEnd: %d, Sträng:
    \n", tid, myStart, myEnd, searchVector.at(i).c_str() );
          myHits[i] = ref.find(reverseString(searchVector.at(i)));
        } else {
          myHits[i] = 0;
          printf("MISS    : My id: %d, myStart: %d, myEnd: %d, Sträng:
    \n", tid, myStart, myEnd, searchVector.at(i).c_str() );
        }
      }
    ref is a global string holding the reference. searchVector is the
    vector<string> holding the strings I look for. myHits are the hit a
    certain thread registers.

    If what am I doing wrong here..?

    EDIT, post was gnarly, sorry! Now it ok
    Last edited by überfuzz; 03-22-2012 at 09:44 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Show us the smallest and simplest program that demonstrates the problem. You should also provide the test input, expected output and actual output.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    110
    Code:
    void *alignDNS(void *t){
      long tid;
      tid = (long)t;
    
    //TO DO: int division check intervalls
      int tInterval = 1+(rows/2)/NUM_THREADS; 
      int myStart, myEnd;
      myStart = tInterval*(int)tid;
      myEnd = myStart+tInterval;
    
    
      int myHits[tInterval];
    
    
      int i;
      string temp;
      for (vector<string>::size_type i = myStart; i <= myEnd; i++) {
        if(ref.find(searchVector.at(i)) != std::string::npos){
          printf("FRONT   : My id: %d, myStart: %d, myEnd: %d, Sträng:  \n", tid, myStart, myEnd, searchVector.at(i).c_str() );
          myHits[i] = ref.find(searchVector.at(i));
        } else if (ref.find(reverseString(searchVector.at(i))) != std::string::npos ){
          printf("BACKWARD: My id: %d, myStart: %d, myEnd: %d, Sträng:  \n", tid, myStart, myEnd, searchVector.at(i).c_str() );
    
          myHits[i] = ref.find(reverseString(searchVector.at(i)));
        } else {
          myHits[i] = 0;
          printf("MISS    : My id: %d, myStart: %d, myEnd: %d, Sträng:  \n", tid, myStart, myEnd, searchVector.at(i).c_str() );
        }
      }
    
    
      pthread_mutex_lock(&mutexDNS);
      //update allHits
    
      pthread_mutex_unlock(&mutexDNS);
      //Addera trådens träffar till träffar. 
    
    
      //  pthread_exit((void*) t);
    }
    
    
    
    int main()
    {
      ref = file2String("reference.fa");
    
      cout << "Enter how many rows your chunkfile has: ";
      cin >> rows;
      searchVector.resize(rows/2);
    
      ifstream searchFile("test_part.fa");
      string line;
      int i = 0;
      while(getline(searchFile, line))
        {
          if(line.find(">")){          
        searchVector.at(i) = line;   
        i++;
          }
    
        }
    
      // printChunks();
    
      pthread_t thread[NUM_THREADS];
      pthread_attr_t attr;
      int rc;
      long t;
      void *status;
    
    
      pthread_attr_init(&attr);
      pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
      pthread_mutex_init(&mutexDNS, NULL);
      for(t=0; t<NUM_THREADS; t++) {
        //    printf("Main: creating thread %ld\n", t);
        rc = pthread_create(&thread[t], &attr, alignDNS, (void *)t); 
        if (rc) {
          printf("ERROR; return code from pthread_create() is %d\n", rc);
          exit(-1);
        }
       }
        
       // Free attribute and wait for the other threads 
       pthread_mutex_destroy(&mutexDNS);
        pthread_attr_destroy(&attr);
        for(t=0; t<NUM_THREADS; t++) {
             rc = pthread_join(thread[t], &status);
            if (rc) {
                printf("ERROR; return code from pthread_join() is %d\n", rc);
                exit(-1);
            }
    
        }
    }
    EDIT, forgot the out put.
    Code:
    FRONT   : My id: 2, myStart: 8, myEnd: 12, Sträng:
    FRONT   : My id: 1, myStart: 4, myEnd: 8, Sträng:
    FRONT   : My id: 3, myStart: 12, myEnd: 16, Sträng:
    FRONT   : My id: 0, myStart: 0, myEnd: 4, Sträng:
    MISS    : My id: 4, myStart: 0, myEnd: 20, Sträng:
    MISS    : My id: 2, myStart: 8, myEnd: 12, Sträng:
    MISS    : My id: 1, myStart: 4, myEnd: 8, Sträng:
    FRONT   : My id: 1, myStart: 4, myEnd: 8, Sträng:
    MISS    : My id: 3, myStart: 12, myEnd: 16, Sträng:
    FRONT   : My id: 3, myStart: 12, myEnd: 16, Sträng:
    etc.
    etc...
    Should be
    Code:
    FRONT   : My id: 2, myStart: 8, myEnd: 12, Sträng: String with approx 30 characters.
    etc...
    etc.
    Last edited by überfuzz; 03-22-2012 at 09:57 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > printf("FRONT : My id: %d, myStart: %d, myEnd: %d, Sträng: \n"
    All of these are missing a %s to print the string.

    If you compile with "gcc -Wall", you should get complaints that your printf calls are malformed.

    Personally, I would be worried about whether any of the data structures you're accessing are immutable.
    Are all your c_str() invokations thread-safe (for example).

    Did you test the algorithm with a single thread first?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    110
    Argh!!! I guess I burned my powder form now. Some coffee maybe... Thank you for lending a pair of sharp eyes. :-)

    Off topic
    I'm not sure I follow you 100%, but it doesn't seem to me that the data get tangled up in the threads. However, I'm not sure I should divide the intervalls in the threads using thread id - tid. It's a hustle to get it right, I have to know exactly how many rows the search file has to avoid segmentation errors. Maybe I should use double division where I calculate myStart and myEnd and recast them just before using them to iterate between.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    >>int myHits[tInterval];
    Definitely wrong. This is a compiler extension at best. Use a vector here instead, eg std::vector<int> MyHits(tInterval);

    >>for (vector<string>::size_type i = myStart; i <= myEnd; i++)
    This looks wrong, too. Are you sure it stays in the proper range (especially that it does not go off the end by one)?

    Furthermore, I must ask why the heck you are using printf? Get rid of it.
    Why are you using pthreads when there are better alternatives available? C++11 has a standard thread library. Otherwise, there is boost or Intel's TBB.
    Lastly, I would suggest you improve your indentation.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Mar 2012
    Posts
    110
    Elysia - Thanks for a stern reply. printf() is just a force of habit, only test print though. I compensate for the interval issue you pointed out when I calculate the start and end of the intervals. I actually change the myHits array to a vector earlier.

    Now I have another problem, vector related...

    When I run the program now I get out_of_range. I pretty sure that it is my handling of the vectors when the data is mounting up and the vector has to be resized. Take a look:

    Code:
     vector<int> myHits(10000);
      int i;
      int count = 0;
      for (vector<string>::size_type i = myStart; i <= myEnd; i++) {
        //Search forward
        if(count >= myHits.size()){
          myHits.resize(1000);
        }
        if(ref.find(searchVector.at(i)) != std::string::npos){
          myHits.at(count) = ref.find(searchVector.at(i));
        } else if (ref.find(reverseString(searchVector.at(i))) !=
    std::string::npos ){
          myHits.at(count) = ref.find(reverseString(searchVector.at(i)));
        } else {
          // myHits.at(i) = 0;
    );
        }
        count++;
      }
    
    
      pthread_mutex_lock(&mutexDNS);
      Hits.resize(1001);
      std::vector<int>::iterator it = Hits.begin();
      Hits.insert(it, myHits.begin(), myHits.end());
      pthread_mutex_unlock(&mutexDNS);
    I try to reallocate a vector if it runs out of room. What am I doing wrong..?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting a 2d vector, pointer trouble
    By Imara in forum C Programming
    Replies: 13
    Last Post: 12-17-2011, 03:57 AM
  2. Converting string vector to integer vector
    By CPlus in forum C++ Programming
    Replies: 4
    Last Post: 05-08-2010, 05:43 AM
  3. Vector Trouble - Dev-C++
    By timmeh in forum C++ Programming
    Replies: 6
    Last Post: 02-16-2009, 03:35 AM
  4. vector trouble
    By h3ro in forum C++ Programming
    Replies: 22
    Last Post: 05-03-2007, 10:24 PM
  5. OGL camera movement and vector trouble
    By psychopath in forum Game Programming
    Replies: 1
    Last Post: 05-12-2004, 05:33 PM