pthread Mutexes

hey guys, i am trying to print "begin" then 11, i've been trying many different ways but I still get race condition, can anyone help? thanks.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
   
pthread_mutex_t  m1;  //declares m1 to be a mutex
int  x;
   
//------------------------------------------------------
void *thread1(void *arg)  {
     //pthread_mutex_lock(&m1);   
     pthread_mutex_unlock(&m1);

     printf("begin");  
     x=x+1;
     printf("\n%d \n", x);
    
     }
//------------------------------------------------------
   
//------------------------------------------------------
void *thread2(void *arg)  {
     pthread_mutex_lock(&m1);
     
     x=10;
     pthread_mutex_unlock(&m1);
     }
//------------------------------------------------------

//------------------------------------------------------
int main (void)  {
   pthread_t TId1, TId2;
   int status;
      
   pthread_mutex_init(&m1, NULL);  //MInitialize(m1)
   x=0;
      
   //create threads
   pthread_create(&TId1, NULL, thread1, NULL);
   pthread_create(&TId2, NULL, thread2, NULL);
      
   //wait for joinable threads to complete
   pthread_join(TId1, (void **)&status);
   pthread_join(TId2, (void **)&status);
      
  
   
   printf("push Enter when done looking\n"); getchar();
   pthread_exit(NULL);
}
//------------------------------------------------------ 
I think initializing mutexes in pthreads is a little more complicated than that.
1
2
3
4
5
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&mutex,&attr);
pthread_mutexattr_destroy(&attr);
this code is from my professor. It does work. The only thing is I need to make sure it prints out
begin
11

this is his question:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Consider:
   
do par
thread1:
do
print "begin";
x:=x+1;
print x;
end;  // thread1
   
thread2:
do
x:=10;
end;  // thread2
end par;
   
//Insert MInitialize(m), MRequest(m)'s, and MRelease(m)'s for a mutex m into the above code so that it is guaranteed to print:
   
begin
11
   
//Don't otherwise change the existing code - only insert the above function calls.  Hint: A thread may release a mutex without having requesting it.  You don't need to translate and run this. 


I've spent so many hours and asked so many people.. lol.. someone know this please help, thanks!
Ah, there it is.
Line 11: Don't unlock a mutex in a thread when that thread hasn't locked it.
As a rule of thumb, abs((number of locks)-(number of unlocks))<=(how many problems you have)
Just add a set of lock-unlock around line 14 and you should be fine.
ok thanks a lot Helios. You mean

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
do par
thread1:
do
print "begin";
x:=x+1;
print x;
end;  // thread1
   
thread2:
do
MRequest(m);
x:=10;
MRelease(m);
end;  // thread2
end par;


? because line 14, that's end;
Last edited on
Topic archived. No new replies allowed.