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:
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);
}
}
The method is:
void * Method_Encryption(void *arguments){
.
.
.
StructInside *args = (StructInside *)arguments;
for (i = 0; i < numOfRows; ++i)
{
plainText[i] = args->plainText[i];
FinalKey[i] = args->FinalKey[i];
}
.
.
.
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.