Question on Linux multithread programming
-
Hello, In my program, I first receive data from another machine, then processing the data. Data receiveing is fast, but data processing is relatively time consuming. Hence, I put the code for data processing in a POSIX thread (i am using RedHAt enterpriese linux 4). In the main() function, I receive data first. Whevenever a data is received, a new thread for data processing is created. I use the following code to test the above program design. It seemed dones not work.
void * thread_func(void * arg)
{printf("thread_func is running %s\n" ,(void *)arg);
}
main()
{
thread_t thread[100];
char *str;
int i;
for(i=0; i<10; i++
{
str = (char*)malloc(sizeof(char)*200);
strcpy(str, "Hello World\n");
int ret;
ret = pthread_create(&thread[i] ,NULLthread_func,(char *)str);
printf("after pthread_cread ret= %d",ret);
free(str);
pthread_detach()thread[i];// I cannot use pthread_join here, since I could not wait until the current thread finishes there are a lot of incoming data arriving.
}}
I noticed the value of 'ret' is 0, indicating that 10 threads have been created successfully. However, I could not see the sentence "thread_func is running ...... Could anybody look at this code and let me know why it does not work . Thanks