简体   繁体   English

我是否正确使用了mutex_trylock?

[英]Am I using the mutex_trylock correctly?

The racers should have an equal chance of winning. 赛车手应该有平等的获胜机会。 When I run the program the results seem to be correct, both racers win about half the time, but I dont think I am using the mutex_trylock correctly. 当我运行程序时,结果似乎是正确的,两个赛车手都赢了大约一半的时间,但是我认为我没有正确使用Mutex_trylock。 Is it actually doing anything the way with how I implemented it? 它实际上是如何实现我的方式的吗? I am new to C so I dont know a lot about this. 我是C语言的新手,所以我对此并不了解。

Program Description: We assume two racers, at two diagonally opposite corner of a rectangular region. 程序说明:我们假设在矩形区域的两个对角相对角上有两个赛车手。 They have to traverse along the roads along the peripheri of the region. 他们必须沿着该地区周边的道路穿越。 There are two bridges on two opposite sides of the rectangle. 矩形的两个相对侧上有两个桥。 In order to complete one round of traversal around this, the racers have to get pass for both the bridge at a time. 为了绕过此轮完成一轮遍历,赛车手必须一次通过两个桥梁。 The conditions of the race are 比赛条件是

1) Only one racer can get a pass at a time. 1)一次只有一名赛车手可以通过。

2) Before one starts one round, he has to request and get both the passes and then after finishing that round has to release the passes, and make new try to get those passes for the next round. 2)在一个人开始一个回合之前,他必须请求并获得两个通行证,然后在完成该回合后必须释放通行证,并进行新的尝试以获取下一轮的通行证。

3) Racer1 (R1) will acquire bridge-pass B1 first, then B0. 3)Racer1(R1)将首先获得B1,然后是B0。 R0 will acquire B0 and then B1. R0将先获取B0,然后再获取B1。

4) There is a maximum number of rounds prefixed. 4)最多可添加回合数。 Whoever reaches that number first will be the winner and the race will stop. 谁先达到那个数字谁将是赢家,比赛将停止。

This is how the situation looks before starting. 这是开始之前情况的样子。

                 B0
        R0-------- ~  -------------
        |                          |
        |                          |
        |                          |
        |                          |
        --------- ~  ------------- R1
                  B1


#include<stdio.h>
#include<pthread.h>
#include<stdlib.h>

#define THREAD_NUM 2  
#define MAX_ROUNDS 200000
#define TRUE  1 
#define FALSE 0

/* mutex locks for each bridge */
pthread_mutex_t B0, B1;

/* racer ID */
int r[THREAD_NUM]={0,1};

/* number of rounds completed by each racer */
int numRounds[THREAD_NUM]={0,0};

void *racer(void *); /* prototype of racer routine */

int main()
{
    pthread_t tid[THREAD_NUM];
    void *status;
    int i,j;

    /* create 2 threads representing 2 racers */
    for (i = 0; i < THREAD_NUM; i++)
   {
       /*Your code here */
       pthread_create(&tid[i], NULL, racer, &r[i]);

    }

    /* wait for the join of 2 threads */
     for (i = 0; i < THREAD_NUM; i++)
    {
         /*Your code here */
         pthread_join(tid[i], &status);
    }

    printf("\n");
    for(i=0; i<THREAD_NUM; i++)
        printf("Racer %d finished %d rounds!!\n", i, numRounds[i]);

 if(numRounds[0]>=numRounds[1]) printf("\n RACER-0 WINS.\n\n");
 else  printf("\n RACER-1 WINS..\n\n");

return (0);
}


void *racer(void  *arg)
{
  int  index = *(int*)arg, NotYet;

    while( (numRounds[0] < MAX_ROUNDS) && (numRounds[1] < MAX_ROUNDS) )
 {

   NotYet = TRUE;

    /* RACER 0 tries to get both locks before she makes a round */
   if(index==0){
     /*Your code here */
     pthread_mutex_trylock(&B0);
     pthread_mutex_trylock(&B1);



   }

    /* RACER 1 tries to get both locks before she makes a round */
   if(index==1){
      /*Your code here */
     pthread_mutex_trylock(&B1);
     pthread_mutex_trylock(&B0);
     }
    numRounds[index]++;      /* Make one more round */


    /* unlock both locks */
    pthread_mutex_unlock(&B0);
    pthread_mutex_unlock(&B1);

       /* random yield to another thread */

     }

printf("racer %d made %d rounds !\n", index, numRounds[index]);

pthread_exit(0);

}

when first thread locks B0 and if second get scheduled to lock B1, it will cause deadlock. 当第一个线程锁定B0时,如果第二个线程计划锁定B1,则它将导致死锁。 If first mutex is locked and second is not locked, then release first mutex and loop again. 如果第一个互斥锁已锁定而第二个互斥锁未锁定,则释放第一个互斥锁并再次循环。 This loop can be smaller if tried with mutex_lock and not trylock. 如果尝试使用Mutex_lock而不是trylock,则此循环会更小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM