Hello, I made this short program to understand a bit of multithreading butits no
ID: 3615438 • Letter: H
Question
Hello,I made this short program to understand a bit of multithreading butits not working. Could anyone please take a look at it?
#include <iostream>
#include <pthread.h>
using namespace std;
void *server( void *ptr );// thread function
void *listener (void *ptr);
struct variable {
int n;
char y;
};
int main(int argc, char **argv)
{
variable *var1, *var2;
cout<<"Enter both n"<<endl;
cin>>var1->n>>var2->n;
cout<<"Enter both y"<<endl;
cin>>var1->y>>var2->y;
int th1, th2;
pthread_t thread1, thread2;
th1 = pthread_create( &thread1, NULL,server, (void*) var1);
th2 = pthread_create( &thread2, NULL,listener, (void*) var2);
pthread_join( thread1, NULL);
pthread_join( thread2, NULL);
cout<<"Thread 1 returns:"<<th1<<endl;
cout<<"Thread 1 returns:"<<th1<<endl;
return 0;
}
void *server( void *ptr )
{
variable *var3;
var3 = (variable*)ptr;
cout<<var3->n<<var3->y<<endl;
}
void *listener (void *ptr)
{
variable *var4;
var4 = (variable*)ptr;
cout<<var4->n<<var4->y<<endl;
}
The errors I am getting are:
Compiling...
DCN.CPP
C:Program FilesMicrosoft VisualStudioMyProjectsProjectDCN.CPP(22) : warning C4700: localvariable 'var2' used without having been initialized
C:Program FilesMicrosoft VisualStudioMyProjectsProjectDCN.CPP(22) : warning C4700: localvariable 'var1' used without having been initialized
C:Program FilesMicrosoft VisualStudioMyProjectsProjectDCN.CPP(50) : error C4716: 'server' :must return a value
C:Program FilesMicrosoft VisualStudioMyProjectsProjectDCN.CPP(58) : error C4716: 'listener' :must return a value
Error executing cl.exe.
DCN.OBJ - 2 error(s), 2 warning(s)
------------------------------------------
Thank you.
Explanation / Answer
//Hello, You must use g++ compilerto compile it in linux environment. // example : g++ test.cpp-lpthread
//Hope this will helpyou.
#include <iostream> #include <pthread.h>
using namespace std;
void *server( void *ptr );// thread function void *listener (void *ptr);
struct variable { int n; char y; };
int main(int argc, char **argv)
{
variable var1, var2; /* you need already allocated variablesnot pointer */ cout<<"Enter bothn"<<endl; cin>>var1.n>>var2.n; cout<<"Enter bothy"<<endl; cin>>var1.y>>var2.y;
int th1, th2; pthread_t thread1, thread2;
th1 = pthread_create( &thread1, NULL,server, (void*) (&var1)); th2 = pthread_create( &thread2, NULL,listener, (void*) (&var2)); pthread_join( thread1, NULL); pthread_join( thread2, NULL); cout<<"Thread 1 returns:"<<th1<<endl; cout<<"Thread 1 returns:"<<th1<<endl;
return 0;
}
void *server( void *ptr ) { variable *var3; var3 = (variable*)ptr; cout<<var3->n<<var3->y<<endl; }
void *listener (void *ptr) { variable *var4; var4 = (variable*)ptr; cout<<var4->n<<var4->y<<endl;
}
#include <iostream> #include <pthread.h>
using namespace std;
void *server( void *ptr );// thread function void *listener (void *ptr);
struct variable { int n; char y; };
int main(int argc, char **argv)
{
variable var1, var2; /* you need already allocated variablesnot pointer */ cout<<"Enter bothn"<<endl; cin>>var1.n>>var2.n; cout<<"Enter bothy"<<endl; cin>>var1.y>>var2.y;
int th1, th2; pthread_t thread1, thread2;
th1 = pthread_create( &thread1, NULL,server, (void*) (&var1)); th2 = pthread_create( &thread2, NULL,listener, (void*) (&var2)); pthread_join( thread1, NULL); pthread_join( thread2, NULL); cout<<"Thread 1 returns:"<<th1<<endl; cout<<"Thread 1 returns:"<<th1<<endl;
return 0;
}
void *server( void *ptr ) { variable *var3; var3 = (variable*)ptr; cout<<var3->n<<var3->y<<endl; }
void *listener (void *ptr) { variable *var4; var4 = (variable*)ptr; cout<<var4->n<<var4->y<<endl;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.