我有一个程序,用于thread_local std::shared_ptr管理一些主要在本地线程访问的对象。但是,当线程加入并且线程局部shared_ptr正在析构时,如果程序是由MinGW(Windows 10)编译的,那么在调试时总是会出现SIGSEGV。以下是重现该错误的最少代码:
// main.cpp
#include <memory>
#include <thread>
void f() {
thread_local std::shared_ptr<int> ptr = std::make_shared<int>(0);
}
int main() {
std::thread th(f);
th.join();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如何编译:
g++ main.cpp -o build\main.exe -std=c++17
Run Code Online (Sandbox Code Playgroud)
编译器版本:
>g++ --version
g++ (x86_64-posix-seh-rev2, Built by MinGW-W64 project) 12.2.0
Copyright (C) 2022 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Run Code Online (Sandbox Code Playgroud)
使用 gdb …
我正在使用以下程序:
\n在main函数中,我想打印poll_timer函数的地址。
\n该程序使用 clang 可以成功编译并运行,但使用 GCC 则不能。
\n我在使用 GCC 时收到以下错误
\n"709568706/source.cpp: In function \xe2\x80\x98int main()\xe2\x80\x99:\n709568706/source.cpp:28:32: error: invalid use of member function \xe2\x80\x98static void MessagePoller::poll_timer()\xe2\x80\x99 (did you forget the \xe2\x80\x98()\xe2\x80\x99 ?)\n std::cout << (void*)m_sut->poll_timer << std::endl;\n ~~~~~~~^~~~~~~~~~"\nRun Code Online (Sandbox Code Playgroud)\n#include <iostream>\n#include <memory>\n\nclass MessagePoller\n{\n protected:\n static void poll_timer()\n {\n std::cout << "Poll timer Base called\\n";\n }\n};\n\nclass TestMessagePoller : public MessagePoller\n{\npublic:\n using MessagePoller::poll_timer;\n\n};\ntypedef std::shared_ptr<TestMessagePoller> TestMessagePollerPtr;\n\nint main()\n{ \n TestMessagePollerPtr m_sut;\n m_sut = TestMessagePollerPtr(new TestMessagePoller());\n\n std::cout << "HERE1\\n";\n m_sut->poll_timer();\n std::cout << (void*)m_sut->poll_timer …Run Code Online (Sandbox Code Playgroud) 我是 Django 框架的初学者。我创建了我的项目,创建了我的应用程序并测试它,它工作正常,直到我决定添加模板。我不知道错误来自哪里,因为我遵循 Django 文档的说明,在应用程序文件夹中创建文件夹名称模板,使用应用程序名称创建一个文件夹,最后在该文件夹中创建 HTML 文件。
注意:除了模板之外,其他路由都工作正常
请查看下面我的文件结构和错误的屏幕截图。

错误
TemplateDoesNotExist at /blog/
Blog/index
Request Method: GET
Request URL: http://127.0.0.1:8000/blog/
Django Version: 4.0.1
Exception Type: TemplateDoesNotExist
Exception Value:
Blog/index
Exception Location: C:\Python39\lib\site-packages\django\template\loader.py, line 19, in get_template
Python Executable: C:\Python39\python.exe
Python Version: 3.9.4
Python Path:
['C:\\Users\\Maxwell\\Desktop\\Django\\WebApp',
'C:\\Python39\\python39.zip',
'C:\\Python39\\DLLs',
'C:\\Python39\\lib',
'C:\\Python39',
'C:\\Users\\Maxwell\\AppData\\Roaming\\Python\\Python39\\site-packages',
'C:\\Python39\\lib\\site-packages',
'C:\\Python39\\lib\\site-packages\\win32',
'C:\\Python39\\lib\\site-packages\\win32\\lib',
'C:\\Python39\\lib\\site-packages\\Pythonwin']
Server time: Sun, 23 Jan 2022 12:04:18 +0000
Template-loader postmortem
Django tried loading these templates, in this order:
Using engine django:
django.template.loaders.app_directories.Loader: C:\Users\Maxwell\Desktop\Django\WebApp\Blog\templates\ Blog\index (Source does …Run Code Online (Sandbox Code Playgroud) 无论如何,我是否可以判断函数参数在 C++ 11 中是否是编译时间常数?我想根据结果对函数体进行分支。例如,如下所示;
template <T>
size_t get_length(const T &t)
{
return t.size();
}
template <typename T, size_t N>
size_t get_length(const std::array<T, N> &t)
{
return N;
}
// Maybe some template
??? foo(size_t length)
{
...
// If length could be evaluated at compile time,
return std::array<int, length>{};
...
// Otherwise,
return std::vector<int>(length);
}
// ??? and auto should be std::array if bar is std::array, std::vector if bar is std::vector or etc.
auto x = foo(get_length(bar));
Run Code Online (Sandbox Code Playgroud)
我相信这需要两项任务;首先,它应该能够确定一个值是否在编译时可评估。其次,如果可能的话,将值转换为编译时间常数。
无论如何有人可以做到这一点吗?或者有什么理由这是不可能的吗?似乎有人说使用模板函数,但这迫使调用者输入编译时间常数,而不是我想要的。
我有一个返回std::future. 我已经在实现中添加了一个缓存,如果不需要重新计算,我想选择立即返回一个值。
我怎样才能创造一个已经解决的未来?
// Class declarations shortened to minimal example to communicate intent
class MyClass {
Cache m_cache;
std::future<int> foo(int arg);
}
class Cache {
std::optional<int> get(int arg);
}
std::future<int> MyClass::foo(int arg) {
if (auto res = m_cache.get(arg)) {
// *res is my result
return std::future(*res); // ????? Doesn't work
}
// If the cache misses, we need to calculate it
// Fire up the ol' thread pool and get to work
return std::launch(std::launch::async /* ommited for brevity …Run Code Online (Sandbox Code Playgroud) 每一两年我都会决定尝试使用 C++ 模块,但每次我都会遇到问题,通常是与编译器有关。但这一次我的问题似乎出在规范本身,而且我似乎找不到解决方法。
考虑一个带有 header 的第三方库foo.hpp。中foo.hpp,它包括<string>. 假设由于某种原因(可能是因为奇怪的宏使用),foo.hpp无法将其编译为标头单元,因此必须将其编译为#included。<string>我的问题来自于尝试同时使用这个库。
想法#1:
module;
#include <foo.hpp>
export module A;
import <string>;
...
Run Code Online (Sandbox Code Playgroud)
据我了解,这里的问题是<string>包含在 中,但是在导入 时不会应用foo.hpp包含的标头防护,从而导致 odr 违规。<string><string>
想法#2:
export module A;
import <string>;
#include <foo.hpp>
...
Run Code Online (Sandbox Code Playgroud)
因为标头单元确实定义了宏,所以这修复了 odr 违规,当foo.hpp包含时标头防护会启动<string>。然而,这最终将 的所有内容定义foo.hpp为 module 中的实体A,这是不正确的并会导致各种问题。
想法#3:
module;
#include <foo.hpp>
export module A;
...
Run Code Online (Sandbox Code Playgroud)
这背后的想法是,如果foo.hpp已经包含<string>,为什么还要尝试再次获取它呢?问题是这在更复杂的情况下不起作用。是说我不是导入<string>,而是导入了另一个导出导入的模块<string>?或者也许是更复杂的事情?最终这个解决方案无法扩展。 …
假设countMe是一个全局变量,并且我同时向这个 while 循环启动 10 个线程,变量countMe互斥体是否在谓词中受保护?我认为因为当代码到达 wait_for 时它会解锁并释放锁,因此变量 countMe 不受互斥保护。我对吗?
while (true)
{
std::unique_lock<std::mutex> lock(mtx_kill);
cv_kill.wait_for(lock, 500ms, [&]() {++countMe; return killFlag; });
if (killFlag)
{
break;
}
}
Run Code Online (Sandbox Code Playgroud) 我的远程服务器上有太多与 vscode-server 相关的进程。我在这台服务器上使用远程 SSH 扩展大约半年了。似乎每次我启动连接时,即使我关闭连接,该进程也会被创建并保持活动状态。
32101 ? Sl 0:00 /home/codeally/.vscode-server/bin/b3e4e68a0bc097f0ae7907b217c1119af9e03435/node /tmp/vscode-remote-containers-server-34c19751-a5ad-42ba-9941-b3ed5ffaca29.js
32216 ? Sl 0:00 /home/codeally/.vscode-server/bin/6a995c4f4cc2ced6e3237749973982e751cb0bf9/node /tmp/vscode-remote-containers-server-1fcdaf18-27e1-417c-857e-3244a36d6fcf.js
32287 ? Sl 0:00 /home/codeally/.vscode-server/bin/b3e4e68a0bc097f0ae7907b217c1119af9e03435/node /tmp/vscode-remote-containers-server-43dcbb33-671b-4310-9e9e-01d9418efe4d.js
32347 ? Sl 0:00 /home/codeally/.vscode-server/bin/b3e4e68a0bc097f0ae7907b217c1119af9e03435/node /tmp/vscode-remote-containers-server-43dcbb33-671b-4310-9e9e-01d9418efe4d.js
32427 ? Sl 0:00 /home/codeally/.vscode-server/bin/b3e4e68a0bc097f0ae7907b217c1119af9e03435/node /tmp/vscode-remote-containers-server-43dcbb33-671b-4310-9e9e-01d9418efe4d.js
32462 ? Sl 0:00 /home/codeally/.vscode-server/bin/b3e4e68a0bc097f0ae7907b217c1119af9e03435/node /tmp/vscode-remote-containers-server-43dcbb33-671b-4310-9e9e-01d9418efe4d.js
32600 pts/56 Ss+ 0:00 /bin/bash --init-file /root/.vscode-server/bin/92da9481c0904c6adfe372c12da3b7748d74bdcb/out/vs/workbench/contrib/terminal/browser/media/shellIntegration-bash.sh
32687 ? Sl 0:00 /home/codeally/.vscode-server/bin/6a995c4f4cc2ced6e3237749973982e751cb0bf9/node /tmp/vscode-remote-containers-server-1fcdaf18-27e1-417c-857e-3244a36d6fcf.js
32712 ? Sl 0:00 /home/codeally/.vscode-server/bin/b3e4e68a0bc097f0ae7907b217c1119af9e03435/node /tmp/vscode-remote-containers-server-43dcbb33-671b-4310-9e9e-01d9418efe4d.js
root@cr-0:~# ps ax | grep -i code | wc -l
371
Run Code Online (Sandbox Code Playgroud)
经过一些连接尝试后,进程数量仍在增加。但我发现有时连接退出时它可能会杀死进程。
root@cr-0:~# ps ax | grep -i code | …Run Code Online (Sandbox Code Playgroud) 我想做以下事情:
// function that depends on key to do stuff
template <int key>
void bar() {...}
template <int ...Keys>
void foo(int key) {
// WHAT SHOULD BE HERE?
}
std::cin >> key;
foo<1,3,5,7,9>(key);
Run Code Online (Sandbox Code Playgroud)
使得它变成
template <int ...Key>
void foo(int key) {
switch (key) {
case 1: bar<1>();break;
case 3: bar<3>();break;
case 5: bar<5>();break;
case 7: bar<7>();break;
case 9: bar<9>();break;
default: break;
}
}
Run Code Online (Sandbox Code Playgroud)
如何生成一个 switch 语句,将所有可变参数模板参数枚举为高效的 switch 语句,而无需手动编写 switch 语句?
lambda 通过std::function引用使用,但 lambda 中的值无法更改。我不知道为什么?我已经使用了参考资料,但仍然无法实现这一目标。
#include <iostream>\n#include <functional>\n\nvoid myInvoke(const std::function<void()>& fn)\n{\n fn();\n}\n\nint main()\n{\n int i{ 0 };\n\n // Increments and prints its local copy of @i.\n auto count{ [i]() mutable {\n std::cout << ++i << '\\n';\n } };\n\n myInvoke(count);\n myInvoke(count);\n myInvoke(count);\n\n return 0;\n}\nRun Code Online (Sandbox Code Playgroud)\n实际结果\xef\xbc\x9a
\n1\n1\n1\nRun Code Online (Sandbox Code Playgroud)\n预期结果\xef\xbc\x9a
\n1\n2\n3\nRun Code Online (Sandbox Code Playgroud)\n我去掉&符号后,运行结果并没有改变。我想这两者应该是有区别的,但是我不知道到底是什么!
\n