Click here to Skip to main content
14,095,863 members
Rate this:
 
Please Sign up or sign in to vote.
I'm writing a program that creates two threads. Each thread is responsible for reading through one text file, with one char on each line.

The first is formatted like:

h
0
h
0
...

The second is formatted like:

0
i
0
i
0
i

Sometimes there can be multiple letters after each other, or multiple zeros after each other. However, the one certainty is that if there is a letter on one line of one file, the corresponding line of the second file will have a 0, and vice versa.

The threads are supposed to keep reading the file input into a global char array until they reach a zero. At this point, they allow the other thread to take over. And they keep going back and forth until both files are completely read.

At this point, when I run, I get variations of either (1) many h's followed by many i's or (2) (the correct answer) a continuous stream of hihihi's, or (3) sometimes many i's followed by many h's. So, I know that my synchronization methods are off.

Here is an example of one of my threads: (Note both threads are exactly the same, except for the file being opened.)

void *getMessage1()
{
FILE *studentOne = fopen("Student1", "r");

size_t howManyChars;
char *placeHolderChars; 
int count = 1;
while (count < 501)
{
    placeHolderChars = NULL;
    getline(&placeHolderChars, &howManyChars, studentOne);

    if(strcmp(placeHolderChars, "0\n") == 0) //if we've reached a zero
    {

         pthread_mutex_unlock(&lock); 
    }
    else
    {   while(1)
        {
            if(pthread_mutex_trylock(&lock) == 0)
            {

                break;
            }
        }

        if(strlen(placeHolderChars)>0)
        {
             placeHolderChars[1] = '\0';
        }

        strcat(message,placeHolderChars);
    }

    free(placeHolderChars);

    if(feof(studentOne))
    {

        pthread_mutex_unlock(&lock); //unlock
        fclose(studentOne);
        break;
    }
    count++;

 }

return 0;
}


Here is my main method:

int main(void)
{
pthread_t id1;
pthread_t id2;

pthread_create((&id1), NULL, getMessage1, NULL);
pthread_create((&id2), NULL, getMessage2, NULL);

pthread_join(id1, NULL);
pthread_join(id2, NULL);

int j;

for (j = 0; j < 1001; j++) 
{
     printf ("%c ",message[j]);
}

return 0;
}


What I have tried:

I have tried using various combinations of lock, unlock (mutex) and wait & signal. A user on stackoverflow told me that I should use semaphores instead of mutex.However, my professor told me that mutex would be the easiest/best way to do this.

I would appreciate any guidance on how I can better use lock, unlock, wait, and/or signal to create a working synchronization technique with consistent results.
Posted
Updated 23-Sep-17 16:53pm

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

First thing to do is to use the debugger and make sure everything is as expected. Inspect variables as they change. Check everything until only the mutex remain as explanation of problem.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
   

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web02 | 2.8.190518.1 | Last Updated 23 Sep 2017
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100