什么是多线程安全 [英] What is multithread safety

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

问题描述

我试图找出多线程的确切含义

安全代码/功能等。我无法在
$ b $上找到相关信息b web并希望获得有关此主题的任何有用链接/信息。


谢谢。


此致,

Shalini。

Hi, I was trying to find out what exactly is meant by multi-thread
safe code/functions etc. I couldn''t find relevant information on the
web and would appreciate any useful links/information on this topic.

Thanks.

Sincerely,
Shalini.

推荐答案

文章< 28 ********************** ****@posting.google.com>,

Shalini Joshi< sh ******* @ yahoo.com>写道:
In article <28**************************@posting.google.com >,
Shalini Joshi <sh*******@yahoo.com> wrote:
我试图找出多线程
安全代码/功能等的确切含义。我无法在
上找到相关信息。网页,并希望有关此主题的任何有用的链接/信息。
Hi, I was trying to find out what exactly is meant by multi-thread
safe code/functions etc. I couldn''t find relevant information on the
web and would appreciate any useful links/information on this topic.




这是指如果多个线程正在运行仍然正确的代码

同时。

这超出了C语言的范围,因此不适合

for comp.lang.c。


谷歌搜索线程安全会发现一些明智的命中(你有多难以实际尝试在网上找到相关信息?)。


comp.programming.threads可能是提问的最佳地点。

dave


-

戴夫Vandervies dj******@csclub.uwaterloo.ca

不必要的演员阵容是邪恶的工具(和C ++程序ammers,但我

重复一遍)。

- 在comp.lang.c中的理查德博斯



This refers to code that is still correct if multiple threads are running
it concurrently.
This is beyond the scope of the C language and therefore not appropriate
for comp.lang.c .

Googling for `thread safety'' turns up a few sensible hits (how hard did
you actually try to `find relevant information on the web''?).

comp.programming.threads is probably the best place to ask questions.
dave

--
Dave Vandervies dj******@csclub.uwaterloo.ca
Unnecessary casts are the tool of evil (and of C++ programmers, but I
repeat myself).
--Richard Bos in comp.lang.c


On 2004年10月27日星期三07:22:17 -0700,Shalini Joshi写道:
On Wed, 27 Oct 2004 07:22:17 -0700, Shalini Joshi wrote:
我试图找出多线程究竟是什么意思
安全代码/功能等我无法在
网站上找到相关信息,并希望了解有关此主题的任何有用链接/信息。
Hi, I was trying to find out what exactly is meant by multi-thread
safe code/functions etc. I couldn''t find relevant information on the
web and would appreciate any useful links/information on this topic.




从根本上说,这里只是偏离主题,因为C没有线程支持
。但是,这个想法很简单:问问自己,如果两个不同的线程以不可预测的方式访问或修改同一块

数据,将会发生什么?例如:


/ * file.c * /

static char workbuff [32];


void myfunc(char * s)

{

/ *用workbuff做一些事情* /

strcpy(s,workbuff);

}

如果只有一个线程,myfunc将正常运行。但是假设

有两个线程,一半通过做某事,第二个
线程调用myfunc。第二个线程做某事它会覆盖工作日中的任何内容,所以当控件返回到第一个函数时,

缓冲区不应该是它应该是什么。因此该函数不是线程安全的。


为了使线程安全,你可以做很多事情。在飞行中分配工作量

。使用线程本地存储。使用自动缓冲区。创建一个

锁定,这样两个进程就永远不会同时修改缓冲区。


你不能做的是允许多个线程在没有某种形式保护的情况下修改_same_

变量。



Fundamentally, it''s simply off-topic here, as C doesn''t have threading
support. However, the idea is simple enough: ask yourself what would
happen if two different threads accessed or modified the same piece of
data in an unpredictable manner. As an example:

/* file.c */
static char workbuff[32];

void myfunc( char *s )
{
/* do something with workbuff */
strcpy( s, workbuff );
}
If there''s only one thread, myfunc will operate correctly. But suppose
there are two threads, and half way through "do something", the second
thread calls myfunc. The second thread "does something" which overwrites
whatever is in workbuff, so when control goes back to the first function,
the buffer is not what it should be. Thus the function is not thread safe.

To make it thread safe, you could do many things. Allocate workbuff on
the fly. Use thread-local storage. Use an automatic buffer. Create a
lock so that two processes can never modify the buffer at the same time.

What you can''t do is allow more than one thread to modify the _same_
variable without some form of protection.




" Shalini Joshi" < SH ******* @ yahoo.com>写了

"Shalini Joshi" <sh*******@yahoo.com> wrote
我试图找出多线程
安全代码/功能等的确切含义。我无法在
网页上找到相关信息并且非常感谢关于这个主题的任何有用链接/信息。
Hi, I was trying to find out what exactly is meant by multi-thread
safe code/functions etc. I couldn''t find relevant information on the
web and would appreciate any useful links/information on this topic.



对于所有意图和目的,C中的线程安全意味着避免全局和

静态变量。 br />


To all intents and purposes, thread safety in C means avoiding global and
static variables.


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

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