I am having problem displaying data with multiple threads, because the output is not in the correct order, and some lines are mixed. My multithread function looks like this, and it displays all the data.
for (int i = 0; i < 3; i++)
{
pthread_create(&tid_array[i], NULL, find, &wData[i]);
}
for (int i = 0; i < 3; i++)
{
pthread_join(tid_array[i], NULL);
}
Now I want to create a buffer to which threads will write outputs and then create one thread that will display all the data. I have no idea how to do it. Can anyone help me? EDIT I don't want to use mutex.
EDIT 2
int rc;
for (int i = 0; i < 3; i++)
{
rc = pthread_create(&tid_array[i], NULL, find, &wData[i]);
}
for (int i = 0; i < 3; i++)
{
rc = pthread_join(tid_array[i], NULL);
}
pthread_t tid;
pthread_create(&tid, NULL, (void *)rc, NULL);
pthread_join(tid, NULL);
Is this the right way? The function find prits all the data.