我正在阅读http://gcc.gnu.org/onlinedocs/libstdc++/manual/shared_ptr.html,我仍然不清楚一些线程安全问题:
编辑:
伪代码:
// Thread I
shared_ptr<A> a (new A (1));
// Thread II
shared_ptr<A> b (a);
// Thread III
shared_ptr<A> c (a);
// Thread IV
shared_ptr<A> d (a);
d.reset (new A (10));
Run Code Online (Sandbox Code Playgroud)
在线程IV中调用reset()将删除在第一个线程中创建的A类的先前实例并将其替换为新实例?此外,在IV线程中调用reset()之后,其他线程只会看到新创建的对象?
我已经转换了以下链表结构
struct node {
node* next;
int v;
};
Run Code Online (Sandbox Code Playgroud)
进入c ++ 11版本 - 没有使用指针.
struct node {
unique_ptr<node> next;
int v;
};
Run Code Online (Sandbox Code Playgroud)
添加,删除元素和遍历工作正常,但是当我插入大约1mil元素时,当调用头节点的析构函数时,我得到堆栈溢出.
我不确定我做错了什么.
{
node n;
... add 10mill elements
} <-- crash here
Run Code Online (Sandbox Code Playgroud) 为什么每次循环运行时 temp 的地址(在 main 的 while 循环中)都相同我试图插入一个链表然后显示然后输出中间元素但最初在显示它时运行了一个无限循环只显示第一个元素。在插入后打印地址和 llist.add_ele_to_beg(&temp); 它每次都打印相同的地址!为什么会这样?
#include<iostream>
#include <unistd.h>
using namespace std;
class LinkedList;
class Node
{
private:
Node* next;
int value;
friend class LinkedList;
public:
Node(int ele) // constructor - declared in private section
// to prevent other classes creating objects of this class,
// only this class can create the object
{
next = nullptr;
value = ele;
}
};
class LinkedList
{
private:
Node* head;
public:
LinkedList()
{
head = nullptr;
}
void add_ele_to_beg(Node …Run Code Online (Sandbox Code Playgroud)