C++ 如何使线程包含使用互斥体控制流的循环?

C++ 如何使线程包含使用互斥体控制流的循环?,c++,multithreading,C++,Multithreading,我正在做家庭作业,我遇到了麻烦。分配是使用2个线程和控制互斥来在5次迭代的循环中减少一个数字x 第一个线程执行x=x-5 第二个线程执行x=x/5 在5次迭代中,正确的结果应该将x从19530减少到5,每次迭代时在thread1和thread2之间交替进行。 到目前为止,我有以下结果: 螺纹1:x=19525 螺纹1:x=19520 螺纹1:x=19515 螺纹1:x=19510 螺纹1:x=19505 从上面可以清楚地看到,我的第二个线程不仅没有完成它的工作,而且根本没有做任何事情 下面是我

我正在做家庭作业,我遇到了麻烦。分配是使用2个线程和控制互斥来在5次迭代的循环中减少一个数字x

第一个线程执行x=x-5
第二个线程执行x=x/5

在5次迭代中,正确的结果应该将x从19530减少到5,每次迭代时在thread1和thread2之间交替进行。

到目前为止,我有以下结果:

螺纹1:x=19525
螺纹1:x=19520
螺纹1:x=19515
螺纹1:x=19510
螺纹1:x=19505


从上面可以清楚地看到,我的第二个线程不仅没有完成它的工作,而且根本没有做任何事情

下面是我的代码,它是用C++编写的,但是样式是使用我们在课堂上学到的工具,这是C语言中的工具,但是它应该以相同的方式工作。p>

#include<iostream>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
using namespace std;

void *first(void *);
void *second(void *);

pthread_mutex_t mymutex = PTHREAD_MUTEX_INITIALIZER;

int x = 19530; // global var that is being manipulated
int i = 1; // counter for the loops

int main() {
    int t1, t2;
    pthread_t thread1, thread2;

    if((t1 = pthread_create( &thread1, NULL, first, NULL))) {
        printf("Thread creation failed: %d\n", t2);
    }

    if((t2 = pthread_create( &thread2, NULL, second, NULL))) {
        printf("Thread creation failed: %d\n", t2);
    }

    pthread_join(thread1, NULL);
    pthread_join(thread2, NULL);
    return(0);
}

void *first(void *){ // function for thread1
  for(; i <=5; i++){
  pthread_mutex_lock(&mymutex);
  x = x-5;
  cout << "Thread1: x = " << x << endl;
  pthread_mutex_unlock(&mymutex);
  }
}

void *second(void *){ // function for thread2
  for(; i<=5; i++){
  pthread_mutex_lock(&mymutex);
  x = x/5;
  cout << "Thread2: x = " << x << endl;
  pthread_mutex_unlock(&mymutex);
  }
}
#包括
#包括
#包括
#包括
使用名称空间std;
无效*第一(无效*);
无效*秒(无效*);
pthread\u mutex\u t mymutex=pthread\u mutex\u初始值设定项;
int x=19530;//被操纵的全局var
int i=1;//循环计数器
int main(){
int t1,t2;
pthread_t thread1,thread2;
if((t1=pthread_create(&thread1,NULL,first,NULL))){
printf(“线程创建失败:%d\n”,t2);
}
if((t2=pthread_create(&thread2,NULL,second,NULL))){
printf(“线程创建失败:%d\n”,t2);
}
pthread_join(thread1,NULL);
pthread_join(thread2,NULL);
返回(0);
}
void*第一个(void*){//thread1的函数

因为(;i感谢rclgdr在上面的评论,我能够回答我自己的问题,以下代码按预期工作:

#include<iostream>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
using namespace std;

void *first(void *);
void *second(void *);

pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; // thread1 mutex
pthread_mutex_t mutex2 = PTHREAD_MUTEX_INITIALIZER; // thread2 mutex

int x = 19530; // global var that is being manipulated

int main() {
    cout << "x = " << x << endl << endl;
    int t1, t2;
    pthread_t thread1, thread2;
    pthread_mutex_lock(&mutex2);
    if((t1 = pthread_create( &thread1, NULL, first, NULL))) {
        printf("Thread creation failed: %d\n", t2);
    }

    if((t2 = pthread_create( &thread2, NULL, second, NULL))) {
        printf("Thread creation failed: %d\n", t2);
    }

    pthread_join(thread1, NULL);
    pthread_join(thread2, NULL);
    return(0);
  }

void *first(void *){ // function for thread1
  for(int i = 1; i <=5; i++){
  pthread_mutex_lock(&mutex1);
  x = x-5;
  cout << "Iteration " << i <<endl;
  cout << "Thread1: x = " << x << endl;
  pthread_mutex_unlock(&mutex2);
  }
}

void *second(void *){ // function for thread2
  for(int i = 1; i<=5; i++){
  pthread_mutex_lock(&mutex2);
  x = x/5;
  cout << "Thread2: x = " << x << endl << endl;
  pthread_mutex_unlock(&mutex1);
  }
}
#包括
#包括
#包括
#包括
使用名称空间std;
无效*第一(无效*);
无效*秒(无效*);
pthread\u mutex\u t mutex1=pthread\u mutex\u初始值设定项;//thread1 mutex
pthread\u mutex\u t mutex2=pthread\u mutex\u初始值设定项;//thread2 mutex
int x=19530;//正在操作的全局变量
int main(){

我担心这段代码可能不能完全满足您的要求。线程1可以在线程2启动之前运行到完成-也称为“交替”不一定会发生。你需要重新考虑你的设计并实施它。不要为不相关的语言发送垃圾邮件!我有一种感觉是这样的。但我想我的基本问题是如何使thread1迭代,然后以调用thread2的方式退出。但我不知道如何做。强制交替的简单解决方案是使用两个互斥锁(或两个信号量)。@KazRodgers-
thread1解锁thread2和viceversa?
是的,每个线程都会有一个它等待的互斥锁,更新互斥锁,然后解锁另一个线程的互斥锁,然后返回并等待其互斥锁解锁。