如何让一个pthread在另一个线程等待C++;? 我现在正在复习一个教授在我们当前的任务中用C++中的信号量和线程处理的例子。当前,当其中一个线程被阻塞时,整个程序将等待 #include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <semaphore.h> #include <unistd.h> #include <iostream> using namespace std; int account = 99; bool sent = false; int rate = 12; int hours = 15; sem_t s1; sem_t s2; //work thread void *work(void*){ while(1){ sem_wait(&s1); account += hours * rate; cout << "Account: " << account << endl; if(account >= 1000 && !sent){ sem_post(&s2); sent = true; } sem_post(&s1); pthread_exit(NULL); } } void* buy(void*){ while(1){ sem_wait(&s2); sem_wait(&s1); account -= 1000; sent = false; cout << "iPhone bought!! Account: " << account << endl; sem_post(&s1); pthread_exit(NULL); } } int main(){ pthread_t workt, buyt; sem_init(&s1, 0, 1); sem_init(&s2, 0, 0); while(1){ pthread_create( &workt, NULL, work, NULL); pthread_create( &buyt, NULL, buy, NULL); pthread_join(workt, NULL); pthread_join(buyt, NULL); } sem_close(&s1); sem_close(&s2); pthread_exit(NULL); } #包括 #包括 #包括 #包括 #包括 #包括 使用名称空间std; 国际账户=99; bool-sent=false; 积分率=12; 整小时=15; sem_t s1; sem_t s2; //工作线程 无效*工作(无效*){ 而(1){ sem_wait(&s1); 账户+=小时*费率; cout

如何让一个pthread在另一个线程等待C++;? 我现在正在复习一个教授在我们当前的任务中用C++中的信号量和线程处理的例子。当前,当其中一个线程被阻塞时,整个程序将等待 #include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <semaphore.h> #include <unistd.h> #include <iostream> using namespace std; int account = 99; bool sent = false; int rate = 12; int hours = 15; sem_t s1; sem_t s2; //work thread void *work(void*){ while(1){ sem_wait(&s1); account += hours * rate; cout << "Account: " << account << endl; if(account >= 1000 && !sent){ sem_post(&s2); sent = true; } sem_post(&s1); pthread_exit(NULL); } } void* buy(void*){ while(1){ sem_wait(&s2); sem_wait(&s1); account -= 1000; sent = false; cout << "iPhone bought!! Account: " << account << endl; sem_post(&s1); pthread_exit(NULL); } } int main(){ pthread_t workt, buyt; sem_init(&s1, 0, 1); sem_init(&s2, 0, 0); while(1){ pthread_create( &workt, NULL, work, NULL); pthread_create( &buyt, NULL, buy, NULL); pthread_join(workt, NULL); pthread_join(buyt, NULL); } sem_close(&s1); sem_close(&s2); pthread_exit(NULL); } #包括 #包括 #包括 #包括 #包括 #包括 使用名称空间std; 国际账户=99; bool-sent=false; 积分率=12; 整小时=15; sem_t s1; sem_t s2; //工作线程 无效*工作(无效*){ 而(1){ sem_wait(&s1); 账户+=小时*费率; cout,c++,multithreading,pthreads,semaphore,C++,Multithreading,Pthreads,Semaphore,在work中循环的每次迭代中调用pthread\u exit(NULL)。基本上就像没有循环一样 也许你的意思更像: while(!sent){ sem_wait(&s1); account += hours * rate; cout << "Account: " << account << endl; if(account >= 1000){ sem_post(&s

work
中循环的每次迭代中调用
pthread\u exit(NULL)
。基本上就像没有循环一样

也许你的意思更像:

  while(!sent){
      sem_wait(&s1);
      account += hours * rate;
      cout << "Account: " << account << endl;
      if(account >= 1000){
          sem_post(&s2);
          sent = true;
      }
      sem_post(&s1);

  }
  pthread_exit(NULL);
while(!sent){
sem_wait(&s1);
账户+=小时*费率;

cout在
work
中循环的每次迭代中调用
pthread\u exit(NULL)
。基本上就像没有循环一样

也许你的意思更像:

  while(!sent){
      sem_wait(&s1);
      account += hours * rate;
      cout << "Account: " << account << endl;
      if(account >= 1000){
          sem_post(&s2);
          sent = true;
      }
      sem_post(&s1);

  }
  pthread_exit(NULL);
while(!sent){
sem_wait(&s1);
账户+=小时*费率;

谢谢!!我可能也误解了他,但是有没有办法让
work
buy
等待的时候继续创建和执行线程?@cspear-work永远不会创建线程-它是一个只创建一次的线程。while(1)是它不断进行“工作”添加的原因我想如果我想实现我所说的,我需要在
main()中使用不同的循环和逻辑
围绕
pthread\u create
初始化。再次感谢!@cspear是的,这听起来像是创建多个线程来运行
work
函数的方法,确实可以让它工作。谢谢!!我可能也误解了他,但是有没有办法让
work
继续创建和执行线程当
buy
正在等待时?@cspear work永远不会创建线程-它是一个只创建一次的线程。while(1)是它一直在做“工作”向帐户中添加“钱”的原因。是的。我想如果我想实现我所说的,我需要在
main()中使用不同的循环和逻辑
围绕
pthread\u create
初始化。再次感谢!@cspear是的,这听起来像是创建多个线程来运行
work
函数的方法