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; } }
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.
When asked, what has been your best career decision?
Deciding to stick with EE.
Being involved with EE helped me to grow personally and professionally.
Connect with Certified Experts to gain insight and support on specific technology challenges including:
We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE