Printing the odd and even no using Pthread
#include<iostream>
#include <pthread.h>
using namespace std;
void *even(void * size)
{
int x = reinterpret_cast<int> (size);
cout << "even no" <<endl;
for(int i = 0; i<x; i++)
{
if(i%2 == 0){
cout << i << endl;
}
}
cout <<endl;
cout <<"even thread is terminited ";
cout <<endl;
}
void *odd(void *size )
{
pthread_t tid2;
int x = reinterpret_cast<int> (size);
cout << "odd no" <<endl ;
for(int i = 0; i<x; i++)
{
if(i%2 != 0){
cout << i <<endl ;
}
}
pthread_create(&tid2, NULL, even, (void*)x);
cout <<endl;
pthread_join(tid2, NULL);
cout <<"odd thread is terminited ";
cout <<endl;
}
int main()
{
int size;
cout << "Enter a no " <<endl;
cin >> size ;
cout << endl;
cout << size <<endl;
pthread_t tid1 , tid2;
pthread_create(&tid1, NULL, odd, (void*)size);
pthread_join(tid1, NULL);
return 0;
}
Good. ..
ReplyDelete