使用pthread处理数组/向量的部分 [英] Using pthreads to process sections of an array/vector

查看:91
本文介绍了使用pthread处理数组/向量的部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有一个长度为256(可以或多或少)的数组或向量,并且要生成的pthread数量为4(可以或多或少)。

Assume we have an array or vector of length 256(can be more or less) and the number of pthreads to generate to be 4(can be more or less).

我需要弄清楚如何将每个pthread分配给进程的向量的一部分。

I need to figure out how to assign each pthread to a process a section of the vector.

因此以下代码调度了多个线程。

So the following code dispatches the multiple threads.

for(int i = 0; i < thread_count; i++)
{
    int *arg = (int *) malloc(sizeof(*arg));
    *arg = i;
    thread_err = pthread_create(&(threads[i]), NULL, &multiThread_Handler, arg);

    if (thread_err != 0)
                printf("\nCan't create thread :[%s]", strerror(thread_err));
}

如上面的代码所示,每个线程将一个参数值传递给启动功能。对于四个线程,参数值的范围是0到3,五个线程= 0到4,依此类推。

As you can tell from the above code, each thread passes an argument value to the starting function. Where in the case of the four threads, the argument values range from 0 to 3, 5 threads = 0 to 4, and so forth.

现在,启动函数执行以下:

Now the starting function does the following:

void* multiThread_Handler(void *arg)
{
   int thread_index = *((int *)arg);

   unsigned int start_index = (thread_index*(list_size/thread_count));
   unsigned int end_index = ((thread_index+1)*(list_size/thread_count));

   std::cout << "Start Index: " << start_index << std::endl;
   std::cout << "End Index: " << end_index << std::endl;
   std::cout << "i: " << thread_index << std::endl;

   for(int i =  start_index; i < end_index;  i++)
   {
      std::cout <<"Processing array element at: " << i << std::endl;
   }

}

因此在上面的代码中,参数为0的线程应处理0-63节(在数组大小为256且线程数为4的情况下),参数为1的线程应处理64-127节,依此类推。最后一个线程处理192-256。

So in the above code, the thread whose argument is 0 should process the section 0 - 63(in the case of an array size of 256 and a thread count of 4), the thread whose argument is 1 should process the section 64 - 127, and so forth. The last thread processing 192 - 256.

这四个部分中的每一个都应并行处理。

Each of these four sections should processed in parallel.

此外,原始主代码中还包含pthread_join()函数,以确保每个线程在主线程终止之前完成。

Also, the pthread_join() functions are present in the original main code to make sure each thread finishes before the main thread terminates.

问题是,上面的for循环中的值i呈现可疑的大值。我不确定为什么会发生这种情况,因为我对pthread相当陌生。

The problem is, that the value i in the above for-loop is taking on suspiciously large values. I'm not sure why this would occur since I am fairly new to pthreads.

似乎有时它可以很好地工作,有时i的值太大,以至于导致程序中止或出现分段错误

It seems like sometimes it works perfectly fine and other times and other times, the value of i becomes so large that it causes the program to either abort or presents a segmentation fault.

推荐答案

   // Incorrect Code
   unsigned int start_index = (thread_index*(list_size/thread_count));
   unsigned int end_index = ((thread_index+1)*(list_size/thread_count));

上面的代码是关键区域,在您上面的程序中是错误的。因为没有使用同步机制,所以存在数据争用。这导致start_index和end_index计数器的计算错误,因此我们可能会得到错误的(随机垃圾值),因此for循环变量 i 折腾。因此,您应该使用以下代码来同步程序的关键区域。

The above code is critical region is wrong in your above program. as there is no synchronization mechanism has been used so there is data race.This leads to the wrong calculation of start_index and end_index counters and hence we may get wrong(random garbage values) and hence the for loop variable "i" goes on the toss. So you should use the following code to synchronize the critical region of your program.

   // Correct Code
   s=thread_mutex_lock (&mutexhandle);
   start_index = (thread_index*(list_size/thread_count));
   end_index = ((thread_index+1)*(list_size/thread_count));
   s=thread_mutex_unlock (&mutexhandle);

这篇关于使用pthread处理数组/向量的部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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