用螺纹模拟火车

noo*_*nee 4 c multithreading pthreads

编辑:我认为我做错了,因为当我编译并运行我的二进制文件两次时,我得到不同的输出..

我正在尝试理解线程pthread所以我做了一些代码来模拟火车在桥上的通道(一次只能处理2列火车)

我设法只用一列火车一次过桥,用这样的代码:

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

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

void    *train()
{
  int   km;
  static int    t = 0;

  km = 1;
  while (km != 10)
    {
      printf("I'm at %02d km\n", km++);
      sleep(1);
      if (km == 2)
        pthread_mutex_lock(&mutex);
      if (km == 4)
        pthread_mutex_unlock(&mutex);
    }
}

int     main()
{
  pthread_t     train1;
  pthread_t     train2;
  pthread_t     train3;

  pthread_create(&train1, NULL, train, NULL);
  pthread_create(&train2, NULL, train, NULL);
  pthread_create(&train3, NULL, train, NULL);

  pthread_join(train1, NULL);
  pthread_join(train2, NULL);
  pthread_join(train3, NULL);
}
Run Code Online (Sandbox Code Playgroud)

这个工作完美(或者我做错了,如果有的话请告诉我)

然后我尝试了2列火车可以同时通过桥的情况.

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

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

void    *train()
{
  int   km;
  static int    t = 0;

  km = 1;
  while (km != 30)
    {
      printf("I'm at %02d km\n", km++);
      sleep(1);
      if (km == 2 && t <= 2)
        {
          ++t;
          pthread_mutex_lock(&mutex);
        }
      if (km == 4)
        {
          t--;
          pthread_mutex_unlock(&mutex);
        }
    }
}

int     main()
{
  pthread_t     train1;
  pthread_t     train2;
  pthread_t     train3;
  pthread_t     train4;

  pthread_create(&train1, NULL, train, NULL);
  pthread_create(&train2, NULL, train, NULL);
  pthread_create(&train3, NULL, train, NULL);
  pthread_create(&train4, NULL, train, NULL);

  pthread_join(train1, NULL);
  pthread_join(train2, NULL);
  pthread_join(train3, NULL);
  pthread_join(train4, NULL);
}
Run Code Online (Sandbox Code Playgroud)

所以我用一个静态的int来把我的火车"盖"到桥上的2,但是那不起作用,我不明白为什么......

我的4列火车正在站着,然后2列车上火车,另外2列等候,当第2个第一次离开桥时,只有一个继续前行,最后一个等待'直到第3个离开桥去...

实际上,我想要4列火车去,2去桥,2等等,当第2次离开桥时,另外2列去了桥.

对不起它有点长,但需要理解我猜.

谢谢你的帮助 !

Jac*_*ack 6

你的代码的问题是所有的pthread_mutex_lock(&mutex);都是等到mutex不再被锁定,然后锁定它.它实际上并没有检查它的价值t.

你真正想要的是一个条件变量:

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

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

void    *train()
{
    int   km;
    static int t = 0;

    km = 1;
    while (km != 30)
    {
        printf("I'm at %02d km\n", km);

        // If we've reached the bridge, wait until less than two trains are on it.
        if (km == 2)
        {
            pthread_mutex_lock(&mutex);
            while (t == 2) { // To be read as: "While two trains are on the bridge, wait."
                pthread_cond_wait(&cond, &mutex);
            }
            ++t; // Put this train onto the bridge.
            pthread_mutex_unlock(&mutex);
        }

        // Leave the bridge.
        if (km == 4)
        {
            pthread_mutex_lock(&mutex);
            --t; // Take this train off the bridge.
            pthread_cond_signal(&cond); // Signal another train to enter.
            pthread_mutex_unlock(&mutex);
        }

        // Move forward 1 km.
        sleep(1);
        ++km;
    }
}

int     main()
{
    pthread_t     train1;
    pthread_t     train2;
    pthread_t     train3;
    pthread_t     train4;

    pthread_create(&train1, NULL, train, NULL);
    pthread_create(&train2, NULL, train, NULL);
    pthread_create(&train3, NULL, train, NULL);
    pthread_create(&train4, NULL, train, NULL);

    pthread_join(train1, NULL);
    pthread_join(train2, NULL);
    pthread_join(train3, NULL);
    pthread_join(train4, NULL);
}
Run Code Online (Sandbox Code Playgroud)

if (km == 4)位额外锁定和解锁的关键是确保没有多个线程试图立即更改/检查值t.(多个线程一次访问同一个变量通常会导致非常错误的行为!)

  • 哦,好的,谢谢你的答案,有点失落,只是你澄清了我想要的一切,再次感谢! (2认同)