gpt4 book ai didi

使用 std::thread 的 C++ 多线程应用程序在 Windows 上运行良好,但在 Ubuntu 上运行不正常

转载 作者:行者123 更新时间:2023-11-28 02:15:14 25 4
gpt4 key购买 nike

我有一个使用 C++ std::thread 库为 Ubuntu 14.04 和 Windows 8.1 编写的有点简单的多线程应用程序。代码几乎完全相同,除了我使用操作系统各自的库 windows.h 和 unistd.h 来使用 Sleep/sleep 暂停执行一段时间。它们实际上都开始运行,而 Ubuntu 版本确实持续运行了一小段时间,但随后挂起。我对 sleep / sleep 函数使用了正确的参数,因为我知道 Windows sleep 需要几毫秒,而 Unix sleep 需要几秒钟。

我已经多次运行代码,在 Ubuntu 上它从来没有超过两分钟,而我在 Windows 上运行了两次,每次 20 分钟,然后多次,每次大约五分钟,看看我是否幸运。这仅仅是与线程库的不兼容,还是 sleep 没有按照我的想法做,或者其他什么?存在无限循环是因为这是一个学校项目,预计会在没有死锁或崩溃的情况下运行。

要点是,这是一个经过修改的 4 向 parking 站,先到的汽车不必减速 parking 。我们只需要一次让一辆车通过十字路口,需要 3 秒才能通过,因此 Sleep(3000),而且不必担心转弯。三个线程运行 spawnCars 函数,还有四个其他线程,每个线程监视 N、E、S 和 W 四个方向之一。我希望可以理解为什么我不能发布整个代码以防其他学生绊倒在此之上。这两个函数是唯一代码不同于顶部操作系统依赖库的地方。谢谢。

编辑:由于我刚刚发布了项目的所有代码,如果问题最终成为死锁,我是否可以请求您只这么说,而不发布深入的解决方案?我是新来的,所以如果这违背了 SO 的精神,那就开火吧,我会在不阅读细节的情况下尝试弄清楚。

        /* function clearIntersection
Makes a car go through the intersection. The sleep comes before the removal from the queue
because my understanding is that the wait condition simulates the go signal for drivers.
It wouldn't make sense for the sensors to tell a car to go if the intersection isn't yet
clear even if the lock here would prevent that.
*/
void clearIntersection(int direction)
{
lock->lock();
Sleep(3000);
dequeue(direction);
lock->unlock();
}
/* function atFront(int direction)
Checks whether the car waiting at the intersection from a particular direction
has permission to pass, meaning it is at the front of the list of ALL waiting cars.
This is the waiting condition.
*/
bool isAtFront(int direction)
{
lock->lock();
bool isAtFront = cardinalDirections[direction].front() == list->front();
lock->unlock();
return isAtFront;
}


void waitInLine()
{
unique_lock<mutex> conditionLock(*lock);
waitForTurn->wait(conditionLock);
conditionLock.unlock();
}
//function broadcast(): Let all waiting threads know they can check whether or not their car can go.
void broadcast()
{
waitForTurn->notify_all();
}
};

/* function monitorDirection(intersectionQueue,int,int)
Threads will run this function. There are four threads that run this function
in total, one for each of the cardinal directions. The threads check to see
if the car at the front of the intersectionQueue, which contains the arrival order
of cars regardless of direction, is the car at the front of the queue for the
direction the thread is assigned to monitor. If not, it waits on a condition
variable until it is the case. It then calls the function to clear the intersection.
Broadcast is then used on the condition variable so all drivers will check if they
are allowed to pass, which one will unless there are 0 waiting cars, waiting again if not the case.
*/
void monitorDirection(intersectionQueue *intersection, int direction, int id)
{
while (true) //Do forever to see if crashes can occur.
{
//Do nothing if there are no cars coming from this direction.
//Possibly add more condition_variables for each direction?
if (!intersection->empty(direction))
{
while (!intersection->isAtFront(direction))
intersection->waitInLine();
intersection->clearIntersection(direction);
cout << "A car has gone " << numberToDirection(direction) << endl;
//All cars at the intersection will check the signal to see if it's time to go so broadcast is used.
intersection->broadcast();
}
}
}

最佳答案

您的罪魁祸首可能是您的 while (!isAtFront(...)) 循环。如果在检查和随后调用 waitInLine() 之间安排了另一个线程,您的队列状态可能会发生变化,从而导致您的所有消费者线程最终等待。那时没有线程向您的 condition_variable 发送信号,因此它们将永远等待。

关于使用 std::thread 的 C++ 多线程应用程序在 Windows 上运行良好,但在 Ubuntu 上运行不正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34232304/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com