在 QThread 中的 run() 方法内部声明与在 QThread 中声明为局部变量
考虑以下代码片段:
class ThreadA::QThread
{
public:
ThreadA()
{
}
void run()
{
myVariable = new int();
*myVariable = 10;
}
void Set(int var)
{
*myVariable = var;
}
private:
int* myVaraible;
}
以及以下代码:
class ThreadB::QThread
{
public:
MyThreadB()
{
}
void run()
{
myVariable = 10;
}
void Set(int var)
{
myVariable = var;
}
private:
int myVaraible;
}
我知道互斥体、竞争条件等的一般理论,
假设 Set 始终在线程启动后调用(即在调用 run() 之后),哪个线程拥有“myVariable” “在ThreadA和ThreadB的执行中??
这样的场景下主线程和QThread如何共享资源?
myVariable 在 QThread(即 ThreadA 和 ThreadB)及其主要应用中的范围和有效性是什么?
谢谢, 毗湿奴。
Consider the following code snippets:
class ThreadA::QThread
{
public:
ThreadA()
{
}
void run()
{
myVariable = new int();
*myVariable = 10;
}
void Set(int var)
{
*myVariable = var;
}
private:
int* myVaraible;
}
and the following code:
class ThreadB::QThread
{
public:
MyThreadB()
{
}
void run()
{
myVariable = 10;
}
void Set(int var)
{
myVariable = var;
}
private:
int myVaraible;
}
I know the general theory of Mutexes, Race condition etc,
Assuming Set is always called after the thread is started, (i.e after a call to run()), Which thread owns "myVariable" in the execution of ThreadA and ThreadB ??
How does the main thread and QThread share resources in such a scenario ??
What is the scope and validity of myVariable within the QThread(i.e. ThreadA and ThreadB) and its main application ??
Thanks,
Vishnu.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,QThread 不是线程。它是线程的管理器/控制器。 QThread 对象总是在与其要控制的线程不同的线程中创建。
假设您的两个自定义 QThread 对象都是在主 GUI 线程中创建的,则 ThreadA::myVariable 将在其自己的线程中分配。而ThreadB::myVariable将在主线程中分配。
如何在线程之间共享信息取决于每个线程所需的访问权限。如果它总是由一个线程读取并由另一个线程写入,那么您应该能够摆脱简单的成员访问函数。如果两个线程都可以写入,那么就需要互斥锁。 QReadWriteLock 提供了清晰的语义并对此进行了一些优化。
您还可以使用事件和信号跨线程发送数据。事件的传递是线程安全的,并且连接正确,信号也是如此。
First of all, a QThread is not a thread. It's the manager/controller of a thread. A QThread object is always created in a thread that is different from the thread it is going to control.
Assuming both of your custom QThread objects are created in the main GUI thread, ThreadA::myVariable will be allocated in its own thread. While ThreadB::myVariable will be allocated in the main thread.
How you can share information between threads depends on the accesses you need from each threads. If it's always read by one thread and written by another, you should be able to get away with simple member access function. If it can be written by both threads, then a mutex lock is needed. QReadWriteLock provides a clear semantic and some optimization for that.
You can also use event and signal to send data across thread. The delivery of event is thread safe and with proper connection, so is signal.
使用线程时请始终记住:所有数据在线程之间共享 线程编程(c/c++)是单个数据、多个执行。
没有定义线程的数据所有权。进程的所有资源(包括内存)每次都可以从每个线程访问。对资源的访问必须由程序员使用封装、互斥、信号槽解耦等来调节。
您可能知道这一点,但我不想听起来居高临下。但是,区分程序中的哪些数据永远不会从多个线程访问以及哪些数据是(或可能是)是多线程编程中最关键的问题。
When using threads always keep in mind: All data is shared between the threads Thread programming (c/c++) is single data, multiple execution.
The is no definition of ownership of data to threads. All of a process' resources, which includes memory, may be accessed from every thread every time. Access to resources must be regulated by the programmer using encapsulation, mutexes, signal-slot decoupling etc
You probably know this and I don't want to sound patronizing. But telling which data in a program is never accessed from more than one thread and which data is (or might be) is the most crucial issue in multithread programming.