lock_guard

abcd7038 2020-06-01 11:50:39
lock_guard是一个模板。
但是,它有什么操作呢?
比如说,我想先创建它,但是先不用一个锁来初始化它,我又没有什么方法能在后面给他加上一个锁呢?
例如:

lock_guard<metux> p;
//我想在这里给p一个锁来管理

希望能有大神来解答~~~

顺带也发一些关于锁的一些较全面的文章~~~
先谢过了
...全文
149 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
这个例子很好,可以参考

// lock_guard example
#include <iostream> // std::cout
#include <thread> // std::thread
#include <mutex> // std::mutex, std::lock_guard
#include <stdexcept> // std::logic_error

std::mutex mtx;

void print_even(int x) {
if (x % 2 == 0) std::cout << x << " is even\n";
else throw (std::logic_error("not even"));
}

void print_thread_id(int id) {
try {
// using a local lock_guard to lock mtx guarantees unlocking on destruction / exception:
std::lock_guard<std::mutex> lck(mtx); // 这个就是加锁了,如果去掉这句,那么输出的顺序就是乱的。
print_even(id);
}
catch (std::logic_error&) {
std::cout << "[exception caught]\n";
}
}

int main()
{
std::thread threads[10];
// spawn 10 threads:
for (int i = 0; i < 10; ++i)
threads[i] = std::thread(print_thread_id, i + 1);

for (auto& th : threads) th.join();

return 0;
}
qybao 2020-06-01
  • 打赏
  • 举报
回复
引用 2 楼 abcd7038 的回复:
谢谢!
我的指针指向静态对象也可以吧?
但是他还有什么操作呢?
我怎么样给一个线程上锁呢?
谢谢您的回复!

怎么使用你自己看例子吧,它是在构造方法里上锁,在析构函数里解锁
https://blog.csdn.net/qq_37233607/article/details/79827466
smwhotjay 2020-06-01
  • 打赏
  • 举报
回复

先把函数过程式的api调用临界锁研究吃透。再封装成类
abcd7038 2020-06-01
  • 打赏
  • 举报
回复
引用 1 楼 qybao 的回复:
你可以查看它的构造函数,构造函数需要一个锁做为参数传入的,所以你没法这样定义 要不你就用指针的方法,在需要的时候才new lock_guard<metux> *p; p = new lock_guard<metux>(your_mutex);
谢谢! 我的指针指向静态对象也可以吧? 但是他还有什么操作呢? 我怎么样给一个线程上锁呢? 谢谢您的回复!
qybao 2020-06-01
  • 打赏
  • 举报
回复
你可以查看它的构造函数,构造函数需要一个锁做为参数传入的,所以你没法这样定义
要不你就用指针的方法,在需要的时候才new
lock_guard<metux> *p;
p = new lock_guard<metux>(your_mutex);
abcd7038 2020-06-01
  • 打赏
  • 举报
回复
引用 7 楼 zjq9931 的回复:
[quote=引用 6 楼 abcd7038 的回复:] 那您的意思是在函数中加锁吗? 如果这个程序我不加锁会发生什么?、 谢谢!
在线程中加锁了啊,输出之前必须取到锁,取不到锁就等待。 先创建的线程就先获得锁,后创建的线程依次等待锁。这样输出的顺序就是按顺序输出的。 注释里面写明了,不加锁的情况,输出顺序就是乱的。[/quote] 谢谢!
  • 打赏
  • 举报
回复
引用 6 楼 abcd7038 的回复:
那您的意思是在函数中加锁吗?
如果这个程序我不加锁会发生什么?、
谢谢!

在线程中加锁了啊,输出之前必须取到锁,取不到锁就等待。
先创建的线程就先获得锁,后创建的线程依次等待锁。这样输出的顺序就是按顺序输出的。
注释里面写明了,不加锁的情况,输出顺序就是乱的。
abcd7038 2020-06-01
  • 打赏
  • 举报
回复
引用 5 楼 zjq9931 的回复:
这个例子很好,可以参考

// lock_guard example
#include <iostream>       // std::cout
#include <thread>         // std::thread
#include <mutex>          // std::mutex, std::lock_guard
#include <stdexcept>      // std::logic_error

std::mutex mtx;

void print_even(int x) {
	if (x % 2 == 0) std::cout << x << " is even\n";
	else throw (std::logic_error("not even"));
}

void print_thread_id(int id) {
	try {
		// using a local lock_guard to lock mtx guarantees unlocking on destruction / exception:
		std::lock_guard<std::mutex> lck(mtx);  // 这个就是加锁了,如果去掉这句,那么输出的顺序就是乱的。
		print_even(id);
	}
	catch (std::logic_error&) {
		std::cout << "[exception caught]\n";
	}
}

int main()
{
	std::thread threads[10];
	// spawn 10 threads:
	for (int i = 0; i < 10; ++i)
		threads[i] = std::thread(print_thread_id, i + 1);

	for (auto& th : threads) th.join();

	return 0;
}
引用 5 楼 zjq9931 的回复:
这个例子很好,可以参考

// lock_guard example
#include <iostream>       // std::cout
#include <thread>         // std::thread
#include <mutex>          // std::mutex, std::lock_guard
#include <stdexcept>      // std::logic_error

std::mutex mtx;

void print_even(int x) {
	if (x % 2 == 0) std::cout << x << " is even\n";
	else throw (std::logic_error("not even"));
}

void print_thread_id(int id) {
	try {
		// using a local lock_guard to lock mtx guarantees unlocking on destruction / exception:
		std::lock_guard<std::mutex> lck(mtx);  // 这个就是加锁了,如果去掉这句,那么输出的顺序就是乱的。
		print_even(id);
	}
	catch (std::logic_error&) {
		std::cout << "[exception caught]\n";
	}
}

int main()
{
	std::thread threads[10];
	// spawn 10 threads:
	for (int i = 0; i < 10; ++i)
		threads[i] = std::thread(print_thread_id, i + 1);

	for (auto& th : threads) th.join();

	return 0;
}
那您的意思是在函数中加锁吗? 如果这个程序我不加锁会发生什么?、 谢谢!

64,678

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧