473,385 Members | 1,838 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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?

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.  
Mar 21 '08 #1
5 1809
JosAH
11,448 Expert 8TB
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
Mar 21 '08 #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
Jos, thanks so much, but I need your advice again. I need to run 2 processes concurrently. How do I implement it using threads?
Mar 21 '08 #3
JosAH
11,448 Expert 8TB
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
Mar 21 '08 #4
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! ---
Mar 21 '08 #5
JosAH
11,448 Expert 8TB
Try z < 1000 because I suspect that every thread has already finished before the
next thread is created and started.

kind regards,

Jos
Mar 21 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: lokb | last post by:
Hi, I am creating a detach thread as shown below and caling pthread_create in a while loop where the file names in the directory are fetched and is passed as a parmater to pthread create. The...
10
by: noleander | last post by:
I've got an application that uses Pthread to do threading. Mostly Im using Condition Variables and the associated function calls: - pthread_cond_wait() - pthread_cond_signal() -...
1
by: haplo | last post by:
Hello! I recently tried using threads in my winForms application in c# along with a form containing a drag& drop listbox which resulted in recieving the infamous "DragDrop registration did not...
0
by: bythelight | last post by:
Hello This: #include <gnutls.h> #include <gcrypt.h> #include <errno.h> #include <pthread.h> GCRY_THREAD_OPTION_PTHREAD_IMPL;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.