Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
15 years ago

Pthread_create hangs program

Hi,

I'm trying to compile some C++ code that uses pthread on uClinux.

I'm using this toolchain:

http://www.nioswiki.com/index.php?title=OperatingSystems/UClinux/BinaryToolchain

I can compile C++ code just fine, but whenever I try to use pthreads, the program will hang at the pthread_create () call.

At this point I'm just trying to compile a very simple program. It will hange right at the create call and never exit.

In addition, I get a linker error when trying to use pthread_join.


# include <iostream># include <pthread.h># include <unistd.h>
void *ReadMessagesThread(void *ptr);
int main(int argc, char** argv)
{
   using namespace std;
   pthread_t thread;
   cout << "starting pthread..." << endl;
   int rc = pthread_create(&thread, NULL, ReadMessagesThread, NULL);
   cout << "return value is : " << rc << endl;
   //pthread_join(thread, NULL);
   return 0;
}
void *ReadMessagesThread(void *ptr)
{
   using namespace std;
   for (int i = 0; i < 5; i++)
   {
      cout << "In thread : " << i << endl;
   }
}
Here is the MakeFile I'm using:


PROJ_NAME = remoteModelServer
CXX=nios2-linux-uclibc-g++
LDLIBS:=-lpthread $(LDLIBS)
CFLAGS=-O2 -fno-gnu-keywords
LDFLAGS=-Wl,-elf2flt 
$(PROJ_NAME): *.cpp
    $(CXX) $(LDLIBS) $(CFLAGS) $(LDFLAGS) -o $(PROJ_NAME).exe *.cpp 
.PHONY: clean
clean:
    -rm -f *. *.elf *.gdb *.bin *.exe *.flt 
Any ideas what I could be doing wrong?

Thanks

3 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    So I did a nios2-elf-objdump on the gdb file created and there is this line:

    00000000 w *UND* 00000000 pthread_create

    Which seems to imply that my program isn't properly finding the pthread library.

    I tried to specify the path to libpthread.a by using the -L option in the MakeFile, but that did not work.

    Any other ideas what could be wrong?
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Hey Kag,

    I'm experiencing the same problem. Have you found a way to fix it?

    There seems to be with the linking process, I removed the -lpthread from the linker arguments and it doesn't complain about it.

    Is there a way to check this? Why does the GPP automatically links the phtread lib? And why does the GCC not.

    Would be great if you or someone else could help me.

    Thanks
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    I made some progress with the Problem, linking of the pthead-lib is not correctly done by G++. I managed to improve the situation describe by Hag by adding the path to the pthread.a to the linker command.

    It looks like this:

    
    nios2-linux-c++ -o ijpis -elf2flt="-s 65536" include/tinyxml.o include/tinyxmlparser.o include/tinyxmlerror.o include/tinystr.o opt/nios2/lib/libpthread.a
    

    After that, it is able to link the invocation of pthread_join() and the objectdumb shows:

    
    0001b170 g     F .text	0000010c pthread_create
    

    The situation is improved, but after starting the program it still hangs after pthread_create...

    A note at the end: I use the gcc version 3.4.6. I haven't tried to use another, yet.

    Best regards