Connecting Tech Pros Worldwide Forums | Help | Site Map

Conflict with cin & pthread

Newbie
 
Join Date: Mar 2008
Posts: 12
#1: Mar 21 '08
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?

Expand|Select|Wrap|Line Numbers
  1. main()
  2. {   pthread_t thread;
  3.     pthread_create(&thread,NULL,run,NULL);
  4. }
  5.  
  6. void *run(void *ptr)
  7. {   int nRate;
  8.     cin >> nRate;
  9. }
  10.  

JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#2: Mar 21 '08

re: Conflict with cin & pthread


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
Newbie
 
Join Date: Mar 2008
Posts: 12
#3: Mar 21 '08

re: Conflict with cin & pthread


Quote:

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?
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#4: Mar 21 '08

re: Conflict with cin & pthread


Quote:

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
Newbie
 
Join Date: Mar 2008
Posts: 12
#5: Mar 21 '08

re: Conflict with cin & pthread


Quote:

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(thread_[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! ---
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#6: Mar 21 '08

re: Conflict with cin & pthread


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

kind regards,

Jos
Reply