No cout printing in pthread starting function -


i'm new here , noob pthread programming. problem in c++ class, i'm trying create encapsulate thread. reading around i'd seen when create pthread, need pass c function pthread_create runs on startup... so, when pthread runs function doesn't cout message on stdout!

but it's better if see code: (obviously it's copy , pasted internet tutorial ^^)

void *runatstart( void *threadid) {      long tid;    tid = (long)threadid;     printf("hello world! it's me, thread #%ld!\n", tid);    pthread_exit(null); }   thread::thread() {     pthread_t threads[1];    int rc;    long t;    for(t=0; t<1; t++){       printf("in main: creating thread %ld\n", t);       rc = pthread_create(&threads[t], null, runatstart, (void *)t);       if (rc){          printf("error; return code pthread_create() %d\n", rc);         // exit(-1);       }    } } 

in main call as:

int main() {     thread *th=new thread();     return 0; } 

the output generated is:

in main: creating thread 0 

i hope has understood! sorry english! :) inzirio

your program runs fine. problem you're seeing main() function returns before thread can run, , causes program exit.

a simple way prove add sleep(5); in main(), prior return call. better way find manner cause main() wait until of threads have completed before returns. 1 reasonable manner add destructor thread class performs pthread_join, , sure call destructor: delete th;


Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -