Subj : Re: copying problem or thread not starting?? To : comp.programming.threads From : kelly Date : Tue Mar 29 2005 03:30 pm I added pthread_join and it worked. Thanks. kelly wrote: > Hi, > The program below compiles and link ok using gcc. However it is not > giving me the result I want. My long string is not being copied to > result1 or result2. I even tried array to array copy and still cannot > get my string. Experts could you help me here. Thx. > > #include "stdio.h" > #include "stdlib.h" > #include "time.h" > #include "string.h" > > /// Multithread header > #include "pthread.h" > > static char this_is_the_source_string[49] = "This is a long string that > need to stay intact."; > static char this_is_the_comparaison_string[49] = "This is a long string > that need to stay intact."; > > static char result1[49]; > static char result2[49]; > > // First thread function > void * ThreadFunc1(void *threadid) > { > int c; > > char *a = this_is_the_source_string; > > printf("1 Start\n"); > strcpy(result1, a); > printf("1 End\n"); > > return 0; > } > > // Second thread function > void * ThreadFunc2(void *threadid) > { > > char *a = this_is_the_source_string; > > printf("2 Start\n"); > strcpy(result2, a); > printf("2 End\n"); > > return 0; > } > > int main() > { > pthread_t threads; > int t; > int a=0; > int b=1; > > // First Tread information > long dwThreadId1, dwThrdParam1 = 1; > int hThread1; > long exitCode1 = 9; > > // Second Tread information > long dwThreadId2, dwThrdParam2 = 1; > int hThread2; > long exitCode2 = 9; > > // Other > int resultB1, resultB2; > > printf("Start of the program\n"); > > // Create the first thread > hThread1 = pthread_create(&threads, NULL, ThreadFunc1, (void*)&a); > if (hThread1){ > printf("ERROR; first pthread_create() fail ID is %d\n", hThread1); > exit(99); > } > > > // Create the second thread > hThread2 = pthread_create(&threads, NULL, ThreadFunc2, (void*)&b); > if (hThread2){ > printf("ERROR; second pthread_create() fail ID is %d\n", > hThread2); > exit(99); > } > > /* > hThread2 = CreateThread( > NULL, // default security attributes > 0, // use default stack size > ThreadFunc2, // thread function > NULL, > 0, // use default creation flags > &dwThreadId2); // returns the thread identifier > if (hThread2 == NULL) // Make sure the thread is created > { > printf("CreateThread failed: 2\n" ); > exit(2); > };*/ > > /* > // Wait for first thread finish > while (exitCode1 == STILL_ACTIVE) > { > GetExitCodeThread(hThread1, &exitCode1); > } > > // Wait for second thread finish > while (exitCode2 == STILL_ACTIVE) > { > GetExitCodeThread(hThread2, &exitCode2); > }*/ > > printf("End of the thread\n"); > > printf("Result 1: %s\n", result1); > printf("Result 2: %s\n", result2); > > resultB1 = strcmp(result1, this_is_the_comparaison_string); > resultB2 = strcmp(result2, this_is_the_comparaison_string); > > if (resultB1 != 0) > { > printf("End of the program with fail\n"); > > pthread_exit(NULL); > exit(3); > } > > else if (resultB2 != 0) > { > printf("End of the program with fail\n"); > > pthread_exit(NULL); > exit(4); > } > > else > { > printf("End of the program with success\n"); > > pthread_exit(NULL); > exit(0); > } > > sleep(1); > > }; > .