public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "murugesandins at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug libstdc++/109074] SIGABRT signal without using -lpthread at Linux RHEL 7.3
Date: Fri, 10 Mar 2023 05:26:47 +0000 [thread overview]
Message-ID: <bug-109074-4-3JY0Iu6Usu@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-109074-4@http.gcc.gnu.org/bugzilla/>
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
*/
next prev parent reply other threads:[~2023-03-10 5:26 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-03-09 6:56 [Bug c++/109074] New: " murugesandins at gmail dot com
2023-03-09 10:20 ` [Bug c++/109074] " rguenth at gcc dot gnu.org
2023-03-09 11:28 ` pinskia at gcc dot gnu.org
2023-03-09 11:30 ` pinskia at gcc dot gnu.org
2023-03-10 4:34 ` [Bug libstdc++/109074] " murugesandins at gmail dot com
2023-03-10 4:53 ` murugesandins at gmail dot com
2023-03-10 5:01 ` pinskia at gcc dot gnu.org
2023-03-10 5:26 ` murugesandins at gmail dot com [this message]
2023-03-10 10:32 ` redi at gcc dot gnu.org
2023-03-10 21:30 ` murugesandins at gmail dot com
2023-03-10 23:20 ` murugesandins at gmail dot com
2023-03-10 23:30 ` pinskia at gcc dot gnu.org
2023-03-12 20:52 ` redi at gcc dot gnu.org
2023-03-13 3:30 ` murugesandins at gmail dot com
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=bug-109074-4-3JY0Iu6Usu@http.gcc.gnu.org/bugzilla/ \
--to=gcc-bugzilla@gcc.gnu.org \
--cc=gcc-bugs@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).