多线程程序正在阻塞 [英] multi threaded program is blocking

查看:81
本文介绍了多线程程序正在阻塞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用C语言编写一个多线程程序,其中我使用4个线程对浮点数组进行一些计算.因此,我从仅创建4个线程开始,并使用一些参数来定义该线程将在数组的哪一部分上工作.到那时,程序运行正常.

Hi I'm trying to write a multi-threaded program in C where I use 4 threads to work on some computation on an array of floats. So, I began by creating just 4 threads and play with some arguments that define which part of the array the thread will work on. And at that point the program is working fine.

现在,我尝试仅使用加载和存储指令(256位Intel内部函数).然后,尽管线程例程似乎已完成其工作,但该程序从未完成.

And now, I tried to use only loading and storing instructions (256 bits Intel intrinsics). And then, the program never finish although it seems that the threads routines are finishing their work.

void *routine(void *thread_info)
{
   int n;
   unsigned t_start,t_stop;
   unsigned ind1, ind2, ind3;
   float *arr_in , *arr_out;
   struct thread_data *mydata;

   mydata = (struct thread_data*) thread_info;
   t_start = mydata->start;
   t_stop  = mydata->stop;
   arr_in  = mydata->input;
   arr_out = mydata->output;

   for (n = t_start; n < t_stop; n += 8)
   {  
      ind1 = 256 + n;
      ind2 = 512 + n;

      vec_a = _mm256_load_ps((float *) (&arr_in[n   ]) );
      vec_b = _mm256_load_ps((float *) (&arr_in[ind1]) );
      vec_c = _mm256_load_ps((float *) (&arr_in[ind2]) );

      _mm256_store_ps((float *) (&arr_out[n   ]), (vec_a) );
      _mm256_store_ps((float *) (&arr_out[ind1]), (vec_b) );
      _mm256_store_ps((float *) (&arr_out[ind2]), (vec_c) );
    }   
    printf("EXECUTION FINISHED ===== Thread : %u \n",t_start);
    pthread_exit(NULL);
}

void foo(float* in,float* out)
{
   unsigned t,i=0;
   for(t=0;t<256;t+=64)
   {
      thread_data_array[i].start    = t;
      thread_data_array[i].stop = t+QUARTER;
      thread_data_array[i].input    = in;
      thread_data_array[i].output   = out;
      pthread_create(&threads[i],NULL,routine,(void*)&thread_data_array[i]);
      i++;
   }
   pthread_exit(NULL);
}

int main()
{
   float *data1;
   float *data2;

   posix_memalign((void**)&data1, 32, 1024 * sizeof(float));
   posix_memalign((void**)&data2, 32, 1024 * sizeof(float));

   Load_inputs(reals,imags);//load data into the two arrays
   foo(data1,data2);
   printf("PROGRAM EXECUTION FINISHED");
   return EXIT_SUCCESS;
}

编译很好,没有错误,但是执行给了我以下内容:

The compilation is good no errors but the execution give me the following:

EXECUTION FINISHED ===== Thread : 0 
EXECUTION FINISHED ===== Thread : 64 
EXECUTION FINISHED ===== Thread : 128 
EXECUTION FINISHED ===== Thread : 192

程序没有终止,仍然缺少PROGRAM EXECUTION FINISHED

the program is not terminating and still missing PROGRAM EXECUTION FINISHED

推荐答案

在您的foo函数中,调用pthread_exit(NULL);,这将立即终止主线程(foo从主线程中调用).这就是为什么您在输出中看不到"PROGRAM EXECUTION FINISHED"的情况,主线程再也没有机会将其打印出来,因为它已在foo中终止.您要执行的操作是使用pthread_join 加入线程,这将使主线程等待其他线程结束.

In you foo function, you call pthread_exit(NULL); which will immediately terminate the main thread (foo is called from the main thread). This is why you are not seeing "PROGRAM EXECUTION FINISHED" in the output, the main thread never gets the chance to print it out because it was terminated in foo. What you want to do instead is join the threads with pthread_join which will make the main thread wait for the other threads to finish.

这篇关于多线程程序正在阻塞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆