如何在c+中使用/创建唯一的_锁+;? 请大家解释一下如何在C++中使用和创建一个UNIQUE锁? 应该使用它来实现监视器的任何过程的互斥,并能够对条件变量执行wait()。从文档中我不了解应该如何创建它。是否需要一个互斥体?下面是一个伪代码: /* compile with g++, flags -std=c++0x -lpthread */ #include <condition_variable> #include <mutex> #include <thread> #include <iostream> #include <string.h> #include <unistd.h> class monitorTh { private: std::mutex m; std::condition_variable waitP; std::condition_variable waitC; char element[32]; std::unique_lock::unique_lock l; public: void produce(char* elemProd) { l.lock(); if (/*already_present_element*/) { waitP.wait(l); } else {/*produce element*/} l.unlock(); } void consume() { /*something specular*/ } }; int main(int argc, char* argv[]) { monitorTh* monitor = new monitorTh(); char prodotto[32] = "oggetto"; std::thread producer([&]() { monitor->produce(prodotto); }); std::thread consumer([&]() { monitor->consume(); }); producer.join(); consumer.join(); } /*使用g++编译,标志-std=c++0x-lpthread*/ #包括 #包括 #包括 #包括 #包括 #包括 类监视器{ 私人: std::互斥m; std::条件变量waitP; std::条件变量waitC; char元素[32]; std::unique_lock::unique_lock l; 公众: 无效生成(char*elemProd){ l、 锁(); 如果(/*已存在\元素*/){ 等待,等待(l); } else{/*生成元素*/} l、 解锁(); } 无效消耗(){ /*镜面反射的东西*/ } }; int main(int argc,char*argv[]){ monitorTh*监视器=新的monitorTh(); char prodotto[32]=“oggetto”; 线程生成器([&](){ 监控->生产(生产); }); std::线程使用者([&](){ 监视器->消费(); }); producer.join(); consumer.join(); }
在这种情况下,我认为您需要做的就是:如何在c+中使用/创建唯一的_锁+;? 请大家解释一下如何在C++中使用和创建一个UNIQUE锁? 应该使用它来实现监视器的任何过程的互斥,并能够对条件变量执行wait()。从文档中我不了解应该如何创建它。是否需要一个互斥体?下面是一个伪代码: /* compile with g++, flags -std=c++0x -lpthread */ #include <condition_variable> #include <mutex> #include <thread> #include <iostream> #include <string.h> #include <unistd.h> class monitorTh { private: std::mutex m; std::condition_variable waitP; std::condition_variable waitC; char element[32]; std::unique_lock::unique_lock l; public: void produce(char* elemProd) { l.lock(); if (/*already_present_element*/) { waitP.wait(l); } else {/*produce element*/} l.unlock(); } void consume() { /*something specular*/ } }; int main(int argc, char* argv[]) { monitorTh* monitor = new monitorTh(); char prodotto[32] = "oggetto"; std::thread producer([&]() { monitor->produce(prodotto); }); std::thread consumer([&]() { monitor->consume(); }); producer.join(); consumer.join(); } /*使用g++编译,标志-std=c++0x-lpthread*/ #包括 #包括 #包括 #包括 #包括 #包括 类监视器{ 私人: std::互斥m; std::条件变量waitP; std::条件变量waitC; char元素[32]; std::unique_lock::unique_lock l; 公众: 无效生成(char*elemProd){ l、 锁(); 如果(/*已存在\元素*/){ 等待,等待(l); } else{/*生成元素*/} l、 解锁(); } 无效消耗(){ /*镜面反射的东西*/ } }; int main(int argc,char*argv[]){ monitorTh*监视器=新的monitorTh(); char prodotto[32]=“oggetto”; 线程生成器([&](){ 监控->生产(生产); }); std::线程使用者([&](){ 监视器->消费(); }); producer.join(); consumer.join(); },c++,c++11,locking,C++,C++11,Locking,在这种情况下,我认为您需要做的就是: m.lock(); // Critical section code m.unlock(); std::unique_lock使用该模式 要锁定互斥锁时,可以创建一个类型为std::unique\u lock的局部变量,将互斥锁作为参数传递。当构造唯一的_锁时,它将锁定互斥锁,当它被破坏时,它将解锁互斥锁。更重要的是:如果抛出异常,将调用std::unique_lock析构函数,从而解锁互斥锁 例如: #include<mutex> int s
m.lock();
// Critical section code
m.unlock();
std::unique_lock
使用该模式
要锁定互斥锁时,可以创建一个类型为std::unique\u lock
的局部变量,将互斥锁作为参数传递。当构造唯一的_锁时,它将锁定互斥锁,当它被破坏时,它将解锁互斥锁。更重要的是:如果抛出异常,将调用std::unique_lock
析构函数,从而解锁互斥锁
例如:
#include<mutex>
int some_shared_var=0;
int func() {
int a = 3;
{ //Critical section
std::unique_lock<std::mutex> lock(my_mutex);
some_shared_var += a;
} //End of critical section
}
#包括
int some_shared_var=0;
int func(){
INTA=3;
{//临界截面
std::unique_lock(我的互斥锁);
某些_共享_var+=a;
}//临界段结束
}
std::unique\u lock
在一个单独的std::mutex
对象上持有一个锁。通过在构造函数中传递锁对象,可以将锁对象与互斥体相关联。除非另行指定,否则互斥锁将立即锁定。如果锁对象在被销毁时持有锁,则析构函数将释放锁。通常,std::unique_lock
对象将是一个局部变量,在您希望获取锁的位置声明
在您的例子中,product()
函数可以这样编写:
void produce(char* elemProd) {
std::unique_lock<std::mutex> lk(m); // lock the mutex
while (/*already_present_element*/) { // condition variable waits may wake spuriously
waitP.wait(lk);
}
{/*produce element*/}
// lk releases the lock when it is destroyed
}
void生成(char*elemProd){
std::unique_lock lk(m);//锁定互斥锁
而(/*已存在\元素*/){///条件变量等待可能会错误唤醒
等待,等待(lk);
}
{/*生成元素*/}
//lk在锁被破坏时释放锁
}
请注意,我已将
if
替换为while
,以解释wait()
调用产生的虚假唤醒。使用条件变量的更详细示例代码:
#include<mutex>
std::mutex(mu); //Global variable or place within class
std::condition_variable condition; //A signal that can be used to communicate between functions
auto MyFunction()->void
{
std::unique_lock<mutex> lock(mu);
//Do Stuff
lock.unlock(); //Unlock the mutex
condition.notify_one(); //Notify MyOtherFunction that this is done
}
auto MyOtherFunction()->void
{
std::unique_lock<mutex> lock(mu);
condition.wait(lock) //Wait for MyFunction to finish, a lambda can be passed also to protects against spurious wake up e.g (lock,[](){return *some condition*})
lock.unlock();
}
#包括
std::互斥(mu)//全局变量或类中的位置
std::条件\可变条件//一种可用于功能间通信的信号
自动MyFunction()->void
{
std::唯一锁(mu);
//做事
lock.unlock();//解锁互斥锁
condition.notify_one();//通知MyOtherFunction此操作已完成
}
自动MyOtherFunction()->void
{
std::唯一锁(mu);
condition.wait(lock)//等待MyFunction完成,也可以传递lambda来防止虚假唤醒,例如(lock,[](){return*some condition*})
lock.unlock();
}
您有一个else
而没有if
。不需要显式解锁。查看unique_lock()的行为方式。@Prab,请给出解释:unique_lock()调用析构函数时自动释放,这意味着它是异常安全的,当您离开作用域时它会自动解锁。@EliSquared您在重复我在对author@pcodexCPPPreference说明了显式解锁:“手动解锁是在通知之前完成的,以避免唤醒等待的线程而再次阻塞(有关详细信息,请参阅notify_one)“@pcodex在第二种情况下我同意,但如果性能很重要,第一种情况对我来说似乎有效。否则,在解锁互斥锁之前,可能会通知该条件。或者我遗漏了什么?为什么不需要一个new
关键字来创建锁?@ProQ它是在堆栈上创建的。如果您使用了new关键字,当您离开作用域时,它不会被破坏。