多线程算法--归并排序

news/2024/5/17 16:45:52 标签: 多线程, 归并排序, 随机数种子

如下:

#include <pthread.h>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cstdlib>
#include <typeinfo>
#include <algorithm>
#include "unistd.h"
using namespace std;
#define NUM_THREADS 5

template<typename T>
struct Leninfo
{
    Leninfo(int _l, int _h, T& _arr):l(_l),h(_h),arr(_arr){}
    int l;
    int h;
    T &arr;
};

vector<int> read_file(string filename)
{
    ifstream file(filename);
    int each_element;
    vector<int> data;
    if(file) {
        while(file >> each_element) {
            data.push_back(each_element);
        }
    }

    return data;
}

template<typename T>
void* my_merge(T& arr, int l, int mid, int r)
{
    size_t i = 0,j = 0, k = l;
    size_t bound1 = mid-l, bound2 = r-mid;
    T first;
    T second;
    for(size_t t = l; t!=mid; t++) {first.push_back(arr[t]); }
    for(size_t t = mid; t!=r; t++) {second.push_back(arr[t]); }
    while(i < bound1 && j < bound2) {
        if(first[i] < second[j]) {
            arr[k++] = first[i++];
        }else {
            arr[k++] = second[j++];
        }
    }
    while(i < bound1) {
        arr[k++] = first[i++];
    }
    while(j < bound2) {
        arr[k++] = second[j++];
    }
}

template<typename T>
void *merge_sort(void *info)
{
    auto args = (struct Leninfo<T>*)(info);
    size_t l, h;
    l = args->l;
    h = args->h;
    T& data = args->arr;

    if(h - l < 5) {
        sort(data.begin()+l, data.begin()+h);
        return nullptr;
    }

    pthread_t tid1, tid2;
    auto mid = (l+h)>>1;
    Leninfo<T> info1 = Leninfo<T>(l,mid,data), info2 = Leninfo<T>(mid,h,data);

    pthread_create(&tid1,nullptr,merge_sort<T>,&info1);
    pthread_create(&tid2,nullptr,merge_sort<T>,&info2);

    void *status1, *status2;
    pthread_join(tid1, &status1);
    pthread_join(tid2, &status2);

    my_merge(data, l, mid, h);
}

void rand_num(vector<int> &nums, size_t n)
{
    if(n < 0) return ;
    for(int i=0; i<n; i++) {
        nums.push_back(rand()%n);
    }
}

int main()
{
    void *status;
    vector<int> data;
    rand_num(data, 50);
    auto info = Leninfo<vector<int>>(0,data.size(),data);
    pthread_t tid;
    pthread_create(&tid,NULL,merge_sort<decltype(data)>,(void *)&info);
    pthread_join(tid, &status);

    for(const auto &x : data) {
        cout << x << " ";
    }
    cout << endl;

    pthread_exit(nullptr);
    return 0;
}

运行结果:
在这里插入图片描述


http://www.niftyadmin.cn/n/726633.html

相关文章

项目,人员少、任务多项目经理应该怎么做?

项目周期偏长&#xff0c;人数少&#xff0c;项目成员要做的事情多&#xff0c;如果不能够扩张人力资源&#xff0c;项目或任务出现延误或失败。管理者应该怎么做。 当发现手上事情过多&#xff0c;但是却无人可用时&#xff0c;管理者需要将任务进行重新规划。 我们需要将所…

深入了解php底层机制(-)

作为一门动态语言&#xff0c;php是如何实现的&#xff0c;其底层机制如何&#xff0c;具有什么样的特点&#xff0c;本文深入浅出介绍了包括php设计理念、整体结构、核心数据结构和变量在内的相关底层知识&#xff0c;对我们更好的开发php程序&#xff0c;优化性能等有一定的指…

Mark 一下专业名词

HTTP long polling Linux eventfd

html到asp.net样式变乱

当把美工做好的Html放入aspx文件后&#xff0c;样式有时会变乱&#xff0c;因为编码问题&#xff0c;把css文件以utf-8的格式保存。问题就OK 参考&#xff1a;http://zhidao.baidu.com/question/180588324.html 转载于:https://www.cnblogs.com/applesuch5/archive/2011/09/20/…

实时了解项目信息,让项目管理更高效

项目管理是一个繁杂的过程&#xff0c;因此&#xff0c;项目参与者实时了解项目的最新进展、最新资料&#xff0c;是项目顺利进行的重要条件。 同时&#xff0c;整个项目过程从开始到结束&#xff0c;中间会涉及多个环节、任务节点与时间段。这些关键点也是项目容易出现分风险…

Linux 进程切换和线程切换的区别分析

进程切换分两步&#xff1a; 1.切换页目录以使用新的地址空间 2.切换内核栈和硬件上下文 对于linux来说&#xff0c;线程和进程的最大区别就在于地址空间&#xff0c;对于线程切换&#xff0c;第1步是不需要做的&#xff0c;第2是进程和线程切换都要做的。 切换的性能消耗&…

CentOS 5.5 安装和卸载桌面

显示系统已经安装的组件&#xff0c;和可以安装的组件:#yum grouplist安装GNOME桌面环境yum groupinstall "GNOME Desktop Environment"安装KDE桌面环境yum groupinstall "KDE (K Desktop Environment)" 卸载GNOME桌面环境yum groupremove "GNOME Des…