我正在为路由器编写代码,我想在接收器线程上收到数据包后,将该数据包(目前为“unsigned char *”类型)存储在 vector 中。我有两个链接到两个不同接口(interface)的接收器线程,并且接收器线程应该仅在另一个线程尚未访问 vector 时存储收到的数据包。
除了两个接收线程之外,我还将有一个处理数据包的主线程和两个发送线程,每个接口(interface)一个。总线程数为 5。为了防止不同线程在 vector 中写入或读取时出现问题,我听说了互斥锁,据说它应该锁定其他(在本例中)线程的 vector ,直到有问题的线程之后已完成从 vector 的读取或写入。
我想知道这是如何处理成代码的。一些例子将非常感激。我对 C++、Boost 和 Raw Sockets 都很陌生。最重要的是,我还不太喜欢在 C++ 中使用不同的类。我非常了解它们在 Java 和 C# 中的工作原理,但我发现 C++ 非常不同(因为头文件)。我将所有代码都放在一个巨大的主类中,这可能不是很好的编码风格,但这就是它现在的工作方式。
我应该创建一个类(.h 和 .cpp 文件)来处理这个 vector ,还是应该在我的巨型主类中实现它,如果是这样,我会很高兴得到一些简单的例子。下面是我希望我的程序如何工作的示例。我已经有了 RecvThread,并且可以打印出数据包的 MAC Hdr。
void RecvThread()
{
//receive a packet
while(true)
{
//get lock status from vector
if(vector is locked)
{
usleep(10000);
}
else
{
//lock vector
//save packet in vector
//unlock vector
}
}
}
void SendThread()
{
while(true)
{
//get lock status from vector
if(vector is locked)
{
usleep(10000);
}
else
{
//lock vector
//get packet from vector
//unlock vector
}
}
//send the packet
}
void MainThread()
{
while(true)
{
if(recv vector is empty)
{
usleep(10000);
}
else if(recv vector is locked)
{
usleep(10000);
}
else
{
//lock recv vector
//get packet in front of queue
//unlock recv vector
//process packet (update different fields)
//get packet interface (either 1 or 2)
if(send vector (matching interface) is locked)
{
usleep(10000);
}
else
{
//lock send vector (matching interface)
//save packet to send vector (matching interface)
//unlock send vector (matching interface)
}
}
}
}
int main()
{
//create Recv vector
//create Send vector 1 and 2 (one for each interface)
//create and join RecvThread 1 and 2
//create and join SendThread 1 and 2
//create and join MainThread
}
最佳答案
希望这能给您带来启发。没有测试它,因为它显然不完整:)
关键是boost::lock_guard<boost::mutex> lock(mx_);
因为这一行一次只能由一个线程执行。实际发生的情况是,例如线程 A 调用此函数,因为它是第一个到达那里的线程 boost::lock_guard<boost::mutex> lock(mx_);
立即返回,线程可以进入所谓的临界区(锁之后的代码段)。基本上boost::lock_guard<boost::mutex> lock(mx_);
仅当临界区内没有其他线程时才返回。如果临界区已被另一个线程访问,boost::lock_guard<boost::mutex> lock(mx_);
直到另一个线程离开临界区才返回。为了让等待线程立即继续,需要解锁互斥体。在 boost 中,一旦阻塞线程(第一个进入该部分的线程)离开临界区的函数范围,就会自动完成此操作。离开作用域会破坏变量 mx_
它允许创建并锁定另一个同名的互斥体。
编辑:您不必专门锁定一个变量,例如 vector 。您锁定了访问该变量的代码段。
class Receiver{
private:
boost::mutex mx_;
int *data_; // I need protection
public:
bool receive_data(){
// Prepare connection and do some stuff...
void some_function(){
// Before we access data_, we need to make sure only the current thread does so.
// Mutex will unlock once the scope of this function is left
boost::lock_guard<boost::mutex> lock(mx_);
// Access data_ here (mutex is locked)
//....
} // End of scope and mutex unlocks automatically since it's destroyed
return true; // Will unlock the mutex since the function's scope is left.
}
};
关于c++ - 使用互斥体锁定 vector - Boost,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19513918/