https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109074
--- Comment #7 from Murugesan Nagarajan <murugesandins at gmail dot com> --- (In reply to Andrew Pinski from comment #3) > The reason for the abort is because you didn't catch the exception as > libstc++ is throwing one because threads are not enabled at runtime via the > linking of libpthread. Thank you again. Handled using try catch exception. However this exception not from my compilation issue. I feel that issue from libstdc++ library(older version) Updated code using try catch exception: /* * Creating thread using "class thread" at C++ * Sample C++ program using the "class thread". * Once an object is created, it used to create a thread. * When creating object, we need to pass the name of the function as parameter to that object. * Once creating the object, that function used to be called by the thread. * I will write mutex comments later. * Example: * thread t1(ThreadMethod); * Here t1 is creating the new thread. * I have not tested following code at other operating systems(UNIX/AIX/SunOS/HP-UX/...) excluding Linux, CYGWIN_NT and MINGW */ #include <unistd.h> #if defined(LINUX) #include <sys/syscall.h> // syscall(SYS_gettid) #ifdef SYS_gettid #define gettid() ((pid_t)syscall(SYS_gettid)) #endif #elif defined(CYGWIN_NT) || defined (MINGW) #define gettid() (unsigned long)pthread_self() #endif #include <iostream> #include <thread> #include <unistd.h> using namespace std; int TotalThreadCount = 0; pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; void ThreadMethod() { while(true) { if( 2 <= TotalThreadCount ) { break; } pthread_mutex_lock(&mutex); string DispTID = ""; DispTID = string("TotalThreadCount :") + to_string(TotalThreadCount) + " TID: " + to_string(gettid()) + "\n"; cout << DispTID; cout.flush(); sleep(1); ++TotalThreadCount; pthread_mutex_unlock(&mutex); } } int main() { // class thread available from C++11 // initialize the object using related thread function. try { thread t1(ThreadMethod); thread t2(ThreadMethod); t1.join(); t2.join(); } catch( std::exception &e) { cout << "Unable to create thread using class thread\n"; cout << "Caught thread exception: " << e.what() << "\n"; cout << "Re-compile your program using -lpthread\n"; } cout << "mutex.__data: " << (unsigned long long)&mutex.__data << "\n"; cout << "mutex.__data.__lock: " << (unsigned long long)&mutex.__data.__lock << "\n"; cout << "mutex.__data.__count: " << (unsigned long long)&mutex.__data.__count << "\n"; cout << "mutex.__data.__owner: " << (unsigned long long)&mutex.__data.__owner << "\n"; cout << "mutex.__data.__nusers: " << (unsigned long long)&mutex.__data.__nusers << "\n"; cout << "mutex.__data.__kind: " << (unsigned long long)&mutex.__data.__kind << "\n"; cout << "mutex.__data.__spins: " << (unsigned long long)&mutex.__data.__spins << "\n"; cout << "mutex.__data.__list: " << (unsigned long long)&mutex.__data.__list << "\n"; cout << "mutex.__data.__list.__prev: " << (unsigned long long)&mutex.__data.__list.__prev << "\n"; cout << "mutex.__data.__list.__next: " << (unsigned long long)&mutex.__data.__list.__next << "\n"; cout << "mutex.__size: " << (unsigned long long)&mutex.__size << "\n"; cout << "mutex.__align: " << (unsigned long long)&mutex.__align << "\n"; return 0; } /* pthread_mutex_t is a union having one struct one char array[40] and one long. union pthread_mutex_t { pthread_mutex_t::__pthread_mutex_s == double linked list { int __lock; == 04 bytes unsigned int __count; == 04 bytes int __owner; == 04 bytes unsigned int __nusers; == 04 bytes int __kind; == 04 bytes int __spins; == 04 bytes __pthread_list_t __list; == 16 bytes { *prev; *next; } } __data; == 40 bytes char __size[40]; == 40 bytes long __align; == 08 bytes } == 40 bytes since this is union taking maximum bytes. Hence size of this union is 40 bytes. */ /* $ /usr/bin/g++ -DLINUX -g -Wall thread.cpp -o ./a.out -std=c++11 $ # Tested this code on padding no wastage of memory using mutex. Unable to create thread using class thread Caught thread exception: Enable multithreading to use std::thread: Operation not permitted Re-compile your program using -lpthread mutex.__data: 6316960 => union mutex.__data.__lock: 6316960 => 6316960, 6316961 6316962 6316963 => union mutex.__data.__count: 6316964 => 6316964, 6316965 6316966 6316967 mutex.__data.__owner: 6316968 => 6316968, 6316969 6316970 6316971 mutex.__data.__nusers: 6316972 => 6316972, 6316973 6316974 6316975 mutex.__data.__kind: 6316976 => 6316976, 6316977 6316978 6316979 mutex.__data.__spins: 6316980 => 6316980, 6316981 6316982 6316983 mutex.__data.__list: 6316984 mutex.__data.__list.__prev:6316984 => 6316984,6316985 6316986 6316987 6316988 6316989 6316990 6316991 mutex.__data.__list.__next:6316992 => 6316992,6316993 6316994 6316995 6316996 6316997 6316998 6316999 mutex.__size: 6316960 => union mutex.__align: 6316960 => union */