2. From this folder isssue the command javah com.sample.android.SampleActivity.
Corresponding .h file should be generated in-place in this folder. You can move it to another folder.
#include "stdlib.h"
#include "stdio.h"
#include "unistd.h"
#include "string"
#include "pthread.h"
class Global {
public:
~Global()
{
std::cout << "Global object dtor\n";
}
};
class A {
public:
~A()
{
std::cout << "A dtor\n";
}
};
void thread_main()
{
int i = 0;
A a;
while (true) {
std::cout << "thread_main" << i++ << std::endl;
sleep(1);
if (i == 5) break;
}
}
void atexit_handler()
{
std::cout << "atexit handler\n";
}
Global global_variable;
int main(int argc, char **argv)
{
const int result = std::atexit(atexit_handler);
std::thread t(thread_main);
t.detach();
return 0;
}
This snippet would produce the following output, note that the destructor for the object allocated on detached thread is not getting called.
atexit handler thread_main0 Global object dtor *** Exited normally ***So the thread is abandoned non-gracefully, in a manner that reminds of the daemon thread in Java. Really, while JVM would not finish application if there is at least one active non-daemon thread, but if the thread is daemon thread it's existence wouldn't prevent app from exiting, and such thread would be abandoned - no stack unwinding, no finally blocks getting called for it. Such threads are used for tasks like garbage collection.
int main(int argc, char **argv)
{
const int result = std::atexit(atexit_handler);
std::thread t(thread_main);
t.detach();
pthread_exit(NULL);
//return 0;
}
This will let the detached thread complete and then the process exits releasing all the resources.
thread_main0 thread_main1 thread_main2 thread_main3 thread_main4 A dtor atexit handler Global object dtor *** Exited normally ***
void foo () {
std::cout << "foo threadid = " << std::this_thread::get_id() << '\n';
}
int main()
{
std::cout << "my threadid = " << std::this_thread::get_id() << '\n';
std::future fut = std::async(std::launch::deferred, foo);
fut.get();
return 0;
}
my threadid = 140737353906048 foo threadid = 140737353906048