For CS2012 me did this experimental programming thing. It is not easy to do Thread programming in C++, In Java it is really easy.
In C++ you have to use special library called pthread.h.The Following link gives better description about it. If your Linux c++ dosent have this you have to download it.
Hear is my reference material where I learn threading.
After installing it you can do your programming easily.
After Learning Implemented the Dining Philosopher Problem. If you are interested contact me.
hello ;
ReplyDeletecan i ask you a question please?
is this tutorial for c or c++?? and do you have a code for the dining philosophers example with threads? as i need it to learn how to implement threads in this example
thanks alot for your help
This Tutorial is for c++. Also there are so many versions of this Dining Philosophers Problem. Here is our scenario.
ReplyDeleteThe Dining Philosophers Problem is a classic programming problem that is used to illustrate a
scenario of potential deadlock in a concurrent program.
In this problem, n number of Chinese Philosophers are seated around a round table with a bowl of
noodles in front of them. Each bowl has a chopstick on either side (between two bowls, there is only
one chopstick). To eat noodles, each philosopher needs two chopsticks (one to each hand).
An observer, who was watching the philosophers dining, has made the following observations:
1. Each philosopher first tries to get a chopstick to one hand. If she is successful in that, she
will try to get a chopstick to other hand. If she is not successful in that, she will put down the
chopstick she has already got.
2. Once a philosopher has two chopsticks in each hand, she will eat some noodle. Then she will
put down the chopstick in one hand and then the chopstick in the other hand.
3. After eating, the philosopher will spend some time thinking.
And this is the solution I did,
ReplyDelete#include
#include
#include
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t condition_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t condition_cond = PTHREAD_COND_INITIALIZER;
#define COUNT_HALT1 10000
#define timer 1000
class ChopStick{
public:
ChopStick(char *name);
bool assignPhilosopher();
bool leavePhilsopher() ;
char *Name;
private:
int counter;
bool haveAPhilosopher;
};
ChopStick::ChopStick(char *name){
haveAPhilosopher=false;
Name=name;
}
bool ChopStick::assignPhilosopher(){
pthread_mutex_lock( &mutex1 );//these will make the function thread safe
counter++;
pthread_mutex_unlock( &mutex1 );
if(haveAPhilosopher==false){
haveAPhilosopher=true;
return true;
}
else
return false;
}
bool ChopStick::leavePhilsopher(){
if(haveAPhilosopher){
haveAPhilosopher=false;
return true;
}
else
return false;
}
class Philosopher{
public:
Philosopher(ChopStick *cs1,ChopStick *cs2,char* name);
void getleftChopstick();
void getrightChopstick();
void leaveleftChopstick();
void leaverightChopstick();
void eat();
void think();
void run();
bool gotRightStick,gotLeftStick;
private:
char* Name;
ChopStick *leftHandSide,*rightHandSide;
};
Philosopher ::Philosopher(ChopStick *cs1,ChopStick *cs2,char* name){
gotRightStick=false;
gotLeftStick=false;
leftHandSide=cs1;
rightHandSide=cs2;
Name=name;
}
void Philosopher :: getleftChopstick(){
ChopStick &cs=*leftHandSide;
bool result=cs.assignPhilosopher();
if(result){
std::cout <<" " << Name <<" get Left ChopStick\n" ;
gotLeftStick=true;
}
}
void Philosopher :: getrightChopstick(){
ChopStick &cs=*rightHandSide;
bool result=cs.assignPhilosopher();
if(result){
std::cout <<" " << Name <<" get Right ChopStick\n" ;
gotRightStick=true;
}
}
void Philosopher ::leaveleftChopstick(){
std::cout <<" " << Name <<" leave the Left ChopStick\n" ;
ChopStick &cs=*leftHandSide;
cs.leavePhilsopher();
gotLeftStick=false;
}
void Philosopher :: leaverightChopstick(){
std::cout <<" " << Name <<" leave the Right ChopStick\n" ;
ChopStick &cs=*rightHandSide;
cs.leavePhilsopher();
gotRightStick=false;
}
void Philosopher :: eat(){
int count=0;
std::cout <<" " << Name <<" is Eating\n" ;
while(count <= COUNT_HALT1 )
{
//pthread_cond_wait( &condition_cond, &condition_mutex );
//std::cout <<" " << count <<"" ;
count++;
//std::cout <<" " << count <<"\n" ;
}
}
ReplyDeletevoid Philosopher :: think(){
std::cout <<" " << Name <<" is Thinking\n" ;
int count=0;
while(count <= COUNT_HALT1 )
{
//pthread_cond_wait( &condition_cond, &condition_mutex );
count++;
//std::cout <<" " << count <<"\n" ;
}
}
void Philosopher ::run(){
Philosopher ps=*this;
ps.getleftChopstick();
if(ps.gotLeftStick){
ps.getrightChopstick();
if(ps.gotRightStick){
ps.eat();
ps.leaverightChopstick();
ps.leaveleftChopstick();
ps.think();
run();
}
else{
ps.leaveleftChopstick();
run();
}
}
else{
ps.getrightChopstick();
if(ps.gotRightStick){
ps.getleftChopstick();
if(ps.gotLeftStick){
ps.eat();
ps.leaverightChopstick();
ps.leaveleftChopstick();
ps.think();
run();
}
else{
ps.leaverightChopstick();
run();
}
}
else{
run();
}
}
}
using namespace std;
void *start(void *threadid)
{
Philosopher *pss=(Philosopher *)threadid;
Philosopher ps=*pss;
ps.run();//this call the run() for exicution
return threadid;
}
int main(){
int rc1, rc2,rc3, rc4,rc5;
pthread_t thread1, thread2,thread3, thread4, thread5;
ChopStick cs1("1");
ChopStick cs2("2");
ChopStick cs3("3");
ChopStick cs4("4");
ChopStick cs5("5");
Philosopher ps1(&cs1,&cs2,"Philosopher1");
Philosopher ps2(&cs2,&cs3,"Philosopher2");
Philosopher ps3(&cs3,&cs4,"Philosopher3");
Philosopher ps4(&cs4,&cs5,"Philosopher4");
Philosopher ps5(&cs5,&cs1,"Philosopher5");
Philosopher *pss1=&ps1;
Philosopher *pss2=&ps2;
Philosopher *pss3=&ps3;
Philosopher *pss4=&ps4;
Philosopher *pss5=&ps5;
rc1 = pthread_create( &thread1, NULL,start,pss1);//these will create the threads.
rc2 = pthread_create( &thread2, NULL,start,pss2);
rc3 = pthread_create( &thread3, NULL,start,pss3);
rc4 = pthread_create( &thread4, NULL,start,pss4);
rc5 = pthread_create( &thread5, NULL,start,pss5);
pthread_join( thread1, NULL);//these will wait main thread until above threads finshes.
pthread_join( thread2, NULL);
pthread_join( thread3, NULL);
pthread_join( thread4, NULL);
std::cout <<"rc1 " << rc1<<" " ;
std::cout <<"rc2 " << rc2<<" " ;
std::cout <<"rc3 " << rc3<<" " ;
std::cout <<"rc4 " << rc4<<" " ;
return 0;
}
I think this will help you.
:D