TokeyRoad

Never underestimate your power to change yourself!


  • 首页

  • 标签

  • 分类

  • 归档

重装系统之后hexo博客恢复

发表于 2020-11-22 | 分类于 环境配置
字数统计: 552 | 阅读时长 ≈ 2

1.安装node.js和git

这个不用多说,直接下载安装就行了。

2.配置 git 个人信息,生成新的 ssh 密钥:

git config –global user.name “xxxxxx”
git config –global user.email “xxxxxx”
ssh-keygen -t rsa -C “xxxxxxxx(邮箱)”

3.添加公钥

在用户文件夹.ssh 文件里面把公钥复制出来粘贴到github个人设置的ssh位置。

4.安装hexo

npm install hexo-cli -g

5.删除博客文件夹文件,保留部分

必须拷贝文件:
├──_config.yml
├── theme
├── scaffolds #文章模板
├── package.json #说明使用哪些包
├── .gitignore #限定在提交的时候哪些文件可以忽略
└── source

阅读全文 »

智能指针类

发表于 2020-05-29 | 分类于 C++
字数统计: 2.8k | 阅读时长 ≈ 12

std::auto_ptr

基本用法

1
2
3
4
5
//第一种形式
std::auto_ptr<int> ap1(new int(1));
//第二种形式
std::auto_ptr<int> ap2;
ap2.reset(new int(1));

智能指针对象ap1和ap2均持有一个在堆上分配int对象,这两块堆内存在对象释放时得以释放。

std::auto_ptr 经常误用的是复制操作,当复制auto_ptr对象时(拷贝复制或operator=复制),原对象的堆内存会转移到复制的对象上,原指针就空了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
	std::auto_ptr<int> ap1(new int(1));
//拷贝复制
std::auto_ptr<int> ap2(ap1);
if (ap1.get()!= NULL) {
std::cout << "ap1 is not NULL" << std::endl;
}
else {
std::cout << "ap1 is NULL" << std::endl;
}
if (ap2.get() != NULL) {
std::cout << "ap2 is not NULL" << std::endl;
}
else {
std::cout << "ap2 is NULL" << std::endl;
}
//operator=复制
std::auto_ptr<int> ap3(new int(1));
std::auto_ptr<int> ap4 = ap3;
if (ap3.get() != NULL) {
std::cout << "ap3 is not NULL" << std::endl;
}
else {
std::cout << "ap3 is NULL" << std::endl;
}
if (ap4.get() != NULL) {
std::cout << "ap4 is not NULL" << std::endl;
}
else {
std::cout << "ap4 is NULL" << std::endl;
}

//output
ap1 is NULL
ap2 is not NULL
ap3 is NULL
ap4 is not NULL

ap1的堆内存转移到了ap2,ap3的堆内存转移到了ap4

因此在使用中,应尽量避免使用std::auto_ptr(C++11之后 该指针已被弃用,不应该再使用它)

阅读全文 »

原子操作类-atomic

发表于 2020-05-22 | 分类于 C++
字数统计: 442 | 阅读时长 ≈ 1

C++ 11 新标准中提供了对整形变量原子操作的相关库,即std::atomic,这是个模板类型:(同时支持跨平台)

1
2
template<class T>
struct atomic;

注意:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <atomic>
#include <stdio.h>
int main() {
std::atomic<int> ato_int;
ato_int = 1;
//std::atomic<int> ato_int = 1;
printf("%d \n", (int)ato_int);

++ato_int;
printf("%d \n", (int)ato_int);
system("pause");
return 0;
}

以上代码在win和linux中都可以运行,但是如果这样:

1
2
3
//std::atomic<int> ato_int;
//ato_int = 1;
std::atomic<int> ato_int = 1;

就只能在win下运行,linux编译无法通过。

阅读全文 »

线程资源同步对象-condition_variable

发表于 2020-05-20 | 分类于 C++
字数统计: 454 | 阅读时长 ≈ 2

C++ 11提供了std::condition_variable这个类 代表条件变量,与Linux系统原生的条件变量一样,同时还提供了等待条件变量的一系列wait方法(wait/wait_for/wait_until),发送信号使用notify方法(notify_one/notify_all),使用std::condition_variable时需要绑定到std::unique_lock或std::lock_guard对象。

C++ 11中 std::condition_variable 不再需要显示调用方法初始化和销毁

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <iostream>
#include <mutex>
#include <shared_mutex>
#include <thread>
#include <stdio.h>
#include <list>


class Task {
public:
Task(int id) {
m_taskID = id;
}
~Task() = default;

void DoTask() {
std::cout << "do task,taskID:" << m_taskID << ", threadID:" << std::this_thread::get_id() << std::endl;
}
private:
int m_taskID;
};

std::mutex mutex;
std::condition_variable cv;
std::list<Task*> tasks;

void produce_thread() {
Task* pTask = NULL;
int taskID = 0;
while (1) {
pTask = new Task(taskID);
{
std::lock_guard<std::mutex> lg(mutex);
tasks.push_back(pTask);
std::cout << "produce a task,taskID:" << taskID << ",threadID:" << std::this_thread::get_id() << std::endl;
}
cv.notify_all();
taskID++;
std::this_thread::sleep_for(std::chrono::seconds(1));
}
return;
}

void consume_thread() {
Task* pTask = NULL;
int taskID = 0;
while (1) {
std::unique_lock<std::mutex> guard(mutex);
while (tasks.empty()) {
cv.wait(guard);
}
pTask = tasks.front();
tasks.pop_front();
if (pTask == NULL) {
continue;
}
pTask->DoTask();
delete pTask;
pTask = NULL;
}
return;
}


int main() {
std::thread consume1 = std::thread(consume_thread);
std::thread consume2 = std::thread(consume_thread);
std::thread consume3 = std::thread(consume_thread);
std::thread consume4 = std::thread(consume_thread);
std::thread consume5 = std::thread(consume_thread);

std::thread produce1 = std::thread(produce_thread);
//std::thread produce2 = std::thread(produce_thread);
//std::thread produce3 = std::thread(produce_thread);

consume1.join();
consume2.join();
consume3.join();
consume4.join();
consume5.join();
produce1.join();
//produce2.join();
//produce3.join();

system("pause");
return 0;
}
//output:
produce a task,taskID:0,threadID:19444
do task,taskID:0, threadID:6344
produce a task,taskID:1,threadID:19444
do task,taskID:1, threadID:6352
produce a task,taskID:2,threadID:19444
do task,taskID:2, threadID:4836
produce a task,taskID:3,threadID:19444
do task,taskID:3, threadID:6352
produce a task,taskID:4,threadID:19444
do task,taskID:4, threadID:6344
produce a task,taskID:5,threadID:19444
do task,taskID:5, threadID:4836
produce a task,taskID:6,threadID:19444
do task,taskID:6, threadID:13436
produce a task,taskID:7,threadID:19444
do task,taskID:7, threadID:4836
.........

线程资源同步对象-mutex

发表于 2020-05-20 | 分类于 C++
字数统计: 895 | 阅读时长 ≈ 4

在C++ 11新标准中新增了线程资源同步对象:std::mutex 和 std::condition_variable 。

std::mutex 系列

C++ 11/14/17中提供了如下mutex系列类型:

互斥量 版本 作用
mutex C++ 11 最基本的互斥量
timed_mutex C++ 11 有超时机制的互斥量
recursive_mutex C++ 11 可重入的互斥量
recursive_timed_mutex C++ 11 结合timed_mutex和recursive_mutex特点的互斥量
shared_timed_mutex C++ 14 具有超时机制的可共享互斥量
shared_mutex C++ 17 共享的互斥量

这个系列的对象均提供了加锁(lock)、尝试加锁(trylock) 和 解锁(unlock) 的方法。

为了避免死锁, std::mutex.lock() 和 std::mutex.unlock() 需要成对使用,同时C++11提供了一些互斥量管理的封装方法,避免忘记unlock而造成死锁。

互斥量管理 版本 作用
lock_guard C++ 11 基于作用域的互斥量管理
unique_lock C++ 11 更加灵活的互斥量管理
shared_lock C++ 14 共享互斥量的管理
scope_lock C++ 17 多互斥量避免死锁的管理

示例:

1
2
3
4
void func(){
std::lock_guard<std::mutex> lg(mymutex);
//保护的资源操作
}

注意:mymutex的声明周期必须比函数func的作用域长。

阅读全文 »
1234…10
Tokey

Tokey

48 日志
9 分类
41 标签
  • C++8
  • Const1
  • Final1
  • HTTP2
  • Redis8
  • SDS1
  • STL4
  • TCP1
  • args1
  • atomic1
  • condition_variable1
  • construct1
  • foreach1
  • git2
  • go4
  • hexo1
  • json1
  • libcurl1
  • mutex1
  • operator1
  • override1
  • priority_queue1
  • shared_ptr1
  • stl1
  • unique_ptr1
  • vector1
  • 关键字2
  • 内存分配1
  • 列表对象1
  • 压缩列表2
  • 堆1
  • 字符串对象1
  • 指针和引用1
  • 敏捷开发1
  • 整数集合1
  • 栈1
  • 环境配置1
  • 缓存1
  • 设计模式1
  • 跳跃表1
  • 链表1
GitHub Google
Links
  • Hacker
本站已运行
© 2021 Tokey
博客全站共45.4k字
苏ICP备-20024031号
访问人数 总访问量 次
0%