iostreams thread safety and linux

iostreams thread safety and linux

Post by c.. » Fri, 14 Jan 2000 04:00:00



greetings,

g++ --version ... egcs-2.90.29 980515 (egcs-1.0.3 release)

redhat 5.2 kernel version 2.0.36

i have the following code (test.cpp), compiled with ...

    g++ -g -static -D_MIT_POSIX_THREADS -Dlinux test.cpp   -lpthread

if you run it a few times it'll cause a seg fault.
after looking thru /usr/include/libio.h i spotted the #define _IO_MTSAFE_IO
however defining this at the top of the code (as i've done below) makes
no difference.

anyone got any clues?
thanks,
c

ps let me know if you need more info.

#define _IO_MTSAFE_IO
#include <unistd.h>
#include <pthread.h>
#include <sched.h>
#include <assert.h>
#include <iostream.h>

void*
run(void *i)
{
    cerr << "ourthread::run" << endl;

    for (int i = 0; i < 100; ++i)
    {
        cerr << " thread i=" << i << endl << flush;
        int m_last_error = sched_yield();
        assert(m_last_error == 0 && "failed to yield");
    }

    return 0;

Quote:}

int
main()
{
#   ifdef _IO_MTSAFE_IO
    cerr << "thread safe io in place !!??" << endl;
#   else
    cerr << "uhoh !!!???" << endl;
#   endif

    cerr << "start" << endl;

    pthread_t        m_thread;
    int m_last_error = pthread_create(&m_thread,
                                        0,
                                        run, (void *)0);

    assert(m_last_error == 0 && "failed for some reason");

    for (int i = 0; i < 100; ++i)
    {
        cerr << "main i=" << i << endl << flush;
        m_last_error = sched_yield();
        assert(m_last_error == 0 && "failed to yield");
    }

    void **ret;
    m_last_error = pthread_join(m_thread, ret);
    assert(m_last_error == 0 && "something really BAD happened");

    cerr << "done" << endl;

    return 0;

Quote:}

 
 
 

iostreams thread safety and linux

Post by (nul » Tue, 18 Jan 2000 04:00:00


greetings,

thanks to all for the followups, however having made all the changes
suggested (assert now doesnt have a string and i've removed the
bogus "i" and "ret" variables) the code still causes a segmentation fault.

i still think it has something to do with thread safe io (try commenting
out all cerr's except the "start" and "done" and all is well) but i dont
know how to switch it on or even determine for sure if it is on.

i've included the code below ...
thanks in advance for any help you can provide,
cheers,
c

using ...

g++ --version ... egcs-2.90.29 980515 (egcs-1.0.3 release)
redhat 5.2 kernel version 2.0.36

compile with ...

g++ -g -static -D_MIT_POSIX_THREADS -Dlinux test.cpp   -lpthread

void*
run(void *)
{
    cerr << "ourthread::run" << endl;

    for (int i = 0; i < 100; ++i)
    {
        cerr << " thread i=" << i << endl << flush;
        int m_last_error = sched_yield();
        assert(m_last_error == 0);
    }

    return 0;

Quote:}

int
main()
{
#   ifdef _IO_MTSAFE_IO
    cerr << "thread safe io in place !!??" << endl;
#   else
    cerr << "uhoh !!!???" << endl;
#   endif

    cerr << "start" << endl;

    pthread_t        m_thread;
    int m_last_error = pthread_create(&m_thread,
                                        0,
                                        run, (void *)0);

    assert(m_last_error == 0);

    for (int i = 0; i < 100; ++i)
    {
        cerr << "main i=" << i << endl << flush;
        m_last_error = sched_yield();
        assert(m_last_error == 0);
    }

    m_last_error = pthread_join(m_thread, 0);
    assert(m_last_error == 0);

    cerr << "done" << endl;

    return 0;

Quote:}


 
 
 

iostreams thread safety and linux

Post by Wolfram Gloge » Tue, 18 Jan 2000 04:00:00



> thanks to all for the followups,

I haven't seen any followups - my news server seems a bit unreliable.

Quote:> using ...

> g++ --version ... egcs-2.90.29 980515 (egcs-1.0.3 release)
> redhat 5.2 kernel version 2.0.36

Those are truly ancient versions, sorry.  RedHat 5.2 was libc5-based,
which didn't provide very good thread support, however..

Quote:> compile with ...

> g++ -g -static -D_MIT_POSIX_THREADS -Dlinux test.cpp   -lpthread

                   ^^^^^^^^^^^^^^^^^^

..the user-level threading system in libc5 as selected by this macro
was especially unreliable.  I never got it to work with anything
non-trivial, if I remember correctly (that was in early 1996!).

I would suggest that you upgrade to a newer Linux distribution with
libc6, or at least try LinuxThreads (which used to compile with libc5)
instead of MIT pthreads.

If your problem persists with libc6, I am sure it would be looked at
and probably fixed quickly.

Regards,
Wolfram.

 
 
 

iostreams thread safety and linux

Post by David Schwart » Wed, 19 Jan 2000 04:00:00



> + > g++ -g -static -D_MIT_POSIX_THREADS -Dlinux test.cpp   -lpthread

        You want '-pthread', not '-lpthread'. Otherwise, you are not guaranteed
that the necessary defines are set.

        DS

 
 
 

iostreams thread safety and linux

Post by c.. » Thu, 20 Jan 2000 04:00:00



+
+ > using ...
+ >
+ > g++ --version ... egcs-2.90.29 980515 (egcs-1.0.3 release)
+ > redhat 5.2 kernel version 2.0.36
+
+ Those are truly ancient versions, sorry.  RedHat 5.2 was libc5-based,
+ which didn't provide very good thread support, however..
+
+ > compile with ...
+ >
+ > g++ -g -static -D_MIT_POSIX_THREADS -Dlinux test.cpp   -lpthread
+                    ^^^^^^^^^^^^^^^^^^
+
+ ..the user-level threading system in libc5 as selected by this macro
+ was especially unreliable.  I never got it to work with anything
+ non-trivial, if I remember correctly (that was in early 1996!).

my apologies, we are in fact using libc6.
i got rid of the -D_MIT_POSIX_THREADS and it still seg faults.
i also tried running the same binary on a red hat 6 machine which
ldd reported as using the same shared libs as on the redhat 5.2
and it didnt crash which suggests(?) a kernel problem.

i also ran it on a single processor redhat 5.2 and it didnt crash,
which is a Good Thing(tm) as thats what we're shipping:)

+ I would suggest that you upgrade to a newer Linux distribution with
+ libc6, or at least try LinuxThreads (which used to compile with libc5)
+ instead of MIT pthreads.

yes we're going to upgrade to redhat6 as soon as possible.

+ If your problem persists with libc6, I am sure it would be looked at
+ and probably fixed quickly.

see above, but i think it may just be a combination of the kernel and
the dual processor.  still wouldnt mind a fix tho if anyones got one.

+
+ Regards,
+ Wolfram.

thanks for your help Wolfram,
cheers,
c

 
 
 

iostreams thread safety and linux

Post by c.. » Fri, 21 Jan 2000 04:00:00



+
+
+ > + > g++ -g -static -D_MIT_POSIX_THREADS -Dlinux test.cpp   -lpthread
+
+       You want '-pthread', not '-lpthread'. Otherwise, you are not guaranteed
+ that the necessary defines are set.

woohoo!
that fixed it:)
i'm off now to try and work out _why_ it fixed it.
many thanks,
cheers,
c

 
 
 

1. glibc2: reentrancy, thread-safety, signal-safety

I have heard that glibc2 is close to thread-safe. Does anyone know how
thread thread safety is implemented. Does thread-safety mean signal-safety
that is: I can use --say-- malloc() both in the main process-flow code and
in signal handlers. I suppose it would fairly simply to enforce
thread-safety by using library owned mutexes or critical sections. But this
would preclude signal safety because of potential deadlocks.

By the same token, does anyone know how reentrancy relates to thread-safety
and signal-safety.

Thanks.

Peter

2. XFree86-3.2 on DualScan?!

3. LinuxThreads and thread-safety, re-entrancy, async-safety!

4. A visual unix shell?

5. Linux libraries and thread-safety!!!

6. stdlib.h goof's up, HELP!!!!

7. Thread safety confusion Pl help

8. can I change the size of shared memory

9. STL thread safety ?

10. Sockets and Thread Safety

11. VisualAge C++, exception handling, and thread safety

12. A question on thread-safety

13. Thread-safety of X11-app