void thread1() { while (num <= 10) { unique_lock<mutex> locker(mut); // 使用 unique_lock 锁管理类,自动上锁和解锁 cout << "thread1: " << num << endl; num++; cond2.notify_one(); // 线程 1 打印完成,通知线程 2 打印 cond1.wait(locker); // 等待线程 2 打印完成再继续打印 }...
第一种信号量的方式 packagethread;importjava.util.concurrent.Semaphore;publicclassPrintNumber{//打印偶数staticSemaphoreprintEven=newSemaphore(1);//打印奇数staticSemaphoreprintOdd=newSemaphore(0);publicstaticvoidmain(String[]args)throwsInterruptedException{PrintEvenp1=newPrintEven();PrintOddp2=newPrintOdd();...
要求创建3个线程,分别打印ABC,共交替打印10次。 public class Main { // 以A开始的信号量,初...
要求创建3个线程,分别打印ABC,共交替打印10次。 public class Main { // 以A开始的信号量,初...