多个线程往访问同一个类中的变量会产生冲突吗

多个线程去访问同一个类中的变量会产生冲突吗
RT,将这个类实例成一个对象,这个变量有两种类型:BOOL和int,一个线程读一个线程写,读的同时可能有写的情况发生,这样会有问题吗?没办法呀,我现在也想不出好的方法来平衡这种。实际上的情况是,一个线程在不断地采集PLC数据(超大量,包括标志位),而我另外一个线程检测那个标志位,为1时则存储这些采集到数据,我做成一个类,类中包含这些所有成员,这两个线程共享这个类的实例对象,同时操作,长时间这样会有问题吗?
线程 对象 实例 互斥

------解决方案--------------------
原子操作不需要加同步,例如只是一个32位的变量,不会冲突。
------解决方案--------------------
会有冲突的。

看来LZ对数据并发冲突的处理没有太多经验,那么就用简单的MFC代码来解决(不考虑吞吐量问题)

MFC代码

定义
CCriticalSection m_csSyncObj;

线程1
{
  CSingleLock singleLock(&m_csSyncObj, TRUE)
  xxx;
  xxx;
}

线程2
{
  CSingleLock singleLock(&m_csSyncObj, TRUE)
  yyy;
  yyy;
}

------解决方案--------------------
“原子操作不需要加同步,例如只是一个32位的变量” ==> 这些主题比较深入。
感觉 对LZ来说 先用简单代码保证不冲突,然后再去研究 原子操作 volatile 等东东。
------解决方案--------------------
LZ还需要考虑:线程2还没有读取,线程1又要写新数据这种情况。
------解决方案--------------------
两个线程的一读一写不是原子操作, 必须保证互斥, 不过可以做成lock-free的 ...

------解决方案--------------------
1)两个线程是可以共享一个对象实例的,但是需要使用锁机制,即无论是读还是写,在使用前获取lock,使用完free lock,如果读写操作对性能消耗不大,可以考虑自旋锁
2)如果读或者写操作,有任何一个比较耗时,则需要考虑是否系统有软件狗机制,在这种情况下,需要考虑初始化软件狗,避免进程复位。
------解决方案--------------------

------解决方案--------------------
不做任何互斥处理, 肯定是会有冲突的.

如果只是一个基本类型的变量, 那完全可以用interlockedXXXX系列函数

以下来自MSDN

The InterlockedIncrement and InterlockedDecrement functions combine the steps involved in incrementing or decrementing a variable into an atomic operation. This feature is useful in a multitasking operating system, in which the system can interrupt one thread's execution to grant a slice of processor time to another thread. Without such synchronization, two threads could read the same value, increment it by 1, and store the new value for a total increase of 1 instead of 2. The interlocked variable-access functions protect against this kind of error.

The InterlockedExchange and InterlockedExchangePointer functions atomically exchange the values of the specified variables. The InterlockedExchangeAdd function combines two operations: adding two variables together and storing the result in one of the variables.

The InterlockedCompareExchange, InterlockedCompare64Exchange128, and InterlockedCompareExchangePointer functions combine two operations: comparing two values and storing a third value in one of the variables, based on the outcome of the comparison.

The InterlockedAnd, InterlockedOr, and InterlockedXor functions atomically perform AND, OR, and XOR operations, respectively.

There are functions that are specifically designed to perform interlocked variable access on 64-bit memory values and addresses, and are optimized for use on 64-bit Windows. Each of these functions contains "64" in the name; for example, InterlockedDecrement64 and InterlockedCompareExchangeAcquire64. 

Most of the interlocked functions provide full memory barriers on all Windows platforms. There are also functions that combine the basic interlocked variable access operations with the acquire and release memory access semantics supported by certain processors. Each of these functions contains the word "Acquire" or "Release" in their names; for example, InterlockedDecrementAcquire and InterlockedDecrementRelease. Acquire memory semantics specify that the memory operation being performed by the current thread will be visible before any other memory operations are attempted. Release memory semantics specify that the memory operation being performed by the current thread will be visible after all other memory operations have been completed. These semantics allows you to force memory operations to be performed in a specific order. Use acquire semantics when entering a protected region and release semantics when leaving it.