Operating System - HP-UX
1821641 Members
3281 Online
109633 Solutions
New Discussion юеВ

Pthread mutex crash before main

 
Stephen_22
New Member

Pthread mutex crash before main


Hi,
I have been experiencing a weird problem with one of my threaded applications, I have managed to reproduce in a small example where i have a stl::string and i link in pthreads. If i dont link pthread OR remove the stl::string then it works.

The following is the test code:

#include
#include
#include
using namespace std;

int main(int argc, char *argv[])
{
string hw("hello world");
printf( "%s\n", hw.c_str() );
return 0;
}


using compile line:
aCC -AA +DA2.0W -o pthreadTest pthreatest.cpp -lpthread


aCC --version :
aCC: HP ANSI C++ B3910B A.03.30

uname -a :
HP-UX borneo B.11.00 U 9000/785 2013089457 unlimited-user license

also whats weird when i do a ldd on the exe i get the following:
ldd pthreadTest
libpthread.1 => /usr/lib/pa20_64/libpthread.1
libstd_v2.2 => /usr/lib/pa20_64/libstd_v2.2
libCsup_v2.2 => /usr/lib/pa20_64/libCsup_v2.2
libm.2 => /usr/lib/pa20_64/libm.2
libcl.2 => /usr/lib/pa20_64/libcl.2
libc.2 => /usr/lib/pa20_64/libc.2
libdl.1 => /usr/lib/pa20_64/libdl.1
libdl.1 => /usr/lib/pa20_64/libdl.1
libdl.1 => /usr/lib/pa20_64/libdl.1
/opt/graphics/OpenGL/lib/pa20_64/libogltls.sl => /opt/graphics/OpenGL/lib/pa20_64/libogltls.sl


Why am i linking to openGL?

someone please help! :)
3 REPLIES 3
NeerajBhatia_1
Occasional Contributor

Re: Pthread mutex crash before main

I am running into the same issue. Did you find the fix?

Neeraj
Stephen_22
New Member

Re: Pthread mutex crash before main

I managed to get around this problem by using the following compile line:

aCC -AA +DA2.0W -o pthreadTest pthreatest.cpp -mt


basically i replaced -lpthreas with -mt.

I hope this helps someone.
Adam J Markiewicz
Trusted Contributor

Re: Pthread mutex crash before main

Hi

I think that the problem lies in the string implementation. It was made in the way that several identical strings share the same buffer instead of copying it exclusivelly for themseves. This leads to the concept of reference counters that are attached to every string buffer.

Unluckilly, because strings can be used from several threads, access to this reference counter has to be synchronized to avoid modifications from several threads at the same time. So together with this buffer we have also mutex.

But there is a lot of programs that are not multithread, so why to use mutex and multithread library always? Thats why it was decided that adding mutex to the string buffer will be dane only if it will be discovered that the program is compiled for multithread.

And comes your problem, I suppose. Some of the used code (libraries, objects) are compiled for not multithread and construct strings that use buffers without mutex attached. When it comes to the code that tries to work with mutex that is not allocated then you have a crash.

I've been using strings with pthread whith a success. I use -D__REENTRANT to enforce multithread strings everywhere. But if you use library that uses strings without mutexes it will be more difficult.

Good luck

Adam
I do everything perfectly, except from my mistakes