简体   繁体   English

在Linux C中使用Pthread进行数组分区

[英]Array Partition using Pthread in linux C

I want to parallel merge sort's partition part, but I don't know how to continue. 我想并行合并排序的分区部分,但是我不知道如何继续。 Please give me some ideas based on my situation, thank you! 请根据我的情况给我一些想法,谢谢!

Serial part: 序列部分:

void Partition(int arr[], int low, int high)
{
    int mid;    
    if(low < high)
    {
        mid = (low + high)/2;
        Partition(arr, low, mid); //first half of data
        Partition(arr, mid+1, high);//second half of data
        MergeSort(arr, low, mid, high);
    }
}

What I have for parallel version: 我对并行版本有什么:

void *PartitionTask(void* rank)
{
    long my_rank = (long)rank;
    if(my_rank == 0)
    {
        int mid;    
        if(low < high)
        {
            mid = (low + high)/2;
            Partition(arr, low, mid); //first half of data
            Partition(arr, mid+1, high);//second half of data
            MergeSort(arr, low, mid, high);
        }
    }
    else
    {

    }
}

The most simple and direct solution would probably be to spawn a new thread for the one partition and entering the other in the calling thread. 最简单直接的解决方案可能是为一个分区产生一个新线程,然后在调用线程中输入另一个线程。

pthread_create(&low_pid, NULL, Partition, (void *)ptr_argument)

With the ptr_argument as a pointer to a shared memory struct containing the pointer to arr and low/high values. 使用ptr_argument作为指向共享存储结构的指针,该结构包含指向arr和低/高值的指针。

When the calling thread returns, call pthread_join with low_pid to synchronize before mergeing. 当调用线程返回时,请在合并之前使用low_pid调用pthread_join进行同步。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM