Skip to content

Instantly share code, notes, and snippets.

@yoh2
Last active January 22, 2016 19:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yoh2/974d0a3753250d7c1978 to your computer and use it in GitHub Desktop.
Save yoh2/974d0a3753250d7c1978 to your computer and use it in GitHub Desktop.
-pthread を忘れると std::thread で例外が発生する仕組み ref: http://qiita.com/yoh2/items/97821d3d1dbe3e024d4f
$ g++ -Wall -std=c++11 sample.cpp -o sample1 # ← sample1 は -pthread 付けない
$ g++ -Wall -std=c++11 -pthread sample.cpp -o sample2 # ← sample2 は -pthread 付ける
$ ./sample1
terminate called after throwing an instance of 'std::system_error'
what(): Enable multithreading to use std::thread: Operation not permitted
中止
$ ./sample2
Hello!
$ g++ -Wall -std=c++11 -c sample.cpp
$ g++ sample.o -o sample3 # ← sample3 は -lpthread 付けない
$ g++ sample.o -lpthread -o sample4 # ← sample4 は -lpthread 付ける
$ ./sample3
terminate called after throwing an instance of 'std::system_error'
what(): Enable multithreading to use std::thread: Operation not permitted
中止
$ ./sample4
Hello!
$ env LD_PRELOAD=/lib/libpthread.so.0 ./sample1
Hello!
$ env LD_PRELOAD=/lib/libpthread.so.0 ./sample3
Hello!
$ lddtree sample1
sample1 (interpreter => /lib64/ld-linux-x86-64.so.2)
libstdc++.so.6 => /usr/lib/gcc/x86_64-pc-linux-gnu/4.9.3/libstdc++.so.6
libm.so.6 => /lib64/libm.so.6
libgcc_s.so.1 => /usr/lib/gcc/x86_64-pc-linux-gnu/4.9.3/libgcc_s.so.1
libc.so.6 => /lib64/libc.so.6
$ readelf -s -W /usr/lib/gcc/x86_64-pc-linux-gnu/4.9.3/libstdc++.so.6 | c++filt
シンボルテーブル '.dynsym' は 3862 個のエントリから構成されています:
番号: 値 サイズ タイプ Bind Vis 索引名
0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND
1: 0000000000059060 0 SECTION LOCAL DEFAULT 9
2: 00000000002f0100 0 SECTION LOCAL DEFAULT 18
3: 0000000000000000 0 FUNC GLOBAL DEFAULT UND __strtof_l@GLIBC_2.2.5 (33)
4: 0000000000000000 0 FUNC GLOBAL DEFAULT UND fileno@GLIBC_2.2.5 (33)
5: 0000000000000000 0 FUNC WEAK DEFAULT UND pthread_cond_destroy@GLIBC_2.3.2 (34)
6: 0000000000000000 0 FUNC GLOBAL DEFAULT UND __strcoll_l@GLIBC_2.2.5 (33)
(中略)
24: 0000000000000000 0 FUNC WEAK DEFAULT UND pthread_cond_signal@GLIBC_2.3.2 (34)
(中略)
33: 0000000000000000 0 NOTYPE WEAK DEFAULT UND pthread_key_create
(後略)
#include <iostream>
#include <thread>
void func()
{
std::cout << "Hello!" << std::endl;
}
int main()
{
std::thread(func).join();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment