C ++代码中的线程安全 [英] Thread Safety in C++ code

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

问题描述






我正在阅读这篇文章
http://blogs.msdn.com/oldnewthing/ar.../08/85901.aspx

这谈到C ++作用域静态初始化不是线程安全的,

目的!

其中一个解决方案是用于关键部分以避免竞争条件。

我的问题是,如果在任何程序中,关键部分本身是静态的,

则会出现线程安全问题,任何问题指针/想法怎么避免这个

条件?


谢谢!


Hi,

I was going through this article
http://blogs.msdn.com/oldnewthing/ar.../08/85901.aspx
This talks about "C++ scoped static initialization is not thread-safe, on
purpose!"
One of the resolutions is to use to critical section to avoid race condition.
My question is what if in any program, critical section itself is static,
then there will be thread safety issue, any pointers/ideas how avoid this
condition?

Thanks!

推荐答案

" SQL_Learner" < SQ ******** @ discussion.microsoft.com写信息

新闻:66 ******************** ************** @ microsof t.com ...
"SQL_Learner" <SQ********@discussions.microsoft.comwrote in message
news:66**********************************@microsof t.com...

我正在阅读这篇文章
http://blogs.msdn.com/oldnewthing/ ar ... / 08 / 85901.aspx

这谈到C ++作用域静态初始化不是线程安全的,用于
目的!

其中一个决议是用于关键部分以避免比赛

条件。

我的问题是如果在任何程序,关键部分本身是静态的,然后会出现线程安全问题,任何指针/想法如何避免这个

条件?
I was going through this article
http://blogs.msdn.com/oldnewthing/ar.../08/85901.aspx
This talks about "C++ scoped static initialization is not thread-safe, on
purpose!"
One of the resolutions is to use to critical section to avoid race
condition.
My question is what if in any program, critical section itself is static,
then there will be thread safety issue, any pointers/ideas how avoid this
condition?



为了在线程之间进行同步,必须在它们之间共享一个关键部分

。在开始任何后续线程之前初始化共享关键的

部分是明智的策略。然后没有问题。


问候,


In order to synchronize between threads, a single critical section must be
shared between them. It''s a wise strategy to initialize the shared critical
section before you start any subsequent threads. Then there is no issue.

Regards,
Will



为了在线程之间进行同步,必须在它们之间共享一个关键部分

。在开始任何后续线程之前初始化共享关键的

部分是明智的策略。然后没有问题。


问候,


In order to synchronize between threads, a single critical section must be
shared between them. It''s a wise strategy to initialize the shared critical
section before you start any subsequent threads. Then there is no issue.

Regards,
Will



嗨Will,


感谢您的回复。我对共享的关键

部分不太清楚。我的实际问题是我有一个代码块,它有一个关键的

部分,它被声明为静态,这看起来像一个可能的线程

安全问题因为这可能会造成2关键部分的副本。我是

寻找另一种方法来避免这种情况。


谢谢

Hi Will,

Thanks for your response. I am little unclear about the shared critical
section. My actual problem is I have a block of code which has a critical
section which is declared as static and this looks like a probable thread
safety issue as this might create 2 copies of critical sections. I am
looking for a alternative way to avoid this.

Thanks


" SQL_Learner" < SQ ******** @ discussion.microsoft.com写信息

news:83 ******************** ************** @ microsof t.com ...
"SQL_Learner" <SQ********@discussions.microsoft.comwrote in message
news:83**********************************@microsof t.com...

感谢您的回复。
Thanks for your response.



欢迎你。

You are welcome.


我对共享的关键部分我不太清楚。我的实际

问题是我有一段代码,其中有一个关键的

部分,声明为静态,这看起来像

a可能的线程安全问题,因为这可能会产生2份副本

的关键部分。我正在寻找另一种方法来避免这种情况。
I am little unclear about the shared critical section. My actual
problem is I have a block of code which has a critical
section which is declared as static and this looks like
a probable thread safety issue as this might create 2 copies
of critical sections. I am looking for a alternative way to avoid this.



你必须显示_little_代码。


问候,

Will

You''ll have to show a _little_ code.

Regards,
Will


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

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