Forum Discussion

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

Problems with pthreds

I'm a program benchmark with pthreads, but I'm having really problems with it. Now i'm only want to do a simple hello world, but when I execute it the uClinux reinit another time. Can someone help me please? Can put some example that really run? Thanks to all.

# include <stdio.h># include <unistd.h># include <stdlib.h># include <pthread.h># include <sched.h># include <errno.h>

void print_message_function( void ){

char message = "Hello";

printf("%s ", message);

pthread_exit(NULL);

}

void main (int argc, char *argv[]){

pthread_t thread1, thread2;

//char *message1 = "Hello";

//char *message2 = "World";

//pthread_attr_t pthread_attr_default;

printf("\nThread 1");

pthread_create( &thread1, NULL, (void*)&print_message_function, NULL);

printf("\nThread 2");

pthread_create( &thread2, NULL, (void*)&print_message_function, NULL);

pthread_exit(NULL);

exit(0);

}

5 Replies

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

    Hi Guillemn,

    Try these changes:

    <div class='quotetop'>QUOTE </div>

    --- Quote Start ---

    /*void print_message_function( void ){*/

    void print_message_function (void *arg){

    char message = "Hello";

    printf("%s ", message);

    pthread_exit(NULL);

    }

    void main (int argc, char *argv[]){

    pthread_t thread1, thread2;

    //char *message1 = "Hello";

    //char *message2 = "World";

    //pthread_attr_t pthread_attr_default;

    printf("\nThread 1");

    pthread_create( &thread1, NULL, (void*)&print_message_function, NULL);

    printf("\nThread 2");

    pthread_create( &thread2, NULL, (void*)&print_message_function, NULL);

    /*pthread_exit(NULL);*/

    /* Wait for threads to exit before trashing the pthread_t vars (on stack)*/

    pthread_join (thread1, NULL);

    pthread_join (thread2, NULL);

    exit(0);

    }[/b]

    --- Quote End ---

    Regards,

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

    Hello, I compiled another time the file corrected, but the both files can be compiled perfectly, without warning and it can be put in uClinux. But when starts the both execution, the system renicialize every time.

    Can it be a problem with a uClinux configuration? what I can do??

    Thanks,

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

    include Rules.mak

    # # configurable options# - set DEBUG = 1 to turn on debugging support#

    DEBUG = 0

    PROJ_NAME = threads

    INSTALL_DIR =

    PROGS := $(PROJ_NAME).exe

    CFLAGS +=

    # # You should not need to modify anything beyond this point#

    ifeq &#39;$(DEBUG)&#39; &#39;1&#39;

    CFLAGS += -O0 -g

    PROGS += $(PROGS:.exe=.gdb)

    endif

    all: $(PROGS)

    threads.bin: threads.o

    .PHONY: clean

    clean:

    -rm -f *.[oad] *.elf *.gdb *.bin *.exe

    .PHONY: install

    install: all

    ifeq "$(INSTALL_DIR)" ""

    $(error No installation directory specified)

    endif

    mkdir -p $(INSTALL_DIR)/bin

    install -v $(filter %.exe, $(PROGS)) $(INSTALL_DIR)/bin

    this is the Makefile, do you need the rules.mak too?

    thanks for all,

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

    Add this to your Makefile, check and make sure -lpthread appears before -lc.

    LDLIBS:=-lpthread $(LDLIBS)