以下是使用两个线程插入哈希表进行测试的简单程序.对于测试,不使用锁定.
#include <iostream>
#include <unordered_map>
#include <thread>
using namespace std;
void thread_add(unordered_map<int, int>& ht, int from, int to)
{
for(int i = from; i <= to; ++i)
ht.insert(unordered_map<int, int>::value_type(i, 0));
}
void test()
{
unordered_map<int, int> ht;
thread t[2];
t[0] = thread(thread_add, ht, 0, 9);
t[1] = thread(thread_add, ht, 10, 19);
t[0].join();
t[1].join();
std::cout << "size: " << ht.size() << std::endl;
}
int main()
{
test();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是,编译时会出错.
$ g++ -std=c++11 -pthread test.cpp
...
/usr/include/c++/4.8.2/functional:1697:61: error: no type …Run Code Online (Sandbox Code Playgroud) 当然下面的代码工作(它调用std :: cout :: operator <<):
cout << 1 << '1' << "1" << endl;
Run Code Online (Sandbox Code Playgroud)
碰巧发现还有std :: operator <<,它似乎只适用于char或char*参数:
operator<<(cout, '1'); // ok
operator<<(cout, "1"); // ok
operator<<(cout, 1); // error
Run Code Online (Sandbox Code Playgroud)
那么为什么我们需要这个运算符以及如何使用它?
谢谢.
我尝试使用重载的运算符==()在向量中找到一个元素.但是,如果type1在以下代码中使用,则输出为1和0(未找到).使用type2给出1和1.环境是Xubuntu 12.04和g ++版本4.6.3.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef pair<string, int> type1;
struct type2: public type1 {};
#define TYPE type1
bool operator== (const TYPE& lhs, const TYPE& rhs) {
return lhs.first == rhs.first;
}
int main()
{
vector<TYPE> vec;
TYPE v1, v2;
v1.first = "abc"; v1.second = 1; vec.push_back(v1);
v2.first = "abc"; v2.second = 2;
cout << (v1 == v2) << endl;
cout << (find(vec.begin(), vec.end(), v2) != vec.end()) << endl;
}
Run Code Online (Sandbox Code Playgroud) c++ operator-overloading overload-resolution name-lookup argument-dependent-lookup
在Bash中,我们可以使用Alt+ number+ .选择前一个命令的第 n 个参数,并使用Alt+ ,选择前一个命令。他们循环浏览历史。
例如:
$ ls 1 2
$ echo 10 20
Run Code Online (Sandbox Code Playgroud)
现在按住Alt,然后按0再点,它将显示“ echo”。在不松开的情况下Alt,.再次按,将显示“ ls”。在同一操作中使用1将显示10和1,以此类推。按Alt和逗号将显示历史记录中的整个命令行。还Alt和.展示历史命令的最后一个参数。
请注意,所有这些操作只是将参数(或整个命令行)插入当前光标。他们不会更改当前命令行中已经存在的内容。
我正在使用Zsh和最新的Oh-My-Zsh软件包,但似乎行为有所不同:
Zsh的Alt+号,表示命令的最后一个参数。
的Alt+ 0+ .是相同的击(示出了comman),但 Alt+ number+ .示出的最后n 个参数,即,Alt+ 1+ .在上面的情况下示出了20和2。
该Alt+ ,不显示在历史上的整个命令。
如何在Zsh中做同样的事情?谢谢。
不知道为什么以下示例中存在错误:
$ a=1; (( a > 0 )) && echo y || echo n
y
$ a=x; (( a > 0 )) && echo y || echo n
n
$ a=a; (( a > 0 )) && echo y || echo n
-bash: ((: a: expression recursion level exceeded (error token is "a")
n
Run Code Online (Sandbox Code Playgroud) 我碰到过这样的情况:“唯一需要进行TURN的时间是当一个对等方位于对称NAT之后而另一个对等方位于对称NAT或端口受限NAT之后。” 那么,对称NAT后面的对等方如何连接另一个后面的对等方,例如全锥状NAT?
例如,让对称NAT后面的对等方为A,而全圆锥NAT后面的对等方为B。呼叫过程应类似于:
请检查我是否正确理解这些步骤。那么,A(在对称NAT后面)如何与B(在整个圆锥或地址限制的圆锥后面)进行通信?
谢谢。
例如,在空目录中尝试以下命令:
$ for i in *; do echo $i; done
*
Run Code Online (Sandbox Code Playgroud)
有没有办法抑制打印输出*?
想要从函数中获取用户的输入.但是,提示(在这种情况下"请回答y或n.")也包含在返回值中.
#!/bin/bash
input() {
while true; do
read -p "input y/n: " yn
case $yn in
[Yy]* ) yn="y"; break;;
[Nn]* ) yn="n"; break;;
* ) echo "Please answer y or n.";;
esac
done
echo $yn
}
val=$(input)
echo "val is: $val"
Run Code Online (Sandbox Code Playgroud)
如果首先输入错误值,则结果如下:
input y/n: other
input y/n: y
val is: Please answer y or n.
y
Run Code Online (Sandbox Code Playgroud)
谢谢.
据说信号处理程序需要volatile,例如,
volatile int flag = 1; // volatile is needed here?
void run() {
while(flag) { /* do someting... */ }
}
void signal_handler(int sig) {
flag = 0;
}
int main() {
signal(SIGINT, sig_handler);
run();
// ...
}
Run Code Online (Sandbox Code Playgroud)
据说volatile通常不用于多线程.但是多线程中类似的情况怎么样:
int flag = 1; // is volatile needed here?
void thread_function() {
while(flag) { /* do someting... */ }
}
int main() {
// pthread_create() to create thread_function()...
sleep(10); // let thread_function run for 10 seconds
flag = 0;
// …Run Code Online (Sandbox Code Playgroud) 对于简单任务,自旋锁应该比互斥锁具有更好的性能。然而,在这个简单的测试(8 个线程递增计数器)中,结果显示不同:
#include <iostream>
#include <thread>
#include <mutex>
#include <atomic>
#include <vector>
using namespace std;
class SpinLock {
private:
atomic_flag lck = ATOMIC_FLAG_INIT;
public:
void lock() { while(lck.test_and_set(memory_order_acquire)) {} }
void unlock() { lck.clear(memory_order_release); }
};
int total = 0;
#ifdef SPINLOCK
SpinLock my_lock;
#else
mutex my_lock;
#endif
void foo(int n)
{
for(int i = 0; i < 10000000; ++i) {
#ifdef SPINLOCK
lock_guard<SpinLock> lck(my_lock);
#else
lock_guard<mutex> lck(my_lock);
#endif
++total;
}
}
int main()
{
vector<thread> v;
for(int i = …Run Code Online (Sandbox Code Playgroud) 使用链接中的示例,但更改为使用char *和vector:
#include <vector>
using namespace std;
class Test{
char *myArray;
public:
Test(){
myArray = new char[10];
}
~Test(){
delete[] myArray;
}
};
int main(){
vector<Test> q; // line 1
Test t; // line 2
q.push_back(t);
}
Run Code Online (Sandbox Code Playgroud)
它会导致双重释放或损坏错误.但是,如果在第1行之前运行第2行,例如:
Test t;
vector<Test> q;
Run Code Online (Sandbox Code Playgroud)
然后运行正常.这是为什么?
在Xubuntu 12.04 g ++ 4.6.3上测试.
更新:
这不是重复的问题.我理解需要一个复制构造函数和赋值运算符(它已在上面的示例代码所在的链接中得到了解答).但是,在原始链接中使用int *或queue类似,但交换第1行和第2行仍然存在错误.仅在使用char *,并且vector和交换1号线和2号线会不会导致错误.我的问题是为什么这个特例?任何人都可以在您的平台上查看它吗