0

I am working on a homework assignment and I've hit a wall. The assignment is to use 2 threads with controlling mutex to reduce a number x over a loop of 5 iterations.

The first thread does x=x-5
The second thread does x = x/5

The proper result should reduce x to 5 from 19530 over 5 iterations alternating between thread1 and thread2 at each iteration.

I have the following result as of now:

Thread1: x = 19525
Thread1: x = 19520
Thread1: x = 19515
Thread1: x = 19510
Thread1: x = 19505


From above it is clear that my second thread is not only not doing it's job but it's not doing anything at all.

Below is my code, it is written in C++ but the style is using the tools we learned in class which is how one would do it in C but it should work the same either way.

#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); } } 

Note I am brand new to the concept threads and mutex. And I'd prefer to stick to the way I have learned in class which I believe is called "the C way".

8
  • 2
    I am afraid this code might not do exactly what you want. thread#1 can run to finish before thread#2 even starts - aka the "alternating" is not guaranteed to happen. You need to rethink your design and enforce it. Commented Feb 28, 2017 at 1:32
  • Don't spam tags for unrelated languages! Commented Feb 28, 2017 at 1:37
  • I had a feeling that was the case. But I guess the fundamental problem I have is how to make thread1 iterate and then exit in a way that calls thread2. But I have no idea how to do that. Commented Feb 28, 2017 at 1:38
  • 1
    The simple solution to force alternation is to use two mutexes (or two semaphores). Commented Feb 28, 2017 at 1:40
  • 1
    @KazRodgers - thread1 unlocks thread2 and viceversa? Yes, each thread would have a mutex that it wait for, update x, then unlock the other thread's mutex, then loop back and wait for it's mutex to get unlocked. Commented Feb 28, 2017 at 3:58

1 Answer 1

1

Thanks to rclgdr who commented above I was able to answer my own question the following code works as intended:

#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); } } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.