#include <iostream>
#include <map>
#include <thread>
#define SIZE 1024
#define AMOUNT 100000
#define THREADS 4
class A
{
private:
char a[SIZE];
};
void test()
{
std::cout << "test start\n";
std::map<int, A*> container;
for(int i=0; i<AMOUNT; i++)
{
A* a = new A();
std::pair<int, A*>p = std::make_pair(i, a);
container.insert(p);
}
std::cout << "test release\n";
for(int i=0; i<AMOUNT; i++)
{
auto iter = container.find(i);
delete iter->second;
container.erase(iter);
}
std::cout << "test end\n";
}
int main()
{
std::thread ts[THREADS];
for(int i=0; i<THREADS; i++) …Run Code Online (Sandbox Code Playgroud) 针对 pthread 的静态链接在 Linux 上是一个困难的话题。它曾经工作包-lpthread的-Wl,--whole-archive -lpthread -Wl,--no-whole-archive(细节都可以在此找到答案)。
效果是符号(对于 pthread)是strong ,而不是 weak。从 Ubuntu 18.04 开始(在 gcc 5.4.0 和 gcc 7.4.0 之间),这种行为似乎发生了变化,pthread 符号现在总是以独立于--whole-archive选项的弱符号结束。
因此,-whole-archive配方停止工作。我的问题的目的是了解工具链(编译器、链接器、标准库)中最近发生了什么变化,以及可以采取哪些措施来恢复旧行为。
例子:
#include <mutex>
int main(int argc, char **argv) {
std::mutex mutex;
mutex.lock();
mutex.unlock();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在以下所有示例中,使用了相同的编译命令:
g++ -std=c++11 -Wall -static simple.cpp -Wl,--whole-archive -lpthread -Wl,--no-whole-archive
Run Code Online (Sandbox Code Playgroud)
之前,与编译时-static,并行线程符号(例如,pthread_mutex_lock)表现强劲(标记为T通过nm),但现在他们是弱(W):
Ubuntu 14.04: docker run --rm -it ubuntu:14.04 bash
$ apt-get …Run Code Online (Sandbox Code Playgroud)