我做了两个线程,一个必须读另一个必须写.但是我得到了未定义的行为,有时我可以读1行,有时1000行.它对我来说没有多大意义.
我做的是以下内容:1.我在main.cpp 2中创建了一个带有mkfifo()的fifo.我启动了2个线程,一个读取,另一个写入.reader.cpp,writer.cpp
在那些线程中,每个循环我打开fifo并关闭它,因为如果我只在循环外执行一次,它就不会工作,我觉得这也很奇怪.
我一直在寻找好的例子,但我找不到.
我的问题很简单,如何使fifo(Reader)等待传入数据并在可用时读取它.它应该能够以4Mhz运行.
我希望有人可以帮助我,因为这是第三天我突然想到这一点.如果使用Qt 4.8重要.
编辑:我找到了我的问题的解决方案:
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();
}
Run Code Online (Sandbox Code Playgroud)
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: …Run Code Online (Sandbox Code Playgroud) 我正在尝试列出变量类型的模板类。所以这个想法是循环一个对象列表,这些对象都有一个共同的函数,例如getValue,但类型不同。类型可以是任何类型、原始类型或对象。
我需要这个是因为我想要一个类,其中包含我希望能够在运行时构造的不同类型的属性列表。
所以我的课程看起来像:
class MyClass {
std::list<Attribute<?>*> attributes;
};
Run Code Online (Sandbox Code Playgroud)
还有我的属性模板:
template<typename T>
class Attribute {
public:
Test(const T &t) : _t(t) {}
T getValue() const { return _t; }
void setValue(const T &t) { _t = t; }
private:
T _t;
};
int main() {
MyClass myClass;
myClass.attributes.push_back(new Attribute<int>(42));
myClass.attributes.push_back(new Attribute<double>(42.0));
}
Run Code Online (Sandbox Code Playgroud)
正如你所看到的我放的 MyClass 列表?因为那是我的问题。我不知道如何制作一个列表,该列表将采用不同类型的属性模板,即 int、double 等。
std::list<Attribute<?> *> attributes;
Run Code Online (Sandbox Code Playgroud)
在 Java 中,可以使用泛型。在 C++ 中是否可以通过某种构造来做到这一点?我尝试使用可变参数模板,但这似乎无助于解决我的问题。
我需要这个,但不是在 Java 中,在 C++ 中:
public class GenericAttribute<T> {
private T value;
public GenericAttribute (T …Run Code Online (Sandbox Code Playgroud) 我问这个问题是因为我不知道如何用谷歌搜索,我找不到正确的关键字。
我有一个QListWidget,里面有字符串。字符串非常长,如果我禁用水平滚动(这正是我想要的),则文本以 ... 结尾,因为它太长了。
我想让文本显示文本的结尾和开头,例如:
This is very long text改为This is ... 文本而不是This is very long ...
有没有简单的方法可以实现这一点而无需操作字符串?之后我需要完整的字符串,并且我不想存储额外的数据。任何帮助表示赞赏。