volatile

Why is (or isn't) setting fields in a constructor thread-safe?

我只是一个虾纸丫 提交于 2019-12-03 05:45:36
Let's say you have a simple class like this: class MyClass { private readonly int a; private int b; public MyClass(int a, int b) { this.a = a; this.b = b; } public int A { get { return a; } } public int B { get { return b; } } } I could use this class in a multi-threaded manner: MyClass value = null; Task.Run(() => { while (true) { value = new MyClass(1, 1); Thread.Sleep(10); } }); while (true) { MyClass result = value; if (result != null && (result.A != 1 || result.B != 1)) { throw new Exception(); } Thread.Sleep(10); } My question is: will I ever see this (or other similar multi-threaded

ConcurrentHashMap怎么保证安全的

最后都变了- 提交于 2019-12-03 05:31:57
HashMap是一个线程不安全的容器,当容量大于 总量*负载因子 发生扩容时可能会出现环形链表从而导致死循环 扩容就是rehash,这个会重新将原数组的内容重新hash到新的扩容数组中,在多线程的环境下,存在同时其他的元素也在进行put操作,如果hash值相同,可能出现同时在同一数组下用链表表示 因此引进了线程安全的容器 ConcurrentHashMap ConcurrentHashMap 在JDK1.7 和 JDK1.8中的实现有所不同 JDK1.7中的实现 先来看看1.7中数据结构实现的图示 由图中可以看出ConcurrentHashMap是由 Segment数组 , HashEntry数组 组成的。这里和 HashMap 一样,都是 数组+链表 的形式 ConcurrentHashMap采用了分段锁的技术,其中一个 Segement 就是一个Lock,其继承自 ReentrantLock ,这样当一个线程占用了锁访问一个 Segment 时,不会影响到其它的 Segment Segment 数组的意义就是将一个大的table分成多个小的table来进行加锁,而一个Segment存储的是HashEntry数组+链表,这和HashMap的数据结构一致 Segment的大小最多65536个,没有指定concurrencyLevel元素初始化

How do I declare an array created using malloc to be volatile in c++

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-03 05:18:11
I presume that the following will give me 10 volatile ints volatile int foo[10]; However, I don't think the following will do the same thing. volatile int* foo; foo = malloc(sizeof(int)*10); Please correct me if I am wrong about this and how I can have a volatile array of items using malloc. Thanks. int volatile * foo; read from right to left "foo is a pointer to a volatile int" so whatever int you access through foo, the int will be volatile. P.S. int * volatile foo; // "foo is a volatile pointer to an int" == volatile int * foo; // foo is a pointer to an int, volatile Meaning foo is volatile

Thread Synchronisation 101

做~自己de王妃 提交于 2019-12-03 05:02:57
问题 Previously I've written some very simple multithreaded code, and I've always been aware that at any time there could be a context switch right in the middle of what I'm doing, so I've always guarded access the shared variables through a CCriticalSection class that enters the critical section on construction and leaves it on destruction. I know this is fairly aggressive and I enter and leave critical sections quite frequently and sometimes egregiously (e.g. at the start of a function when I

How does “Compare And Set” in AtomicInteger works

非 Y 不嫁゛ 提交于 2019-12-03 05:01:07
问题 AtomicInteger works with two concepts : CAS and volatile variable. Using volatile variable insures that the current value will be visible to all threads and it will not be cached. But I am confused over CAS(compare AND set) concept which is explained below: public final int getAndIncrement() { for (;;) { int current = get(); int next = current + 1; if (compareAndSet(current, next)) return current; } } My question is that what if(compareAndSet(current, next) returns false ? Will the value not

How do I track down the cause of a StackOverflowException in .NET?

☆樱花仙子☆ 提交于 2019-12-03 04:46:01
问题 I get a StackOverflowException when I run the following code: private void MyButton_Click(object sender, EventArgs e) { MyButton_Click_Aux(); } private static volatile int reportCount; private static void MyButton_Click_Aux() { try { /*remove because stack overflows without*/ } finally { var myLogData = new ArrayList(); myLogData.Add(reportCount); myLogData.Add("method MyButtonClickAux"); Log(myLogData); } } private static void Log(object logData) { // my log code is not matter } What could

高并发编程基础知识

这一生的挚爱 提交于 2019-12-03 04:41:02
4)线程和进程的区别:(必考) 答: 进程是一个 “执行中的程序”,是系统进行资源分配和调度的一个独立单位; 线程是进程的一个实体,一个进程中拥有多个线程,线程之间共享地址空间和其它资源(所以通信和同步等操作线程比进程更加容易); 线程上下文的切换比进程上下文切换要快很多。 (1)进程切换时,涉及到当前进程的 CPU 环境的保存和新被调度运行进程的 CPU 环境的设置。 (2)线程切换仅需要保存和设置少量的寄存器内容,不涉及存储管理方面的操作。 面试官:进程间如何通讯?线程间如何通讯? 答:进程间通讯依靠 IPC 资源,例如管道(pipes)、套接字(sockets)等; 线程间通讯依靠 JVM 提供的 API,例如 wait()、notify()、notifyAll() 等方法,线程间还可以通过共享的主内存来进行值的传递。 什么是阻塞(Blocking)和非阻塞(Non-Blocking)? 答:阻塞和非阻塞通常用来形容多线程间的相互影响。比如一个线程占用了临界区资源,那么其他所有需要这个而资源的线程就必须在这个临界区中进行等待。等待会导致线程挂起,这种情况就是阻塞。此时,如果占用资源的线程一直不愿意释放资源,那么其他所有阻塞在这个临界区上的线程都不能工作。 非阻塞的意思与之相反,它强调没有一个线程可以妨碍其他线程执行。所有的线程都会尝试不断前向执行。 临界区是什么? 答

C# volatile variable: Memory fences VS. caching

£可爱£侵袭症+ 提交于 2019-12-03 04:33:32
So I researched the topic for quite some time now, and I think I understand the most important concepts like the release and acquire memory fences . However, I haven't found a satisfactory explanation for the relation between volatile and the caching of the main memory. So, I understand that every read and write to/from a volatile field enforces strict ordering of the read as well as the write operations that precede and follow it (read-acquire and write-release). But that only guarantees the ordering of the operations. It doesn't say anything about the time these changes are visible to other

Do I need volatile for variables of reference types, too?

断了今生、忘了曾经 提交于 2019-12-03 03:42:31
问题 We often use volatile to ensure that a condition variable can be visible to every Thread. I see the volatile fields are all primitive type in code so far. Does object field has this problem? For example: class a { public String str; public List list; } If there are some threads which will access str and list, must I add 'volatile'? I guess each access to Object will get directly from Heap , and the Object will not be cached like primitive type. Is that right? 回答1: You have to distinguish

Could the JIT collapse two volatile reads as one in certain expressions?

◇◆丶佛笑我妖孽 提交于 2019-12-03 03:39:43
问题 Suppose we have a volatile int a . One thread does while (true) { a = 1; a = 0; } and another thread does while (true) { System.out.println(a+a); } Now, would it be illegal for a JIT compiler to emit assembly corresponding to 2*a instead of a+a ? On one hand the very purpose of a volatile read is that it should always be fresh from memory. On the other hand, there's no synchronization point between the two reads, so I can't see that it would be illegal to treat a+a atomically, in which case I