C语言中的线程和线程安全 [英] Threading and Thread Safety in C

查看:947
本文介绍了C语言中的线程和线程安全的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当需要在几个线程进程之间共享一组通用的全局数据时,我通常使用线程令牌来保护共享资源:

When there is a common set of global data that needs to be shared among several threaded processes, I typically have used a thread token to protect the shared resource:

编辑-15/7/22 (以合并

Edit - 7/22/15 (to incorporate atomics as a viable option, per Jens comments)

我的[第一个]问题是 ,在C语言中,如果我编写例程以确保每个线程访问一个线程,而一个线程仅访问一个元素,数组:

My [First] question is, in C, if I write my routines in such a way as to guarantee each thread accesses one, and only one element of an array:

是否有理由认为异步和同时访问同一未受保护的数组的不同索引(如图所示)会成为问题?

Is there any reason to think that asynchronous and simultaneous access to different indices of the same unprotected array (as shown in diagram) would be a problem?

第二个问题: 鉴于一个可以通过以下方式访问的对象 一个原子实体,即使存在异步中断(

Second question: Given that an object that can be accessed as an atomic entity, even in the presence of asynchronous interrupts ( C99 - 7.14 Signal handling ) would using atomics be an effective method for thread protection for an otherwise unprotected variable?

编辑 (澄清说明以解决目前评论中的问题):
-此应用程序的详细信息:
-目标操作系统:Windows 7/8/10
-编译器:符合C99(不能使用C11,其中包括_

Edit (Clarifications to address questions in comments to this point):
- Specifics for this application:
- Target OS: Windows 7/8/10
- Compiler : C99 compliant (cannot use C11, which include the _Atomic() type specifier )
- H/W : Intel i7 family

推荐答案

这(看起来像某种C标准) http://www.open-std.org/jtc1 /sc22/wg14/www/docs/n1570.pdf sayeth:

This (which looks like a C standard of some sort) http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf sayeth:

注意1两个执行线程可以更新和访问单独的内存 个位置而不会互相干扰

NOTE 1 Two threads of execution can update and access separate memory locations without interfering with each other

注意13编译器转换,这些转换将分配引入到 潜在的共享内存位置,不会被 该标准通常排除抽象机器,因为这样 一个工作分配可能会被另一个线程覆盖另一个工作分配 在抽象机器执行不会有的情况下 遇到了数据竞赛.这包括数据成员的实现 覆盖单独内存中相邻成员的分配 位置.通常,我们还排除了原子负载中的重新排序 有问题的原子可能会混叠的情况,因为这可能 违反了可见序列"规则.

NOTE 13 Compiler transformations that introduce assignments to a potentially shared memory location that would not be modified by the abstract machine are generally precluded by this standard, since such an assignment might overwrite another assignment by a different thread in cases in which an abstract machine execution would not have encountered a data race. This includes implementations of data member assignment that overwrite adjacent members in separate memory locations. We also generally preclude reordering of atomic loads in cases in which the atomics in question may alias, since this may violate the "visible sequence" rules.

以我的理解,这将避免quamrana的担忧,并保证您在没有数据竞争的情况下,对未保护的写到单独的内存位置的写操作永远不会导致未定义的行为.

The way I understand it, this would preclude quamrana's concerns and guarantee you that unprotected writes to separate memory locations should never result in undefined behavior if there is no data race.

这篇关于C语言中的线程和线程安全的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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