Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8575181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:38:11+00:00 2026-06-11T19:38:11+00:00

I made 2 threads, one has to read the other has to write. But

  • 0

I made 2 threads, one has to read the other has to write.
But i get undefined behaviour, sometimes i can read 1 line, sometimes 1000. It does not make alot of sense to me.

What i do is the following:
1. i create a fifo with mkfifo() in main.cpp
2. i start 2 threads, one that reads, the other writes. reader.cpp, writer.cpp

In those threads, every loop i open the fifo and i close it, because it wont work if i only do that once outside the loop, which i find also strange.

I have been looking for good examples but i found none.

My questions is simple, how can i make the fifo (Reader) wait for incoming data and read it when it is available. It should be able to run at 4Mhz.

I hope that somebody can help me out because this is the 3rd day i am breaking my head on this. If it matters im using Qt 4.8.

EDIT: I found the solution to my problem:

main.cpp

#include <QtCore/QCoreApplication>
#include "reader.h"
#include "writer.h"
#include <sys/types.h>  // mkfifo
#include <sys/stat.h>   // mkfifo
#include <fcntl.h>

int main(int argc, char *argv[]) {

    QCoreApplication a(argc, argv);

    int fifo = mkfifo("/tmp/fifo", S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH);

    Reader r;
    Writer w;
    r.start();
    w.start();

    return a.exec();
}

writer.h

#ifndef WRITER_H
#define WRITER_H

#include <QThread>
#include <stdio.h>
#include <iostream>
#include <errno.h>
#include <string.h>
#include <fcntl.h>

class Writer : public QThread {

    Q_OBJECT

public:
    explicit Writer(QObject *parent = 0);

private:
    void run();

};

#endif // WRITER_H

reader.h

#ifndef READER_H
#define READER_H

#include <QThread>
#include <stdio.h>
#include <iostream>
#include <errno.h>
#include <string.h>
#include <fcntl.h>

class Reader : public QThread {

    Q_OBJECT

public:
    explicit Reader(QObject *parent = 0);

private:
    void run();

};

#endif // READER_H

writer.cpp

#include "writer.h"

char * phrase = "Stuff this in your pipe and smoke it\n";

using namespace std;

Writer::Writer(QObject *parent) : QThread(parent) {}

void Writer::run() {

    int num, fifo;
    if ((fifo = open("/tmp/fifo", O_WRONLY)) < 0) {
       printf("%s\n", strerror(errno));
       return;
    }
    while (true) {

        if ((num= write(fifo, phrase, strlen(phrase)+1)) < 0) {
            printf("ERROR: %s\n", strerror(errno));
        }
    }
    close(fifo);

}

reader.cpp

#include "reader.h"

using namespace std;

Reader::Reader(QObject *parent) : QThread(parent) {}

void Reader::run() {

    int num, fifo;
    char temp[38];
    if ((fifo = open("/tmp/fifo", O_RDONLY)) < 0) {
        printf("%s\n", strerror(errno));
        return;
    }
    while (true) {
        if ((num = read(fifo, temp, sizeof(temp))) < 0) {
            printf("%s\n", strerror(errno));
        }
        printf("In FIFO is %d %s \n", num, temp);
    }
    close(fifo);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T19:38:12+00:00Added an answer on June 11, 2026 at 7:38 pm

    The basic read() and write() functions make no promise to read or write all available data.

    You need something like:

    int tot = 0;
    while (tot < sizeof(temp))
    {
        num = read(fifo, temp + tot, sizeof(temp) - tot);
        if (num < 0)
            break;
        tot += num;
    }
    

    And the same for write.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I made an array of threads but I do not use all of them.
I can see that this question has been asked several times, but none of
I'm learning how to use threads in Android, and to do that I've made
I posted a thread about this earlier and have made some progress but now
Made a custom obj called Item with some string fields and one float. .h
I made a dll in c# but i need to export the functions. Is
On one of my dev machines I have unlimited undos, I can hold u
I am writing a program which has 8 threads. I am implementing a barrier
I've read all 484 pages of Professional XMPP and read countless forum threads regarding
I have a Clojure network application, basic structure is like this: Server has one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.