跨线程数据访问 [英] Cross thread data access

查看:81
本文介绍了跨线程数据访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有一个客户端进程在一个线程和更多线程中运行,这些线程正在访问客户端进程提供的不同数据。我正在考虑将客户端进程作为静态类,以便任何线程都可以轻松访问数据(只读)。

我是否必须使用线程锁定?将数据共享(提供)到另一个线程的最佳做法是什么?

Hi,

I have a "client process" runing in one thread , and more threads, that are accessing different data that are provided by "client process". I am considering making a "client process" as static class, so that data would be easily accessible by any thread (read only).
Do I have to use thread locking ? What would be the best practice to share (provide) data to another threads?

推荐答案

哦,是的!你确实......



使它静态不会影响可读性 - 只有正常的可访问性:你不能访问静态内的任何基于实例的数据线程。



如果你有两个任务访问相同的数据,那么数据需要是线程安全的 - 在你的情况下几乎肯定意味着锁定。 />


问题是数据访问不是单指令过程:甚至

Oh yes! You do indeed...

Making it static will not affect the readability - only the accessibility as normal: you would not have access to any instance based data within the static thread.

If you have two tasks accessing the same data, then the data needs to be made thread safe - and in your case the almost certainly means locking.

The problem is that data access is not a "single instruction" process: even
i++;

是三个独立的操作:

1)获取当前值 i

2)添加一个到值

3)将值保存到 i

多线程意味着执行指令的线程可以部分通过并且不同的线程进入并修改fetch和add操作之间的值。所以当第一个线程重新运行时,它会覆盖第二个线程刚写的新版本。当线程在不同的核心上运行时,情况会更糟,因为它们可能真的互相混乱,并且可能会出现可怕的,可怕的间歇性错误(在开发过程中似乎永远不会发生,但每天都会在生产中发生......) />


如果要访问不同线程中的相同数据,则需要考虑将其锁定以确保安全。

is three separate operations:
1) Fetch the current value of i
2) Add one to the value
3) Save the value to i
And multithreading means that the thread executing the instruction could be switched out part way through and a different thread get in and modify the value between the "fetch" and the "add" operations. So when the first thread gets back in to run, it overwrites the newer version the second thread just wrote. The situation is even worse when you have threads running on separate cores, as they can really mess each other up and case horrible, horrible intermittent errors (which never seem to happen in development, but happen every day in production...)

If you are accessing the same data in different threads, you need to consider locking it for safety.


这是否也适用于只有线程A写入数据(变量),所有其他线程只读取线程A中的数据。



例如:

线程A提供测量数据(在无限循环中,它定期测量数据)。其他线程正在读取度量数据,并在必要时或根据用户请求执行操作。



请求线程A,也可以从其他线程(当时只有一个)获取命令,并在线程A与之通信的设备上设置一个值。 />


例如:

线程B请求:设备上的SET值x为1



后等待,直到x设置为1



while(measure.x!= 1)

{

SET x = 1;

}
Does this apply also if only thread A is writing data (variable) and all other threads are only reading data from thread A.

For example:
Thread A provides measure data (in endless loop it periodically measures data). Other threads are reading measure data and act when necessary or on user request.

On request thread A, can also take command from other threads (only one at the time) and set a value on device that thread A is communicating with.

For example:
thread B request: SET value x on device to 1

after that thread B waits until x is set to 1

while (measure.x != 1)
{
SET x = 1;
}


这篇关于跨线程数据访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆