Conflict with cin & pthread

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • unclefester
    New Member
    • Mar 2008
    • 12

    Conflict with cin & pthread

    I created a pthread named thread which calls the function run(). Inside run, it's supposed to ask for user input. However, when I execute the program, it simply terminates. Is there a problem using cin with threads?

    Code:
    main()
    {   pthread_t thread;
        pthread_create(&thread,NULL,run,NULL);
    }
    
    void *run(void *ptr)
    {   int nRate;
        cin >> nRate;
    }
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    It has nothing to do with 'cin'; your main thread (the main() function) exited so the
    show is over. Don't just simply exit but join the other thread (see pthread_join()) .

    kind regards,

    Jos

    Comment

    • unclefester
      New Member
      • Mar 2008
      • 12

      #3
      Originally posted by JosAH
      It has nothing to do with 'cin'; your main thread (the main() function) exited so the
      show is over. Don't just simply exit but join the other thread (see pthread_join()) .

      kind regards,

      Jos
      Jos, thanks so much, but I need your advice again. I need to run 2 processes concurrently. How do I implement it using threads?

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by unclefester
        Jos, thanks so much, but I need your advice again. I need to run 2 processes concurrently. How do I implement it using threads?
        Simply start a new thread for at least one of them (the other thread is already
        running, but you might want to create a new thread for it as well).

        kind regards,

        Jos

        Comment

        • unclefester
          New Member
          • Mar 2008
          • 12

          #5
          Originally posted by JosAH
          Simply start a new thread for at least one of them (the other thread is already
          running, but you might want to create a new thread for it as well).

          kind regards,

          Jos

          Jos, thanks again, but we need your advice again... we've created a sample program to run 10 threads w/c prints values from 1 to 10. We wanted them to run concurrently that's why we used threads but based on the output it seems that they still run in a sequential manner.

          Thanks for the help...

          CODE:

          #include <stdio.h>
          #include <iostream>
          #include <string>
          #include <pthread.h>

          using namespace std;

          void * printMsg(void * args){
          int * msg;
          msg = (int *)args;

          for(int z=0; z<10 ; z++){
          cout << msg << " - " << z << endl;
          }
          }

          int main(){

          pthread_t thread_[6];

          for(int i=0; i<6; i++){
          pthread_create( &thread_[i], NULL ,printMsg, (void *) i);
          }

          for(int i=0; i<6; i++){
          pthread_join(th read_[i], NULL);
          }

          cout << " --- DONE! --- "<< endl;
          }

          OUTPUT:

          0 - 0
          0 - 1
          0 - 2
          0 - 3
          0 - 4
          0 - 5
          0 - 6
          0 - 7
          0 - 8
          0 - 9
          0x1 - 0
          0x1 - 1
          0x1 - 2
          0x1 - 3
          0x1 - 4
          0x1 - 5
          0x1 - 6
          0x1 - 7
          0x1 - 8
          0x1 - 9
          0x2 - 0
          0x2 - 1
          0x2 - 2
          0x2 - 3
          0x2 - 4
          0x2 - 5
          0x2 - 6
          0x2 - 7
          0x2 - 8
          0x2 - 9
          0x3 - 0
          0x3 - 1
          0x3 - 2
          0x3 - 3
          0x3 - 4
          0x3 - 5
          0x3 - 6
          0x3 - 7
          0x3 - 8
          0x3 - 9
          0x4 - 0
          0x4 - 1
          0x4 - 2
          0x4 - 3
          0x4 - 4
          0x4 - 5
          0x4 - 6
          0x4 - 7
          0x4 - 8
          0x4 - 9
          0x5 - 0
          0x5 - 1
          0x5 - 2
          0x5 - 3
          0x5 - 4
          0x5 - 5
          0x5 - 6
          0x5 - 7
          0x5 - 8
          0x5 - 9
          --- DONE! ---

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Try z < 1000 because I suspect that every thread has already finished before the
            next thread is created and started.

            kind regards,

            Jos

            Comment

            Working...