This is my code
#include <pthread.h>
#include <stdio.h>
void cleanup(void *arg) {
printf("cleanup: %s\n", (const char*)arg);
}
void *thr_fn1(void *arg) {
printf("thread 1 strat\n");
pthread_cleanup_push(cleanup, (void*)"thread 1 first handler");
pthread_cleanup_push(cleanup, (void*)"thread 1 first handler");
if(arg)
return (void*)1;
pthread_cleanup_pop(0);
pthread_cleanup_pop(0);
return (void*)1;
}
void *thr_fn2(void *arg) {
printf("thread 2 strat\n");
pthread_cleanup_push(cleanup, (void*)"thread 2 first handler");
pthread_cleanup_push(cleanup, (void*)"thread 2 first handler");
if(arg)
return (void*)2;
pthread_cleanup_pop(0);
pthread_cleanup_pop(0);
return (void*)2;
}
int main() {
int err;
pthread_t tid1, tid2;
void *tret;
pthread_create(&tid1, NULL, thr_fn1, (void*)1);
pthread_create(&tid2, NULL, thr_fn2, (void*)1);
pthread_join(tid1, &tret);
printf("pthread 1 exit code %ld\n", tret);
pthread_join(tid2, &tret);
printf("pthread 2 exit code %ld\n", tret);
return 0;
}
Now I run it using gcc and g++
$ gcc main.c -o main
$ ./main
thread 2 strat
thread 1 strat
pthread 1 exit code 1
pthread 2 exit code 2
$ g++ main.c -o main
$ ./main
thread 1 strat
cleanup: thread 1 first handler
cleanup: thread 1 first handler
thread 2 strat
cleanup: thread 2 first handler
cleanup: thread 2 first handler
pthread 1 exit code 1
pthread 2 exit code 2
$
Why they behave differently?
Any other functions behave like this?
I found the implementations of gcc and g++ are different. So which one is a better implementation?
On Linux, the pthread_cleanup_push() and pthread_cleanup_pop() functions are implemented as macros that expand to text containing { and }, respectively.
# define pthread_cleanup_push(routine, arg) \
do { \
__pthread_cleanup_class __clframe (routine, arg)
If compiled with g++, __pthread_cleanup_class is a C++ class:
#ifdef __cplusplus
/* Class to handle cancellation handler invocation. */
class __pthread_cleanup_class
{
void (*__cancel_routine) (void *);
void *__cancel_arg;
int __do_it;
int __cancel_type;
public:
__pthread_cleanup_class (void (*__fct) (void *), void *__arg)
: __cancel_routine (__fct), __cancel_arg (__arg), __do_it (1) { }
~__pthread_cleanup_class () { if (__do_it) __cancel_routine (__cancel_arg); }
void __setdoit (int __newval) { __do_it = __newval; }
void __defer () { pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED,
&__cancel_type); }
void __restore () const { pthread_setcanceltype (__cancel_type, 0); }
};
It behaves like any class, its destructor runs on scope end.
In C, using gcc, cleanup handlers require pthread_exit(), but your code does return.
When a thread terminates by calling pthread_exit(3), all clean-up handlers are executed as described in the preceding point. (Clean-up handlers are not called if the thread terminates by performing a return from the thread start function.)
Related
I want to debug an application where some threads seem to go into a deadlock situation in a production environment (I cannot debug, thus I need a meaningful log). I found boost::stacktrace::stacktrace(), but it dumps info for all threads.
Is it possible to limit the output to a specific thread?
If it is Linux specific you can use backtrace and backtrace_symbols to retrieve the backtrace for that specific thread.
I adapted this example from man backtrace to include a thread.
#include <execinfo.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <thread>
#define BT_BUF_SIZE 100
void printbt(void)
{
int j, nptrs;
void *buffer[BT_BUF_SIZE];
char **strings;
nptrs = backtrace(buffer, BT_BUF_SIZE);
printf("backtrace() returned %d addresses\n", nptrs);
/* The call backtrace_symbols_fd(buffer, nptrs, STDOUT_FILENO)
would produce similar output to the following: */
strings = backtrace_symbols(buffer, nptrs);
if (strings == NULL) {
perror("backtrace_symbols");
exit(EXIT_FAILURE);
}
for (j = 0; j < nptrs; j++)
printf("%s\n", strings[j]);
free(strings);
}
static void myfunc2(void)
{
printbt();
}
void myfunc(int ncalls)
{
if (ncalls > 1)
myfunc(ncalls - 1);
else
myfunc2();
}
int main(int argc, char *argv[])
{
if (argc != 2) {
fprintf(stderr, "%s num-calls\n", argv[0]);
return 0;
}
std::thread th( myfunc, atoi(argv[1]) );
th.join();
}
You have to compile with -rdynamic to work
$ g++ -g3 -rdynamic test2.cpp -o /tmp/test2 -lpthread
Running it provides
$ /tmp/test2 3
backtrace() returned 13 addresses
/tmp/test2(_Z7printbtv+0x32) [0x55e2dbe5d37b]
/tmp/test2(+0x4451) [0x55e2dbe5d451]
/tmp/test2(_Z6myfunci+0x29) [0x55e2dbe5d47d]
/tmp/test2(_Z6myfunci+0x22) [0x55e2dbe5d476]
/tmp/test2(_Z6myfunci+0x22) [0x55e2dbe5d476]
/tmp/test2(_ZSt13__invoke_implIvPFviEJiEET_St14__invoke_otherOT0_DpOT1_+0x36) [0x55e2dbe5e0af]
/tmp/test2(_ZSt8__invokeIPFviEJiEENSt15__invoke_resultIT_JDpT0_EE4typeEOS3_DpOS4_+0x4a) [0x55e2dbe5dffd]
/tmp/test2(_ZNSt6thread8_InvokerISt5tupleIJPFviEiEEE9_M_invokeIJLm0ELm1EEEEvSt12_Index_tupleIJXspT_EEE+0x47) [0x55e2dbe5df4d]
/tmp/test2(_ZNSt6thread8_InvokerISt5tupleIJPFviEiEEEclEv+0x2b) [0x55e2dbe5deef]
/tmp/test2(_ZNSt6thread11_State_implINS_8_InvokerISt5tupleIJPFviEiEEEEE6_M_runEv+0x20) [0x55e2dbe5dec0]
/lib/x86_64-linux-gnu/libstdc++.so.6(+0xd6de4) [0x7f306312ede4]
/lib/x86_64-linux-gnu/libpthread.so.0(+0x8609) [0x7f3063242609]
/lib/x86_64-linux-gnu/libc.so.6(clone+0x43) [0x7f3062f6a133]
You can use __cxa_demangle to automatically demangle the symbols or you can simply use c++filt from gcc for that.
$ echo _ZSt13__invoke_implIvPFviEJiEET_St14__invoke_otherOT0_DpOT1_+0x36 | c++filt
void std::__invoke_impl<void, void (*)(int), int>(std::__invoke_other, void (*&&)(int), int&&)+0x36
$ echo _Z6myfunci+0x29 | c++filt
myfunc(int)+0x29
You can then go into gdb and look what line the offset 0x29 corresponds to inside the function myfunc(int)
#include <unistd.h>
#include <stdio.h>
#include <cstring>
#include <thread>
void test_cpu() {
printf("thread: test_cpu start\n");
int total = 0;
while (1) {
++total;
}
}
void test_mem() {
printf("thread: test_mem start\n");
int step = 20;
int size = 10 * 1024 * 1024; // 10Mb
for (int i = 0; i < step; ++i) {
char* tmp = new char[size];
memset(tmp, i, size);
sleep(1);
}
printf("thread: test_mem done\n");
}
int main(int argc, char** argv) {
std::thread t1(test_cpu);
std::thread t2(test_mem);
t1.join();
t2.join();
return 0;
}
Compile it with g++ -o test test.cc --std=c++11 -lpthread
I run the program in Linux, and run top to monitor it.
I expect to see ONE process however I saw THREE.
It looks like std::thread is creating threads, why do I end up with getting processes?
Linux does not implement threads. It only has Light Weight Processes (LWP) while pthread library wraps them to provide POSIX-compatible thread interface. The main LWP creates its own address space while each subsequent thread LWP shares address space with main LWP.
Many utils, such as HTOP (which seems to be on the screenshot) by default list LWP. In order to hide thread LWPs you can open Setup (F2) -> Display Options and check Hide kernel threads and Hide userland process threads options. There is also an option to highlight threads - Display threads in different color.
During my work writing a C++ wrapper for MPI I ran into a segmentation fault in MPI_Test(), the reason of which I can't figure out.
The following code is a minimal crashing example, to be compiled and run with mpic++ -std=c++11 -g -o test test.cpp && ./test:
#include <stdlib.h>
#include <stdio.h>
#include <memory>
#include <mpi.h>
class Environment {
public:
static Environment &getInstance() {
static Environment instance;
return instance;
}
static bool initialized() {
int ini;
MPI_Initialized(&ini);
return ini != 0;
}
static bool finalized() {
int fin;
MPI_Finalized(&fin);
return fin != 0;
}
private:
Environment() {
if(!initialized()) {
MPI_Init(NULL, NULL);
_initialized = true;
}
}
~Environment() {
if(!_initialized)
return;
if(finalized())
return;
MPI_Finalize();
}
bool _initialized{false};
public:
Environment(Environment const &) = delete;
void operator=(Environment const &) = delete;
};
class Status {
private:
std::shared_ptr<MPI_Status> _mpi_status;
MPI_Datatype _mpi_type;
};
class Request {
private:
std::shared_ptr<MPI_Request> _request;
int _flag;
Status _status;
};
int main() {
auto &m = Environment::getInstance();
MPI_Request r;
MPI_Status s;
int a;
MPI_Test(&r, &a, &s);
Request r2;
printf("b\n");
}
Basically, the Environment class is a singleton wrapper around MPI_Init and MPI_Finalize. When the program exits, MPI will be finalized and the first time the class is instantiated, MPI_Init is called. Then I do some MPI stuff in the main() function, involving some other simple wrapper objects.
The code above crashes (on my machine, OpenMPI & Linux). However, it works when I
comment any of the private members of Request or Status (even int _flag;)
comment the last line, printf("b\n");
Replace auto &m = Environment::getInstance(); with MPI_Init().
There doesn't seem to be a connection between these points and I have no clue where to look for the segmentation fault.
The stack trace is:
[pc13090:05978] *** Process received signal ***
[pc13090:05978] Signal: Segmentation fault (11)
[pc13090:05978] Signal code: Address not mapped (1)
[pc13090:05978] Failing at address: 0x61
[pc13090:05978] [ 0] /usr/lib/libpthread.so.0(+0x11dd0)[0x7fa9cf818dd0]
[pc13090:05978] [ 1] /usr/lib/openmpi/libmpi.so.40(ompi_request_default_test+0x16)[0x7fa9d0357326]
[pc13090:05978] [ 2] /usr/lib/openmpi/libmpi.so.40(MPI_Test+0x31)[0x7fa9d03970b1]
[pc13090:05978] [ 3] ./test(+0xb7ae)[0x55713d1aa7ae]
[pc13090:05978] [ 4] /usr/lib/libc.so.6(__libc_start_main+0xea)[0x7fa9cf470f4a]
[pc13090:05978] [ 5] ./test(+0xb5ea)[0x55713d1aa5ea]
[pc13090:05978] *** End of error message ***
-------------------------------------------------------
Primary job terminated normally, but 1 process returned
a non-zero exit code. Per user-direction, the job has been aborted.
-------------------------------------------------------
--------------------------------------------------------------------------
mpirun noticed that process rank 0 with PID 0 on node pc13090 exited on signal 11 (Segmentation fault).
--------------------------------------------------------------------------
Maybe this questions is easy but I can't figure it out how to resolve it:
I try to compile an example which has pthreas on windows 7 (64bit), with Code Blocks I downloaded the prebuild library and set up building_options: the path for the compiler pthreadLib\include and the linker to pthreadLib\lib\x64
The program is:
extern "C"
{
#include <pthread.h>
#include <unistd.h>
}
#include <iostream>
#include <windows.h>
using namespace std ;
void * function1(void * argument);
void * function2(void * argument);
int main( void )
{
pthread_t t1, t2 ; // declare 2 threads.
pthread_create( &t1, NULL, function1,NULL); // create a thread running function1
pthread_create( &t2, NULL, function2,NULL); // create a thread running function2
Sleep(1);
return 0;
}
void * function1(void * argument)
{
cout << " hello " << endl ;
Sleep(2); // fall alseep here for 2 seconds...
return 0;
}
void * function2(void * argument)
{
cout << " world " << endl ;
return 0;
}
In case I comment pthread_create(); function, than it builds. So pthread_t is recognized as a type.
When I try to compile with pthread_create I get an error:
mingw32-g++.exe -L..\libs\Pre-built.2\lib\x64 -LD:\DropBox\WorkUT\Programs\MyODP\libs -o bin\Release\RIP.exe obj\Release\main.o -s ..\libs\Pre-built.2\lib\x64\libpthreadGC2.a ..\libs\Pre-built.2\lib\x64\pthreadVC2.lib
obj\Release\main.o:main.cpp:(.text.startup+0x36): undefined reference to `_imp__pthread_create'
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 0 second(s))
1 error(s), 1 warning(s) (0 minute(s), 0 second(s))
Do I have to make additional setups in C::B? I tired to add the linker command -lpthread but is not recognized.
After 2 days I figured it out.
First: I installed the minGW 64 for Windows OS.
Next: I set up C::B to use the minGW_64 after this post.
Moreover: I added to the linker libs ..\libs\Pre-built.2\lib\x64\libpthreadGC2.a and ..\libs\Pre-built.2\lib\x64\pthreadVC2.lib. Finally, I added to my project the pthreadGC2.dll (64bit version!).
Lesson learned, don't mix lib. and compiler with 86 and 64.
For Linux
Use sleep(1) in stead of Sleep(1)
I just cant see what Im doing wrong here:
Q. Why is my library not being found by g++ when compiling the program with a shared object?
Im trying to include a shared library to my c++ program:
g++ -fpic -c sha.cpp
g++ -shared -o libsha.so sha.o
g++ main.cpp -o main -L. -lsha
where sha.cpp and sha.h are the library files and main.cpp is my program.
Ive tried the same with static library which works find:
g++ -static -c sha.cpp -o libsha.o
ar rcs libsha.a libsha.o
g++ main.cpp -o main -L. -lsha
The platform is cygwin on windows and here is the output:
rob#pc /cygdrive/c/src/a
$ g++ main.cpp -o shatest -L. -lsha
/usr/lib/gcc/i686-pc-cygwin/4.3.4/../../../../i686-pc-cygwin/bin/ld: cannot find -lsha
collect2: ld returned 1 exit status
Ive read all forum posts but the library is in the same folder!
$ ls
libsha.so main.cpp sha.cpp sha.h sha.o
The reason im doing this is that on another platform, a library is being created whereby when one object is called, it works, but the app crashes when the second object is being constructed. Im doing the above as a simple test! (annoyingly not so simple).
source files below:
main.cpp
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include "sha.h"
void *thread_one( void *ptr );
void *thread_two( void *ptr );
main()
{
pthread_t thread1, thread2;
int iret1, iret2;
/* Create independent threads each of which will execute function */
iret1 = pthread_create( &thread1, NULL, thread_one, 0);
iret2 = pthread_create( &thread2, NULL, thread_two, 0);
/* Wait till threads are complete before main continues. Unless we */
/* wait we run the risk of executing an exit which will terminate */
/* the process and all threads before the threads have completed. */
pthread_join( thread1, NULL);
pthread_join( thread2, NULL);
printf("Thread 1 returns: %d\n",iret1);
printf("Thread 2 returns: %d\n",iret2);
exit(0);
}
void *thread_one( void *ptr )
{
printf("Run thread_one\n");
CObj1 obj;
}
void *thread_two( void *ptr )
{
printf("Run thread_two\n");
CObj2 obj;
}
sha.cpp
#include <stdio.h>
#include <stdlib.h>
#include "sha.h"
CObj1::CObj1()
{
printf("CObj1\n");
a = 10;
printf("CObj1: %d \n", a);
}
CObj2::CObj2()
{
printf("CObj2\n");
a = 10;
printf("CObj2: %d \n", a);
}
sha.h
#ifndef LIB
#define LIB
class CObj1
{
public:
CObj1();
private:
int a;
};
class CObj2
{
public:
CObj2();
private:
int a;
};
#endif