0

the following is a c program where I want to implement an array of thread. there are two thread functions. I want to send an int value inside each function. But the code isn't giving any output. sample program:

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


void * threadFunc1(void * arg)
{

    int id = *((int *) arg);
    printf("Inside threadfunc2 for thread %d",id)
}

void * threadFunc2(void * arg)
{
    int i= *((int *)arg);
    printf("Inside threadfunc2 for thread %d",i)

}

int main(void)
{

    pthread_t thread[10];

    for(int i=0;i<10;i++)
    {

        pthread_create(&thread[i],NULL,threadFunc1,(void*)&i ); // want to send the value of i inside each thread

        pthread_create(&thread[i],NULL,threadFunc,(void*)&i );
    }


    while(1);
    return 0;
}

Is there anything wrong in the code?

Iharob Al Asimi
  • 51,091
  • 5
  • 53
  • 91
xls
  • 23
  • 5
  • If you are using C++, use `std::thread`, that's one thing wrong. Then, the C tag in your question would be wrong, too. In any case, it lacks info what the code does when executed and what you expected. My crystal ball tells me that you should try to output the pointer passed to the thread function using the `%p` format specifier or just streaming it to `std::cout`. – Ulrich Eckhardt Oct 17 '15 at 07:14
  • Please see [@UlrichEckhardt](http://stackoverflow.com/questions/33183877/implementing-an-array-of-thread#comment54174239_33183877)'s comment. This is why everyone will tell you **not to tag a c question with c++ tag**. – Iharob Al Asimi Oct 17 '15 at 07:23
  • Can you share the output obtained. – Karthik Balaguru Oct 17 '15 at 19:20

2 Answers2

1

Just add a "\n" terminator to the strings in printf inside your thread functions. This would force flushing the output buffer.

There are also some syntax errors in the code you pasted, but you'll probably figure those out easily. And you can just use pthread_join() instead of while (1); ...

Community
  • 1
  • 1
dragosht
  • 3,177
  • 2
  • 20
  • 30
0

The thread[i] should be an unique identifier for the new thread returned by the pthread_create.(It will hold the thread ID of the newly created thread.) However, in the code provided here, the thread[i] is overwritten by the second pthread_create(). One approach could be to have separate array of pthread_t for threadFunc1 and threadFunc as below :

pthread_t thread_func[10];
pthread_t thread_func1[10];

For passing argument of datatype int to the thread, you need to allocate an int on the heap and pass it to pthread_create() as below :

for(i=0;i<10;i++)
{
    int *arg = malloc(sizeof(*arg));
    *arg = i;
    pthread_create(&thread_func[i],NULL,threadFunc,(void*)arg ); 
    int *arg1 = malloc(sizeof(*arg1));
    *arg1 = i;
    pthread_create(&thread_func1[i],NULL,threadFunc1,(void*)arg1 );
}

Ensure to free the memory from heap in the respective thread function as below :

void *threadFunc(void *i) {
    int a = *((int *) i);
    printf("threadFunc : %d \n",a);
    free(i);
}
void *threadFunc1(void *i) {
    int a = *((int *) i);
    printf("threadFunc1 : %d \n",a);
    free(i);
}

Also, use pthread_join as below instead of while at end:

for (i=0;i<10;i++)
{
    pthread_join(thread_func[i],NULL);  
    pthread_join(thread_func1[i],NULL);  
}
Karthik Balaguru
  • 5,685
  • 6
  • 39
  • 55