C-为什么将线程随机加载数据



我是新来的线程,我正在尝试创建一个程序,其中四个线程使用来自全局数组的值进行一些并行计算。但是,线程没有按顺序加载数据的问题。

#define QUARTER 64
#define RANGE_STEP 256
struct thread_data
{
  unsigned          start;
  unsigned          stop;
  __m256*           re_fc;
  __m256*           im_fc;
};
#define         NUM_THREADS                 4
struct          thread_data                 thread_data_array[NUM_THREADS];
void *routine(void *thread_info)
{
  int n,t;
  unsigned t_start,t_stop;
  __m256 *Re_fac    , *Im_fac;
  struct thread_data *mydata;
  mydata = (struct thread_data*) thread_info;
  t_start   = mydata->start;
  t_stop    = mydata->stop;
  Re_fac        = mydata->re_fc;
  Im_fac        = mydata->im_fc;
  t = t_start;
  for (n = t_start; n < t_stop; n += 8)
  {
    // computations
    RE_m256_fac = Re_fac[t];
    IM_m256_fac = Im_fac[t]; 
    // computations
    t++;
  }
  pthread_exit(NULL);
}
int main()
{
  unsigned t,i=0;
  for(t=0;t<RANGE_STEP;t+=QUARTER)
  {
    thread_data_array[i].start      = t;
    thread_data_array[i].stop       = t+QUARTER;
    thread_data_array[i].re_fc      = RE_factors;
    thread_data_array[i].im_fc      = IM_factors;
    pthread_create(&threads[i],NULL,routine,(void *)&thread_data_array[i]);
    i++;
  }
  for(i=0; i<NUM_THREADS; i++)
  {
     int rc = pthread_join(threads[i], NULL);
     if (rc)
     {
         fprintf(stderr, "failed to join thread #%u - %sn",i, strerror(rc));
     }
  }
}

我正在谈论的问题是在for()循环内部的线程的例程中,完全使用这两个加载指令RE_m256_fac = Re_fac[t];IM_m256_fac = Im_fac[t];加载数据不正确...我认为索引t是局部变量,因此不需要同步,还是我错了?

在挖掘后,它变成了,因为我从全局共享数组中读取了我必须使用Mutex机制来防止相互排斥:

void *routine(void *thread_info)
{
  int n,t;
  unsigned t_start,t_stop;
 __m256 *Re_fac    , *Im_fac;
 struct thread_data *mydata;
 mydata = (struct thread_data*) thread_info;
 t_start   = mydata->start;
 t_stop    = mydata->stop;
 Re_fac        = mydata->re_fc;
 Im_fac        = mydata->im_fc;
 t = t_start;
 for (n = t_start; n < t_stop; n += 8)
 {
   pthread_mutex_lock(&mutex);
   // computations
   RE_m256_fac = Re_fac[t];
   IM_m256_fac = Im_fac[t]; 
   // computations
   pthread_mutex_unlock(&mutex);
   t++;
 }
 pthread_exit(NULL);
}

从那以后,我可以看到线程从共享数组正确加载值。

最新更新