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?

3
  • 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. Commented Oct 17, 2015 at 7:14
  • Please see @UlrichEckhardt's comment. This is why everyone will tell you not to tag a c question with c++ tag. Commented Oct 17, 2015 at 7:23
  • Can you share the output obtained. Commented Oct 17, 2015 at 19:20

2 Answers 2

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); ...

Sign up to request clarification or add additional context in comments.

Comments

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);  
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.