Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8911295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:54:25+00:00 2026-06-15T03:54:25+00:00

I am trying to simulate an intersection using threads and mutex locks. I have

  • 0

I am trying to simulate an intersection using threads and mutex locks.

I have functions for going strait, turn left, turn right.
Now, i have a function for approaching the intersection. This generates a random orientation and turn. Each thread shares the approaching intersection.

I have all the locks defined for all the cars in all directions.

Take the going strait function. It has a switch statement that just prints what car is doing what at the time. Now, i am just not sure what to lock in this function. If the car is in direction pointing north would i lock east and west and same with the car pointing south going north?

Here is my locks which just calls a function to lock or unlock

#define NUMCARS 30

#define lock_NW(CAR) lock(CAR, NW_mutex)
#define lock_NE(CAR) lock(CAR, NE_mutex)
#define lock_SW(CAR) lock(CAR, SW_mutex)
#define lock_SE(CAR) lock(CAR, SE_mutex)

#define unlock_NW(CAR) unlock(CAR, NW_mutex)
#define unlock_NE(CAR) unlock(CAR, NE_mutex)
#define unlock_SW(CAR) unlock(CAR, SW_mutex)
#define unlock_SE(CAR) unlock(CAR, SE_mutex)

here is main

int main(int argc, char **argv){
/* Initial variables*/
int index, tid;
unsigned int carids[NUMCARS];
pthread_t carthreads[NUMCARS];

/* Start up a thread for each car*/ 
for(index = 0; index <NUMCARS; index++){
carids[index] = index;
tid = pthread_create(&carthreads[index], NULL, approachintersection,  (void*)&carids[index]);
}

/* Wait for every car thread to finish */
for(index = 0; index <NUMCARS; index++){
pthread_join(carthreads[index], NULL);
}
printf("Done\n");
return 1;
}

here is the approaching intersection which calls the function going strait

static void * approachintersection(void* arg){
unsigned int * carnumberptr;
unsigned int carnumber;
orientation_t cardir = (orientation_t)random()%4;
unsigned long turn = random()%3;

carnumberptr = (unsigned int*) arg;
carnumber = (unsigned int) *carnumberptr;

if(turn==LEFT){
turnleft(cardir, carnumber);
} else if(turn==RIGHT){
turnright(cardir, carnumber);
} else {//straight
gostraight(cardir, carnumber);
}

return (void*)carnumberptr;
}

Now, here is the going strait function where i want to lock the appropriate directions.

 /*
  cardirection - The direction the car is pointing.  If it is pointing NORTH,
  it is starting from the South-Eastern corner of the intersection
  and "going straight" means it wants to move SOUTH to NORTH.

  valid options: NORTH, SOUTH, EAST, WEST

 carnumber -    The car identifier
*/


static void gostraight(orientation_t cardirection, unsigned int carnumber){

switch(cardirection){
case NORTH:
printf("Car %d, Moving South-North\n", carnumber);
break;
case SOUTH:
printf("Car %d, Moving North-South\n", carnumber);
break;
case EAST:
printf("Car %d, Moving West-East\n", carnumber);
break;
case WEST:
printf("Car %d, Moving East-West\n", carnumber);
break;
}
}

So, if the approaching car is pointing north from south the car would be the SE car and i would lock case east, west print function with lock_SE(CAR)? preventing the other threads from coming in and printing? so i would lock unlock the print statements?

Or would i lock the whole switch statement?

** EDIT: would this be the way to do it? **

static void turnleft(orientation_t cardirection, unsigned int carnumber){

int CAR;
CAR = carnumber;


  switch(cardirection){
  case NORTH:
  lock_SE(CAR)
  printf("Car %d, Moving South-West\n", carnumber);
  unlock_SE(CAR)
  break;
  case SOUTH:
  lock_NW(CAR)
  printf("Car %d, Moving North-East\n", carnumber);
  unlock_NW(CAR)
  break;
  case EAST:
  lock_SW(CAR)
  printf("Car %d, Moving West-North\n", carnumber);
  unlock_SW(CAR)
  break;
  case WEST:
  lock_NE(CAR)
  printf("Car %d, Moving East-South\n", carnumber);
  unlock_NE(CAR)
  break;
  }

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T03:54:26+00:00Added an answer on June 15, 2026 at 3:54 am

    This is not an easy problem. I’ll try to show two solutions.

    First the obvious one: One mutex for the entire intersection, in the beginning of turnleft, turnright, gostraight add lock(car, intersection_mutex);, just before the end of each function release said mutex. This will only let one car in through the intersection at a time. The upside of this is that it’s easy to understand and will not result in dead locks. The downside is that a only one car can enter at a time but as we all know two cars travelling non-intersecting paths can enter without issues.
    Here’s an example of go_straight() (the others follow the same approach):

    static void gostraight(orientation_t cardirection, unsigned int carnumber){
        pthread_mutex_lock(&intersection_mutex);
        switch(cardirection){
            case NORTH:
                printf("Car %d, Moving South-North\n", carnumber);
                break;
            case SOUTH:
                printf("Car %d, Moving North-South\n", carnumber);
                break;
            case EAST:
                printf("Car %d, Moving West-East\n", carnumber);
                break;
            case WEST:
                printf("Car %d, Moving East-West\n", carnumber);
                break;
            }
        }
        pthread_mutex_unlock(&intersection_mutex);
    }
    

    To let more than one car in at a time we need a fine grained approach. The problem with a fine grained approach is that it’s much harder to implement and get correct. Both go_straight and turn_left needs to lock two mutexes (you could argue that turn left needs three..). So if you can’t acquire both mutexes you need to back off. Translating this into driving rules:

    you must not enter the intersection before you can exit it. 
    

    So, to go straight we must first acquire the mutex nearest to you, then the next one in your path to be able to exit. If we can’t get both we must release the one we have locked. If we don’t release it we will dead-lock.

    To do this I’d add two helper functions:

    static void lock_two(pthread_mutex_t *a, pthread_mutex_t *b) {
        while(1) { 
            pthread_mutex_lock(a);
            if(pthread_mutex_trylock(b) == 0) 
                break;
            else
            /* We must release the previously taken mutex so we don't dead lock the intersection */
                pthread_mutex_unlock(a);                            
            pthread_yield(); /* so we don't spin over lock/try-lock failed */
        }
    }
    static void unlock_two(pthread_mutex_t *a, pthread_mutex_t *b) {
        pthread_mutex_unlock(a);
        pthread_mutex_unlock(b);
    }
    

    Here’s my version of go straight:

    static void gostraight(orientation_t cardirection, unsigned int carnumber){  
        switch(cardirection){
            case NORTH:
                lock_two(&SE_mutex, &NE_mutex); 
                printf("Car %d, Moving South-North\n", carnumber);
                unlock_two(&SE_mutex, &NE_mutex); 
                break;
            case SOUTH:
                lock_two(&NW_mutex, &SW_mutex); 
                printf("Car %d, Moving North-South\n", carnumber);
                unlock_two(&NW_mutex, &SW_mutex); 
                break;
            case EAST:
                lock_two(&SW_mutex, &SE_mutex); 
                printf("Car %d, Moving West-East\n", carnumber);
                unlock_two(&SW_mutex, &SE_mutex); 
           break;
           case WEST:
                lock_two(&NE_mutex, &NW_mutex); 
                printf("Car %d, Moving East-West\n", carnumber);
                unlock_two(&NE_mutex, &NW_mutex); 
                break;
        }
    }
    

    turn_left would then need to follow the same approach.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to simulate a token ring using python with sockets, but I have
I am trying to simulate linux command ls using linux api from c. Looking
I'm trying to simulate rotating box using Newton Physics and OpenGL. This is what
I am trying to simulate a simple MIPS processor using behavior code in Verilog.
I have a 32GB memory machine and I want to simulate trying out one
I am trying to simulate a keyboard interface using buttons and labels... I understand
I'm trying to simulate a click on an anchor tag using jQuery. I've been
I am trying to simulate an asynchronous file upload using an iframe. Here is
I´m trying to simulate a table using only CSS and DIV. The problem is
I am trying to simulate the upper Macbook keys to any active app using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.