Skip to content

printf()in Pthreads not works (test_pthread_printf.cpp failed) #11543

Closed

Description

@tigercosmos

I have a simple pthread sample code, but the pthread's printf()s don't show when running NodeJS.

test.c:

#include <pthread.h>
 #include <stdio.h>
 #include <stdlib.h>
 #define NUM_THREADS     5

 void *PrintHello(void *threadid)
 {
    long tid;
    tid = (long)threadid;
    printf("Hello World! It's me, thread #%ld!\n", tid);
    pthread_exit(NULL);
 }

 int main (int argc, char *argv[])
 {
    pthread_t threads[NUM_THREADS];
    int rc;
    long t;
    for(t=0; t<NUM_THREADS; t++){
       printf("In main: creating thread %ld\n", t);
       rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);
       if (rc){
          printf("ERROR; return code from pthread_create() is %d\n", rc);
          exit(-1);
       }
    }

    /* Last thing that main() should do */
    pthread_exit(NULL);
 }

build:

emcc test.c  -s USE_PTHREADS=1 -s EXIT_RUNTIME=1 -o test.html

Node running:

$ node --experimental-wasm-threads --experimental-wasm-bulk-memory a.out.js
In main: creating thread 0
In main: creating thread 1
In main: creating thread 2
In main: creating thread 3
In main: creating thread 4

where I expect:

$ gcc test.c -lpthread && ./a.out
In main: creating thread 0
In main: creating thread 1
Hello World! It's me, thread #0!
Hello World! It's me, thread #1!
In main: creating thread 2
Hello World! It's me, thread #2!
In main: creating thread 3
In main: creating thread 4
Hello World! It's me, thread #3!
Hello World! It's me, thread #4!

The messages in void *PrintHello don't print out.

Environment:

emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 1.39.18
clang version 11.0.0 (/b/s/w/ir/cache/git/chromium.googlesource.com-external-github.com-llvm-llvm--project 613c4a87ba9bb39d1927402f4dd4c1ef1f9a02f7)
Target: x86_64-apple-darwin19.3.0
Thread model: posix
InstalledDir: /Users/tigercosmos/emsdk/upstream/bin
shared:INFO: (Emscripten: Running sanity checks)

NodeJS 12.18.1

Activity

  1. tigercosmos commented on Jul 5, 2020

    @tigercosmos
    Author

    Seems the test, https://github.com/emscripten-core/emscripten/blob/master/tests/pthread/test_pthread_printf.cpp, do the same thing, but I can run it neither.

    The test code: (remove #include <emscripten> part)

    #include <stdio.h>
    #include <stdlib.h>
    #include <pthread.h>
    
    #include <errno.h>
    #include <assert.h>
    
    void *ThreadMain(void *arg)
    {
    	printf("Hello from thread, string: %s, int: %d, double: %g\n", "str", 5, 42.0);
    	return 0;
    }
    
    int main()
    {
    	pthread_t thread;
    	int rc = pthread_create(&thread, NULL, ThreadMain, 0);
    		assert(rc == 0);
    
    		rc = pthread_join(thread, NULL);
    		assert(rc == 0);
    
    		printf("The thread should print 'Hello from thread, string: str, int: 5, double: 42.0'\n");
    }
    

    Run:

    $ emcc test.c  -s USE_PTHREADS=1 -s EXIT_RUNTIME=1 
    $ node --experimental-wasm-threads --experimental-wasm-bulk-memory a.out.js
    
    # nothing, it just hangs on ...
  2. changed the title [-]`printf()`in Pthreads not works[/-] [+]`printf()`in Pthreads not works (test_pthread_printf.cpp not works)[/+] on Jul 5, 2020
  3. changed the title [-]`printf()`in Pthreads not works (test_pthread_printf.cpp not works)[/-] [+]`printf()`in Pthreads not works (test_pthread_printf.cpp failed)[/+] on Jul 5, 2020
  4. sbc100 commented on Jul 5, 2020

    @sbc100
    Collaborator

    This is known issue with emscripten pthread support: New pthreads can't be created until you return the event loop (return from main).

    You can fix this in one of several ways:

    1. Build with -s PROXY_TO_PTHREAD
    2. Build with -s PTHREAD_POOL_SIZE=N where N > 0.
    3. Break up your main function using something like emscripten_set_main_loop

    We should really find a better way to report this issue as many people run into it.

  5. tigercosmos commented on Jul 6, 2020

    @tigercosmos
    Author

    @sbc100 No matter I use method 1 or method 2, it failed with:

    method 1

    $ emcc test.c  -s USE_PTHREADS=1 -s EXIT_RUNTIME=1emcc test.c  -s USE_PTHREADS=1 -s EXIT_RUNTIME=1 -s PROXY_TO_PTHREAD
    wasm-ld: error: duplicate symbol: ThreadMain
    >>> defined in /tmp/emscripten_temp_m219d24x/test_0.o
    >>> defined in /tmp/emscripten_temp_m219d24x/test_0.o
    
    wasm-ld: error: duplicate symbol: __original_main
    >>> defined in /tmp/emscripten_temp_m219d24x/test_0.o
    >>> defined in /tmp/emscripten_temp_m219d24x/test_0.o
    
    wasm-ld: error: duplicate symbol: main
    >>> defined in /tmp/emscripten_temp_m219d24x/test_0.o
    >>> defined in /tmp/emscripten_temp_m219d24x/test_0.o
    emcc: error: '/mnt/c/Users/tigercosmos/Desktop/test/em/emsdk/upstream/bin/wasm-ld -o /tmp/emscripten_temp_m219d24x/a.out.wasm /tmp/emscripten_temp_m219d24x/test_0.o -L/mnt/c/Users/tigercosmos/Desktop/test/em/emsdk/upstream/emscripten/system/local/lib /tmp/emscripten_temp_m219d24x/test_0.o -L/mnt/c/Users/tigercosmos/Desktop/test/em/emsdk/upstream/emscripten/system/lib -L/mnt/c/Users/tigercosmos/Desktop/test/em/emsdk/upstream/emscripten/cache/wasm /mnt/c/Users/tigercosmos/Desktop/test/em/emsdk/upstream/emscripten/cache/wasm/libc-mt.a /mnt/c/Users/tigercosmos/Desktop/test/em/emsdk/upstream/emscripten/cache/wasm/libcompiler_rt.a /mnt/c/Users/tigercosmos/Desktop/test/em/emsdk/upstream/emscripten/cache/wasm/libc-wasm.a /mnt/c/Users/tigercosmos/Desktop/test/em/emsdk/upstream/emscripten/cache/wasm/libc++-mt-noexcept.a /mnt/c/Users/tigercosmos/Desktop/test/em/emsdk/upstream/emscripten/cache/wasm/libc++abi-mt-noexcept.a /mnt/c/Users/tigercosmos/Desktop/test/em/emsdk/upstream/emscripten/cache/wasm/libdlmalloc-mt.a /mnt/c/Users/tigercosmos/Desktop/test/em/emsdk/upstream/emscripten/cache/wasm/libpthread-mt.a /mnt/c/Users/tigercosmos/Desktop/test/em/emsdk/upstream/emscripten/cache/wasm/libc_rt_wasm.a /mnt/c/Users/tigercosmos/Desktop/test/em/emsdk/upstream/emscripten/cache/wasm/libsockets-mt.a -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr --allow-undefined --import-memory --import-table --shared-memory --strip-debug --export main --export malloc --export free --export stackSave --export stackRestore --export stackAlloc --export __data_end --export __wasm_call_ctors --export fflush --export __errno_location --export emscripten_get_global_libc --export __pthread_tsd_run_dtors --export pthread_self --export __emscripten_pthread_data_constructor --export emscripten_futex_wake --export memalign --export proxy_main --export emscripten_main_t

    method 2

    $ emcc test.c  -s USE_PTHREADS=1 -s EXIT_RUNTIME=1emcc test.c  -s USE_PTHREADS=1 -s EXIT_RUNTIME=1 -s PTHREAD_POOL_SIZE=4
    wasm-ld: error: duplicate symbol: ThreadMain
    >>> defined in /tmp/emscripten_temp_k86offat/test_0.o
    >>> defined in /tmp/emscripten_temp_k86offat/test_0.o
    
    wasm-ld: error: duplicate symbol: __original_main
    >>> defined in /tmp/emscripten_temp_k86offat/test_0.o
    >>> defined in /tmp/emscripten_temp_k86offat/test_0.o
    
    wasm-ld: error: duplicate symbol: main
    >>> defined in /tmp/emscripten_temp_k86offat/test_0.o
    >>> defined in /tmp/emscripten_temp_k86offat/test_0.o
    emcc: error: '/mnt/c/Users/tigercosmos/Desktop/test/em/emsdk/upstream/bin/wasm-ld -o /tmp/emscripten_temp_k86offat/a.out.wasm /tmp/emscripten_temp_k86offat/test_0.o -L/mnt/c/Users/tigercosmos/Desktop/test/em/emsdk/upstream/emscripten/system/local/lib /tmp/emscripten_temp_k86offat/test_0.o -L/mnt/c/Users/tigercosmos/Desktop/test/em/emsdk/upstream/emscripten/system/lib -L/mnt/c/Users/tigercosmos/Desktop/test/em/emsdk/upstream/emscripten/cache/wasm /mnt/c/Users/tigercosmos/Desktop/test/em/emsdk/upstream/emscripten/cache/wasm/libc-mt.a /mnt/c/Users/tigercosmos/Desktop/test/em/emsdk/upstream/emscripten/cache/wasm/libcompiler_rt.a /mnt/c/Users/tigercosmos/Desktop/test/em/emsdk/upstream/emscripten/cache/wasm/libc-wasm.a /mnt/c/Users/tigercosmos/Desktop/test/em/emsdk/upstream/emscripten/cache/wasm/libc++-mt-noexcept.a /mnt/c/Users/tigercosmos/Desktop/test/em/emsdk/upstream/emscripten/cache/wasm/libc++abi-mt-noexcept.a /mnt/c/Users/tigercosmos/Desktop/test/em/emsdk/upstream/emscripten/cache/wasm/libdlmalloc-mt.a /mnt/c/Users/tigercosmos/Desktop/test/em/emsdk/upstream/emscripten/cache/wasm/libpthread-mt.a /mnt/c/Users/tigercosmos/Desktop/test/em/emsdk/upstream/emscripten/cache/wasm/libc_rt_wasm.a /mnt/c/Users/tigercosmos/Desktop/test/em/emsdk/upstream/emscripten/cache/wasm/libsockets-mt.a -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr --allow-undefined --import-memory --import-table --shared-memory --strip-debug --export main --export malloc --export free --export stackSave --export stackRestore --export stackAlloc --export __data_end --export __wasm_call_ctors --export fflush --export __errno_location --export emscripten_get_global_libc --export __pthread_tsd_run_dtors --export pthread_self --export __emscripten_pthread_data_constructor --export emscripten_futex_wake --export memalign --export emscripten_main_thr
  6. sbc100 commented on Jul 6, 2020

    @sbc100
    Collaborator

    It looks like you are including your test.c twice on the command line.. which would lead to duplicate symbols.

  7. tigercosmos commented on Jul 6, 2020

    @tigercosmos
    Author

    @sbc100 thanks! It works.
    Shall I close this issue?
    A piece of advice, IMO, it's better to update the tutorial. :)

  8. sbc100 commented on Jul 6, 2020

    @sbc100
    Collaborator

    Yes, lets close this issue.

    We do need to work on surfacing a fix for this common issue. I'll open a separate bug for that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions