同步变量存储 [英] Synchronization Variable Storage

查看:72
本文介绍了同步变量存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们知道进程中的所有线程共享进程地址空间.
我们有各种同步机制,例如信号量,互斥量,自旋锁,临界区等.用于线程同步.

我想知道这些同步变量存储在进程地址空间的哪一部分?
一个线程如何访问另一个线程中声明的同步变量?如果这个问题不正确,请纠正我.

We know that all the threads in process share the process address space.
We have various synchronization mechanisms like semaphore, mutex, spinlock, critical sections etc . used for thread syncronization.

I want to know in which part of the process address space are these synchronization variables stored?
How one thread gets access to the syncronization variable declared in another thread? Pls correct me if this question is improper.

推荐答案

Ah,地址空间.属于您的地址空间只有一个.您的所有内容都生活在该地址空间中.

现在,可以查看地址空间,就像将其划分为不同的用途一样.

代码,执行路径,通常是只读的.一些代码来自您的可执行文件,一些来自DLL,一些来自共享库,等等,但这都只是位于您地址空间中的代码.

常量数据(字符串,const等)通常也是只读的.

全局内存是您的模块范围变量"所在的地方,它在program/cpp文件的范围内声明,并且对所有功能可见.可能包含或不包含"extern"声明.

堆/池,读/写内存,您可以将其分配出去(新的/删除的和/或malloc/免费的).基本上,您所有的动态素材.

堆栈(每个线程一个),局部变量存放在其中.

通常,所有这些内容都可以在应用程序中的所有线程中看到,并且有些可以更改角色",例如,某些最著名的病毒进入方法是说服一些编写得不好的代码来执行数据,就像代码,但我离题了.

您可以直接通过名称访问事物,也可以在函数之间传递指针或结构/对象作为参数.

您唯一需要注意的是将地址/指针传递给位于函数/线程的堆栈空间中的变量到另一个线程.尽管指针最有可能保持有效,但不应信任内容.所以不要那样.

哦,唯一的唯一的事情"就是不要传递您最终删除"或释放"的事物的指针/地址,因为同样,该指针很可能是有效的,但数据不再受信任.

哦,我敢肯定还有其他唯一的东西",所以要小心.

---------------------------
编辑只是为了消除一些潜在的混乱
---------------------------

现在有两种方法可以解释您的问题
Ah, address space. There is one and only one address space that belongs to you. All things yours live in that address space.

Now, the address space can be looked at as if it is partitioned into different uses.

Code, the execution path, generally read-only. Some code comes from your executable, some from DLLs, some from shared libraries, etc but it''s all just code sitting in your address space.

Constant data (strings, const, things like that), also generally read-only.

Global Memory, a place where your "module wide variables" go, things declared in the scope of the program / cpp file and are visible to all functions. May or may not include the "extern" declaration.

Heap / Pool, read/write memory you can allocate things out of (new/delete and/or malloc/free). Basically, all your dynamic stuff.

Stack (one per thread) where local variables go.

In general, all of this can be seen by all threads in your application and some can change "roles", for example, some of the best known virus entry methods is to convince some poorly written piece of code to execute data as if it were code, but I digress.

You can either access things directly by name or pass pointers or structures / objects around as parameters between functions.

The only thing you have to be careful of is passing the address / pointer to a variable that lives in the stack space of a function / thread to another thread. While the pointer will most likely remain valid, the contents should not be trusted. So don''t do that.

Oh, the only other "only thing" is to not pass pointers / addresses of things you eventually "delete" or "free" since again, the pointer is most likely valid but the data cannot be trusted anymore.

Oh, and I''m sure there are other "only things" out there so be careful.

---------------------------
Edit just to remove some potential confusion
---------------------------

Now there are two ways to interpret your question
Quote:

我想知道这些同步变量存储在进程地址空间的哪一部分? br/>

I want to know in which part of the process address space are these synchronization variables stored?


1)您创建的对象,即"HANDLE",用于标识关键部分的信号/事件或对象,答案将是在您的地址空间中",以便您可以访问它们,将它们传递给其他对象,等等. br/>
2)代表您可以等待的事物的对象,侍者的队列等.这些是位于内核内存空间中的内核对象",由您从HANDLE返回的HANDLE映射"到create语句,并提供内核为您提供"Wait","Signal"等功能所需的所有内容.您只能通过定义的API并通过提供给您的"HANDLE"间接引用这些对象. >
这种分离适用于几乎所有操作系统,无论是Windows还是诸如提供POSIX线程同步功能之类的Unix.


1) The object you create, that is the "HANDLE" that identifies the semaphore / event or object for the critical section, and the answer would be "in your address space" so you can access them, pass them around, etc.

2) The object that represents the thing you can wait on, the queue of waiters, etc. Those are "kernel objects" that live in the kernel''s memory space, are "mapped to" by the HANDLE you get back from the create statement, and provide all the stuff the kernel needs to provide you the functionality of "Wait", "Signal", etc. You only indirectly reference these objects through the API defined and with the "HANDLE" given to you.

This separation is true for just about any OS, be it Windows or some unix like offering the POSIX thread synchronization features.


在问题中发现的这部分短语是不正确:"...在另一个线程中声明".同步原语的变量永远不会在线程中声明;只有堆栈和保留的CPU上下文属于线程;所有其他变量在线程之间共享.访问对象和线程是完全正交的类别.唯一的问题是不同线程对对象的并行访问.

需要再澄清一点:严格来说,共享地址空间不是进程的地址空间,而是应用程序域的地址空间.同一进程的每两个应用程序域以及两个不同的进程都相互隔离.

—SA
This part of phrase found in the question is not correct: "...declared in another thread". A variable of a synchronization primitive is never declared in a thread; only the stacks and the preserved CPU context belong to a thread; all other variables are shared between threads. Access to an object and threads are completely orthogonal categories. The only problem is the parallel access to the object by different threads.

One more clarification: strictly speaking, shared address space is not the address space of a process, but of an Application Domains. Each two Application Domains of the same process are isolated from each other as well as two different processes.

—SA


对于VC ++/MFC中的多线程,您需要使用AFXBeginThread函数.谁的第一个参数是指向全局函数的指针,原型是,
UINT FunctionName(LPVOID参数);
第二个参数是通过参数发送的价值.

您可以发送任何要通过参数访问的数据.
For multithreading in VC++/MFC, you need to use AFXBeginThread function. Whose first pararmeter is pointer to global function, prototype is,
UINT FunctionName(LPVOID param);
And second paramater is value to send through param.

You can send any data you want to access through param.


这篇关于同步变量存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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