0

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.

k7499
  • 3
  • 4

2 Answers2

2

the output is not in the correct order, and some lines are mixed

Because, your program accepts the readings and writings at the same time. For example, when one thread tries to read (display) the context of the line, there is at least one thread that edits this line, so it makes the context of line change when the reader has not finished to read.

You have to synchronize the reading and writing.

  • If one of the threads tries editing the line, no other threads should be displaying at the same time, otherwise changes will not be visible to him/her.

  • However if some threads is displaying the line, then others may read it at the same time. In your case, you want only one thread to display the line, so you can ignore this rule.

    You can find the examples of the implementation:

    1. Reader-Writer problem using Monitors using the mutex and signal

    2. Readers-Writers Problem using the semaphore

    3. POSIX Threads Programming very helpful tutorial of multithreading

Hitokiri
  • 3,607
  • 1
  • 9
  • 29
  • I don't want to use mutex or semaphore because I want to have multithreading. – k7499 May 07 '20 at 10:02
  • But only one thread can work at the time. Do you know how to implement it with a buffer? – k7499 May 07 '20 at 10:06
  • the mutex and semaphore are created for the synchronization in multithreading. – Hitokiri May 07 '20 at 10:08
  • `But only one thread can work at the time`. You can create many threads to edit the buffer at the same time, but difference thread edits difference part of buffer. For example, you hace the buffer `abc def`, thread 1 edits `abc` part, and thread 2 edits `def` part – Hitokiri May 07 '20 at 10:13
  • Should the buffer looks something like this? ```buff[i] = pthread_create(&tid_array[i], NULL, searchForWord1, &wData[i]);``` – k7499 May 07 '20 at 10:15
  • `pthread` does not return `char`. I cannot help you without your code. That is all things i can help. This link can help you so much https://computing.llnl.gov/tutorials/pthreads/ – Hitokiri May 07 '20 at 10:19
  • I added more code. And I have already read that, but still don't know how to solve my problem – k7499 May 07 '20 at 10:26
  • your edit is not enough, you should join all thread at the end. it means after creating `tid`. Read carefully the tutorial, and 2 examples in my answers to understand what is multithreading, what is critical section, what is mutex, semaphore and how to use them in the code for synchronizing the thread, etc – Hitokiri May 07 '20 at 10:35
  • Should I join it with ```pthread_join```? And am I using second pthread_create correctly? – k7499 May 07 '20 at 10:46
  • Yes, pthread_create all threads, then pthread_join all thread. – Hitokiri May 07 '20 at 10:50
  • Can you give me a hint how to create all of them, so the output is displayed in correct order? – k7499 May 07 '20 at 11:03
  • This post in c++, but you can apply to C https://stackoverflow.com/questions/36824992/single-reader-multiple-writers-with-pthreads-and-locks-and-without-boost. if you like my answer, click on check mark to accept it. – Hitokiri May 07 '20 at 13:07
0

You should make sure that each line is written atomically. It is possible to use a mutex and make each individual thread lock the buffer temporarily. This is probably bad design. It is likely better to have each thread make sure it writes an entire line at a time, so use a per-thread buffer for that. This might prevent you from needing a mutex, because this can introduce complexity and bugs.

Another design is to have a buffer and mutex per thread, and have your output thread regularly iterate over all threads, lock the mutex for that thread, pull the contents of the buffer, unlock the mutex, output the buffer. This is a design that allows for different priorities.

The lines being in the wrong order... Well that is multi-threading for you. If that is a problem, then you have probably messed up your design.

Cheatah
  • 1,825
  • 2
  • 13
  • 21