We help IT Professionals succeed at work.
Get Started
Troubleshooting Question

Need help with this problem

Alan Quispe
Alan Quispe asked
on
53 Views
Last Modified: 2020-10-18
NEED HELP PLS WITH THIS PROBLEM FOR C++:
"A single-lane bridge connects the two Vermont villages of North Tunbridge and South Tunbridge. Farmers in the two villages use this bridge to deliver their produce to the neighboring town. The bridge can become deadlocked if both a northbound and a southbound farmer get on the bridge at the same time (Vermont farmers are stubborn and are unable to back up.) Using SEMAPHORES, design an algorithm that prevents deadlock. "
I have the code in java but I need it in c ++, please if someone could help me, thanks.
***********************************************************
package threading.practice;


import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;


public class SingleLaneBridge {


public static void main(String[] args)
{
final Bridge bridge = new Bridge();


Thread thNorthbound = new Thread( new Runnable() {


@Override
public void run() {


while(true)
{
Farmer farmer = new Farmer(bridge);
Thread th = new Thread(farmer);
farmer.setName("North Farmer : "+th.getId());
th.start();
try
{
TimeUnit.SECONDS.sleep((long)(Math.random()*10));
}
catch(InterruptedException iex)
{
iex.printStackTrace();
}
}


}
});


Thread thSouthbound = new Thread( new Runnable() {


@Override
public void run() {


while(true)
{
Farmer farmer = new Farmer(bridge);
Thread th = new Thread(farmer);
farmer.setName("South Farmer : "+th.getId());
th.start();
try
{
TimeUnit.SECONDS.sleep((long)(Math.random()*10));
}
catch(InterruptedException iex)
{
iex.printStackTrace();
}
}
}
});


thNorthbound.start();
thSouthbound.start();
}


}


class Bridge
{
private final Semaphore semaphore;


public Bridge()
{
semaphore = new Semaphore(1);
}
public void crossBridge(Farmer farmer)
{
try
{
System.out.printf("Farmer %s is trying to cross the bridge.\n",farmer.getName());
semaphore.acquire();
System.out.printf("Farmer %s is crossing the bridge.\n",farmer.getName());
long duration = (long)(Math.random() * 10);
TimeUnit.SECONDS.sleep(duration);
}
catch(InterruptedException iex)
{
iex.printStackTrace();
}
finally
{
System.out.printf("Farmer %s has crossed the bridge.\n",farmer.getName());
semaphore.release();
}
}
}


class Farmer implements Runnable
{
private String name;
private Bridge bridge;


public Farmer(Bridge bridge)
{
this.bridge = bridge;
}


public void run()
{
bridge.crossBridge(this);
}


public String getName() {
return name;
}


public void setName(String name) {
this.name = name;
}


}
Comment
Watch Question
This question hasn't been answered yet.
While you wait, find out how you can maximize your capability with the most trusted tool in IT.
Explore More Ask a Question
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE