- #1
perplexabot
Gold Member
- 329
- 5
Hey all. I am currently trying to brush up on my multithreading "skills" (I dare say) that I learned in my undergrad class. I have the following code that I am trying to run:
I am compiling using g++ with the following:
No errors are encountered BUT when I execute
in the terminal I don't get anything. I was hoping to get the sequence of numbers from 0 to 9...
Any help on this would be greatly appreciated as I have a job interview tomorrow and I believe I will be questioned about threads : /
Thank you for reading.EDIT------------------------------------------------------
Hey again. So I have been playing around with this little program and I ran into something interesting (maybe not so interesting if you know what is going on... I don't tho!). If i place the following extra line in the code (a snippet):
I am able to see the output of func1 twice on the terminal! Which means the thread is working and so is the function call to func1 in main.
So why is it that when I remove this extra line I am not able to see the new thread's function output?
Code:
#include <pthread.h>
#include <iostream>
using namespace std;
void* func1(void*);
int main(int argc, char** argv){
pthread_t t1;
//func1(NULL);
int rc = pthread_create(&t1,NULL,func1,NULL);
func1(NULL);
return 0;
}
void* func1(void* trash){
for(int i = 0; i < 10; i++){
cout<<i<<endl;
}
}
I am compiling using g++ with the following:
Code:
g++ -pthread main.cpp
Code:
./a.out
Any help on this would be greatly appreciated as I have a job interview tomorrow and I believe I will be questioned about threads : /
Thank you for reading.EDIT------------------------------------------------------
Hey again. So I have been playing around with this little program and I ran into something interesting (maybe not so interesting if you know what is going on... I don't tho!). If i place the following extra line in the code (a snippet):
Code:
int main(int argc, char** argv){
pthread_t t1;
//func1(NULL);
int rc = pthread_create(&t1,NULL,func1,NULL);
func1(NULL); //<---- extra line
return 0;
}
I am able to see the output of func1 twice on the terminal! Which means the thread is working and so is the function call to func1 in main.
So why is it that when I remove this extra line I am not able to see the new thread's function output?
Last edited by a moderator: