linux 多线程下简单数据类型的存取与原子变量
我们知道,count++这种操作不是原子的。一个自加操作,本质是分成三步的:
- 从缓存取到寄存器
- 在寄存器加1
- 存入缓存。
由于时序的因素,多个线程操作同一个全局变量,会出现问题。这也是并发编程的难点。在目前多核条件下,这种困境会越来越彰显出来。
最简单的处理办法就是加锁保护,最初的解决方案。看下面的代码:
pthread_mutex_t count_lock = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_lock(&count_lock);
global_int++;
pthread_mutex_unlock(&count_lock);
对于简单类型的数据类型,比如int,long,在实践上,原子变量是在多线程情况下最好解决方案。
原子变量,其实关键点就是只能有一个线程读或写,其他线程不能打断这个过程。
原子变量是如何工作的
其实很简单,Intel x86和x86_64处理器架构,允许在做内存读取操作时,发出指令锁定FSB。FSB(Front Serial Bus)是CPU访问RAM的数据通道。也就是说,锁定了FSB,将会阻止任何其他的线程,读取该部分的内存。这就是原子变量的作用过程。
原子变量被广泛使用在内核中,gcc 4.1.2之前可能由于一些原因没有在用户模式的fork上使用。
原子变量的大小设置
当存取 1,2,4,8 byte的整型数据时,可以锁定FSB。所以在gcc中也就允许你对int,long,long long的数据类型执行原子操作。
操作函数
总共有12个函数,对原子变量进行操作,有:加,减,逻辑 or and xor nand。每一个操作有两个函数,分别返回改变前的值和改变后的值。
函数:
type __sync_fetch_and_add (type *ptr, type value);
type __sync_fetch_and_sub (type *ptr, type value);
type __sync_fetch_and_or (type *ptr, type value);
type __sync_fetch_and_and (type *ptr, type value);
type __sync_fetch_and_xor (type *ptr, type value);
type __sync_fetch_and_nand (type *ptr, type value);
这些函数返回变量改变之前的值
type __sync_add_and_fetch (type *ptr, type value);
type __sync_sub_and_fetch (type *ptr, type value);
type __sync_or_and_fetch (type *ptr, type value);
type __sync_and_and_fetch (type *ptr, type value);
type __sync_xor_and_fetch (type *ptr, type value);
type __sync_nand_and_fetch (type *ptr, type value);
这些函数返回变量改变之后的值
返回值type可以是下列数据类型:
- int
- unsigned int
- long
- unsigned long
- long long
- unsigned long long
上述函数是build-in函数,所以可以不include任何文件就可以使用。
是时候看一个实例了
这个程序使用了线程,线程数是你计算机的CPU数目。然后每个CPU运行一个线程,最后每个线程将一个全局整型变量加1 million次。
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
#include <sched.h>
#include <linux/unistd.h>
#include <sys/syscall.h>
#include <errno.h>
#define INC_TO 1000000 // one million...
int global_int = 0;
pid_t gettid( void )
{
return syscall( __NR_gettid );
}
void *thread_routine( void *arg )
{
int i;
int proc_num = (int)(long)arg;
cpu_set_t set;
CPU_ZERO( &set );
CPU_SET( proc_num, &set );
if (sched_setaffinity( gettid(), sizeof( cpu_set_t ), &set ))
{
perror( "sched_setaffinity" );
return NULL;
}
for (i = 0; i < INC_TO; i++)
{
// global_int++;
__sync_fetch_and_add( &global_int, 1 );
}
return NULL;
}
int main()
{
int procs = 0;
int i;
pthread_t *thrs;
// Getting number of CPUs
procs = (int)sysconf( _SC_NPROCESSORS_ONLN );
if (procs < 0)
{
perror( "sysconf" );
return -1;
}
thrs = malloc( sizeof( pthread_t ) * procs );
if (thrs == NULL)
{
perror( "malloc" );
return -1;
}
printf( "Starting %d threads...\n", procs );
for (i = 0; i < procs; i++)
{
if (pthread_create( &thrs[i], NULL, thread_routine,
(void *)(long)i ))
{
perror( "pthread_create" );
procs = i;
break;
}
}
for (i = 0; i < procs; i++)
pthread_join( thrs[i], NULL );
free( thrs );
printf( "After doing all the math, global_int value is: %d\n",
global_int );
printf( "Expected value is: %d\n", INC_TO * procs );
return 0;
}
原子操作的方式比线程加锁的速度要快很多。
参照
原文 [http://www.alexonlinux.com/multithreaded-simple-data-type-access-and-atomic-variables]