Problem with Threads

Hello

I was trying to develop a program with some threads. And each thread, at the end of its job, should write the results to the same text file. But I want them to write in order. Meaning, first thread should write first, second should write second and so on. Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
void *threadRun(void *arg)
{
        //Runner is a class to keep some parameters
	Runner* rn;
	rn = (Runner*) arg;

        //NCC is a class to do the job
	NCC* ncc = new NCC(rn->ap);
	
        ncc->run();	
	
        //Writing the results
        ncc->writeTXT(rn->outputPath);
	
        pthread_exit(NULL);
}

void main(){

	int  iret, threadNum;
	threadNum = 2;
	pthread_t thread[threadNum];
	pthread_attr_t attr;

	pthread_attr_init(&attr);
	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

	for(int t=0; t<threadNum; t++)
	    iret = pthread_create( &thread[t], &attr, threadRun, this);


	pthread_attr_destroy(&attr);

	for(int t=0; t<threadNum; t++)
	    iret = pthread_join( thread[t], &status);

	pthread_exit(NULL);
}



Do you have any ideas how to do this? When I run the code, somethimes thread2 finishes the job earlier and writes first. And I am not getting any errors when these two threads try to access the same file.

Thanks
Kenter
Last edited on
You should use conditional variables in order to synchronize threads.
ok I spent some time with conditional variables but couldnt find a way to synchronize the threads. I tried to use mutex lock but again first thread which finishes the job is writing first. This first thread can be thread1 and thread2 as well.

Do you have any ideas to solve this problem?
There are two easy ways I can immediately think of in order to do that.
But before talking about that, I'd like to remind you that you didn't seem to take into consideration the synchronization of threads. You need to have locks on shared resources and conditional variable+lock number for thread_wait(). I hope you're not missing them in your mind and you've actually done it outside the code snippet shown here.

Back to the topic:

Way 1. force the threads to go to a wait queue immediately after they are done computing but hasn't written results to file. The wait queue can then check whether all threads with thread id (for example, you can easily maintain a value like max_id to track the max thread_id that has finished) < the one at the queue head have finished. If yes, let the thread at the head of the queue to write results to file; if not, put the popped-out thread back to the tail of the wait queue. Do this until the wait queue is empty. Note, however, that the program shouldn't end until all threads are done.

Way 2. let the threads finish in whatever order they want, and store all the results and producer ids of the results in some temporary data structure (say, use array to store results and array index to represent thread ids). After all threads are done, write the results in the data structure to the file in whatever orders you want.
Last edited on
Thanks for the reply. I will try to implement your suggestions.

For the synchronization of threads, I dont have any common variables. All the variables are being created and deleted inside the tread. Since the threads are not using a common resource, I am not using any locks or conditional variable. I hope this makes sense.

Since the threads are not using a common resource, I am not using any locks or conditional variable.

Are you sure? What about the file that is written by all threads? Shared variables are only one type of shared resources.
If I were you, I'd like to sit down and think about what can be shared resources and in what conditions a thread might need to wait.
Perhaps in your pthread_create() you could pass the thread_id of the thread with the higher priority than the current one. Then in your threadRun() function you could pthread_join the higher priority thread before writing the output.
Last edited on
Thanks for your help. I solved the problem. I applied the first option that h9uest suggested. I defined a global threadID variable and compare it with the threads IDs. And indeed, I used conditional variables for this.

Cheers
kenter
Great. I'm glad to hear you get it done.
Topic archived. No new replies allowed.