我知道当前使用Executors而不是ThreadGroup的做法:
然而,ThreadGroup本身的固有缺陷是什么(我听过那个类的含糊不清的批评)?
谢谢你的回答.
PS.这似乎没有回答这个问题.
请考虑以下示例代码:
class MyClass
{
public long x;
public void DoWork()
{
switch (x)
{
case 0xFF00000000L:
// do whatever...
break;
case 0xFFL:
// do whatever...
break;
default:
//notify that something going wrong
throw new Exception();
}
}
}
Run Code Online (Sandbox Code Playgroud)
忘掉片段的无用性:我的疑问是关于switch声明的行为.
假设该x字段只能有两个值:0xFF00000000L或0xFFL.上面的开关不应该属于"默认"选项.
现在想象一个线程正在执行"x"等于0xFFL的开关,因此第一个条件将不匹配.同时,另一个线程将"x"变量修改为0xFF00000000L.我们知道64位操作不是原子操作,因此变量首先将较低的dword置零,然后将较低的dword置零(反之亦然).
如果在"x"为零时(即在新的分配期间)完成开关中的第二个条件,我们是否会陷入不希望的"默认"情况?
Qt 文档说明可以将两个信号连接在一起:
甚至可以将信号直接连接到另一个信号.
我试过了:
connect(x, SIGNAL(S()), y, SIGNAL(func()));
Run Code Online (Sandbox Code Playgroud)
它的工作原理如上所述,但Qt文档仍在继续:
(每当发射第一个信号时,这将立即发出第二个信号.)
这是否意味着QueuedConnection无法正常工作?我可以跨线程连接两个信号吗?
我之所以这样问是因为我通过避免这种情况解决了应用程序中的一类崩溃问题,但我不确定这是否与将信号连接在一起有关.
一次又一次我发现自己必须编写BindingList和ObservableCollection的线程安全版本,因为当绑定到UI时,不能从多个线程更改这些控件.我想要了解的是为什么会出现这种情况 - 是设计错误还是故意这种行为?
我知道,在.net所有32位类型(如int,bool等)是线程安全的.也就是说,不会有部分写入(根据规范).
但是,同样适用于int?(可空的int)吗?
有人说python字典是线程安全的.这是否意味着我可以或不可以在迭代时修改字典中的项目?
所以这就是问题的关键:Foo.Bar可以返回null吗?为了澄清,'_bar'在被评估为非null并且在返回值之前可以设置为null吗?
public class Foo
{
Object _bar;
public Object Bar
{
get { return _bar ?? new Object(); }
set { _bar = value; }
}
}
Run Code Online (Sandbox Code Playgroud)
我知道使用以下get方法不安全,并且可以返回null值:
get { return _bar != null ? _bar : new Object(); }
Run Code Online (Sandbox Code Playgroud)
更新:
另一种看待同一问题的方法,这个例子可能更清楚:
public static T GetValue<T>(ref T value) where T : class, new()
{
return value ?? new T();
}
Run Code Online (Sandbox Code Playgroud)
并再次询问GetValue(...)是否会返回null?根据你的定义,这可能是也可能不是线程安全的...我猜正确的问题陈述是询问它是否是一个关于价值的原子操作...... David Yaw已经通过说上面的函数等效来定义问题了以下内容:
public static T GetValue<T>(ref T value) where T : class, new()
{
T result = value;
if …Run Code Online (Sandbox Code Playgroud) 我对C#很新,所以请耐心等待.我对线程安全感有点困惑.什么时候线程安全什么东西什么时候不安全?
正在阅读从现场总是线程安全的(刚刚从一些被初始化之前读)?
//EXAMPLE
RSACryptoServiceProvider rsa = new RSACrytoServiceProvider();
rsa.FromXmlString(xmlString);
//Is this thread safe if xml String is predifined
//and this code can be called from multiple threads?
Run Code Online (Sandbox Code Playgroud)
被访问的数组或列表的对象始终是线程安全的(如果你使用一个for循环枚举)?
//EXAMPLE (a is local to thread, array and list are global)
int a = 0;
for(int i=0; i<10; i++)
{
a += array[i];
a -= list.ElementAt(i);
}
Run Code Online (Sandbox Code Playgroud)
被列举始终/曾经线程安全的?
//EXAMPLE
foreach(Object o in list)
{
//do something with o
}
Run Code Online (Sandbox Code Playgroud)
可以写入和读出一个特定的领域不断导致损坏的读取(场的一半被改变,一半仍是不变的)?
感谢您的所有答案和时间.
编辑:我的意思是如果所有线程只读取和使用(不写或更改)对象.(除了最后一个问题,显然我的意思是线程既读又写).因为我不知道普通访问或枚举是否是线程安全的.
我正在使用全局变量实现线程间通信.
//global var
volatile bool is_true = true;
//thread 1
void thread_1()
{
while(1){
int rint = rand() % 10;
if(is_true) {
cout << "thread_1: "<< rint <<endl; //thread_1 prints some stuff
if(rint == 3)
is_true = false; //here, tells thread_2 to start printing stuff
}
}
}
//thread 2
void thread_2()
{
while(1){
int rint = rand() % 10;
if(! is_true) { //if is_true == false
cout << "thread_1: "<< rint <<endl; //thread_2 prints some stuff
if(rint == 7) …Run Code Online (Sandbox Code Playgroud) 多个来源声称ActiveScaffold不是线程安全的:
从那些我收集的控制器级配置更改和授权不是线程安全的.如果应用程序不使用ActiveScaffold的那些组件,是否可以安全地考虑应用程序线程安全?ActiveScaffold中是否有任何其他功能不是线程安全的?
thread-safety ×10
c# ×5
.net ×3
arrays ×1
atomicity ×1
bindinglist ×1
c++ ×1
coalesce ×1
concurrency ×1
enumeration ×1
int ×1
java ×1
python ×1
qt ×1
signals ×1
slot ×1
string ×1
visual-c++ ×1