Click here to Skip to main content
15,888,119 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I am writing a program that runs two threads (pthreads). The code runs fine on some parts but in some parts the results are like symbols.

Is there an expert on threads or like a community that I can post my issue which are dedicated to multi-threading?

I really need help, thank you in advance.

[Edit added code]
This is the part of the code that has the problem:

C++
void Encryption()
{
	int i = 0, j = 0, rc=0;
	ourStruct args[Chunks]; 
	pthread_t thread[Threads];

	ReadOurFile();
	counter = 0;
	for (j = 0; j <= numOfChunks; j++)
	{
		for (i = 0; i < numOfRows; ++i)
		{
			args[j].plainText[i] = InPut[j][i];
			args[j].FinalKey[i] = FinalKey[j][i];
		}
		args[j].index = counter;
		counter++;
	}
	for (j = 0; j <= numOfChunks; j=j+2)
	{
		rc = pthread_create(&thread[0], NULL, Method_Encryption, &args[j]);
		rc = pthread_create(&thread[1], NULL, Method_Encryption, &args[j+1]);
		rc = pthread_join(thread[0], NULL);
		rc = pthread_join(thread[1], NULL);
	}
	//writing the output to a file
}

The method is:
void * Method_Encryption(void *arguments){
.
.
.
StructInside *args = (StructInside *)arguments;
	//setting the received arguments into plaintext and key
	for (i = 0; i < numOfRows; ++i)
	{
		plainText[i] = args->plainText[i];
		FinalKey[i] = args->FinalKey[i];
	}
.
.
.
//store results
	for (i = 0; i < numOfRows; ++i)
		OutPut[args->index][i] = plainText[i];
}

The output is filled inside an array of output inside the method. The array is indexed by the order.
numOfChunks ==the number of chunks I'm reading from the file. For example: if the file is of size 100 characters, and each chunk will be of size 5 characters, then:
numOfChunks=100/5
We also tried using mutex inside the struct and initialize the mutex and lock it inside the method and afterwards unlocking and destroying the mutex but it didn’t work, or maybe we didn’t implement it correctly.
Posted
Updated 13-Feb-14 22:19pm
v2
Comments
Vedat Ozan Oner 11-Feb-14 16:08pm    
it is hard to say what is happening according to that information. but 'the results are like symbols' gives some clue. two possibilities with that.
1- may be an improper pointer usage which may change the data unintentionally.
2- your code may not be thread-safe. in that case, one of threads may change a common data since you don't guard it while another is processing that data.
it would be better to get more information or code sample.
Afnan_ 11-Feb-14 16:22pm    
I encrypt a file and then decrypt it using these two threads to speed the process. The decryption runs fine on most of the file, only some chunks of the file are decrypted incorrectly (the actual plain-text isn't returned/decrypted correctly). This problem happens ONLY when the size of the file is big.
Sergey Alexandrovich Kryukov 11-Feb-14 16:35pm    
This is not related to threading; if data manipulation is incorrect, it is incorrect. (Of course, unless you allowed for race conditions, but I don't expect such level of stupidity from anyone who can use threads, so I would assume you didn't.)
Anyway, there is nothing to discuss before you provide some relevant code sample.
By the way, is there any special reason to work in plain C, not even C++?
—SA
Afnan_ 11-Feb-14 16:47pm    
No, data manipulation is correct because:
1. most of the file is decrypted correctly.
2. when I run the code without threads I get the right results (the whole file is decrypted correctly).
3. these incorrect results happen at random places, as in, sometimes at the beginning of the file, some times in the middle or at the end, when I run the threads.

We are using C because it is more secure and faster.
Albert Holguin 11-Feb-14 16:51pm    
How are you sharing the data? ...this just about points to thread data access problems (or lack of thread synchronization).

Your code may access uninitialized memory.

Your loops run up to numOfChunks inclusive where numOfChunks is file size / chunk size (I would expect '<' as stop condition). You did not show the definitions for buffers like Input and FinalKey, but accessing these with index numOfChunks may get undefined data or generate access violations if the buffers are not large enough.

Your thread creation loop uses increments of two. If numOfChunks is odd, your second thread will definitely access undefined data or generate an access violation if the buffers are not large enough.
 
Share this answer
 
Comments
Afnan_ 16-Feb-14 9:14am    
After debugging, we have reached the following conclusion:
The Method_Encryption is causing all the problems (shared variables are being accessed at the same time causing this issue). So we are going to lock (using mutex)the variables inside the method rather than on the structs.
Here is the bug:
C++
for (j = 0; j <= numOfChunks; j++) { /* ... */ }

Should be:
C++
for (j = 0; j < numOfChunks; j++) { /* ... */ }

This is because in index j equals to numOfChunks, you are addressing past the memory allocated by the array args.

—SA
 
Share this answer
 
Comments
Afnan_ 15-Feb-14 12:50pm    
I'll look into your solutions,hopefully they'll solve the issue.

Thank you
merano 15-Feb-14 18:34pm    
ourStruct args[Chunks];

for (j = 0; j <= numOfChunks; j=j+2) {
rc = pthread_create(&thread[0], NULL, Method_Encryption, &args[j]);
rc = pthread_create(&thread[1], NULL, Method_Encryption, &args[j+1]);

The Field args has to be bigger (not equal) than numOfChunks because of [j+1]
Cant see where numOfChunks is calculated.
It was in the data manipulation ,as in , it was in the algorithm itself that was calculating the values and NOT in threads.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900