第一个多线程程序——使用pthread

最近学习并行算法,先看了看pthread多线程编程。今天写了第一个算法,就是求10000个数的和。算法很简单,用了两个线程,第一个求前5000个数的和,第二个求后5000个的和,然后把两个线程的结果加起来。

程序如下:

#include <pthread.h>
#include <cstdlib>
#include <iostream>
using namespace std;

#define NUM_THREAD 2

// 线程调用的函数的参数
struct add_arg
{
    int* data;
    int  size;
    int  left;
    int  right;
    int* sum;
};

//计算序列的和,这个函数由线程调用
void *add(void* addarg)
{
    struct add_arg *arg;
    arg = (struct add_arg*)addarg;
    int i;
    arg->sum[0] = 0;
    cout<<arg->size<<" "<<arg->left<<" "<<arg->right<<endl;
    for(i=arg->left;i<=arg->right;i++)
        arg->sum[0] += arg->data[i];
    cout<<arg->sum[0]<<endl;
}

//序列求和的算法,包含两个线程
int addnum(int *A, int size)
{
    int i;
    struct add_arg arg[2];
    arg[0].data = A;
    arg[0].size = size;
    arg[0].left = 0;
    arg[0].right = int(size/2);
    arg[0].sum = new int;

    arg[1].data = A;
    arg[1].size = size;
    arg[1].left = arg[0].right+1;
    arg[1].right = size-1;
    arg[1].sum = new int;

    pthread_t thread[2];
    int rc;
    for(i=0;i<2;i++){
        //创建线程
        rc = pthread_create(&thread[i],NULL,add,(void* )&arg[i]);
        //等待线程完成
        pthread_join(thread[i],NULL);
    }
    cout<<arg[0].sum[0]+arg[1].sum[0]<<endl;
    return *arg[0].sum+*arg[1].sum;
}

int main()
{
    int* A = new int[10000];
    int i;
    for(i=0;i<10000;i++){
        if(i%2==0) A[i] = i;
          else A[i] = i;
    }
    int sum = addnum(A,10000);
    cout<<"the sum is : "<<sum<<endl;
   
}

这个程序中包含了两个多线程函数:
int pthread_create (pthread_t *thread, const pthread_attr_t *attr, void
*(*start_routine) (void *), void *arg) ;
创建一个线程
他的文档可以参考这里

int pthread_join (pthread_t thread, void **value_ptr);
他的文档可以参考这里

g++ -pthread add.cpp -o add
./add

运行结果如下:
10000 0 5000
12502500
10000 5001 9999
37492500
49995000
the sum is : 49995000
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用C语言编写的多线程程序来生成斐波那契数列的示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <pthread.h> #define MAX_FIB 50 int fib[MAX_FIB]; void *calculate_fib(void *n) { int i; int *num = (int *)n; fib[0] = 0; fib[1] = 1; for (i=2; i<*num; i++) { fib[i] = fib[i-1] + fib[i-2]; } pthread_exit(NULL); } int main() { int n; pthread_t tid; printf("Enter the number of fibonacci numbers to generate (max %d): ", MAX_FIB); scanf("%d", &n); if (n > MAX_FIB) { printf("Error: maximum number of fibonacci numbers exceeded.\n"); exit(EXIT_FAILURE); } pthread_create(&tid, NULL, calculate_fib, &n); pthread_join(tid, NULL); printf("Fibonacci sequence:\n"); for (int i=0; i<n; i++) { printf("%d ", fib[i]); } printf("\n"); return 0; } ``` 在该程序中,我们首先定义了一个全局数组`fib`来存储生成的斐波那契数列。然后,我们创建了一个新的线程来计算斐波那契数列,并将线程ID存储在`tid`变量中。我们使用`pthread_create`函数来创建线程,该函数接受四个参数:线程ID,线程属性,线程函数和传递给线程函数的参数。在本例中,我们将`calculate_fib`函数作为线程函数,并将要生成的斐波那契数列的数量作为参数传递。 `calculate_fib`函数接受一个指向整数的指针作为参数,并使用斐波那契数列的递推公式来计算每个数。然后,该函数将用`pthread_exit`函数退出线程。 在主函数中,我们使用`pthread_join`函数来等待线程的结束。`pthread_join`函数接受两个参数:线程ID和指向线程返回值的指针。在本例中,我们只需要等待线程结束,因此第二个参数为`NULL`。 最后,我们输出生成的斐波那契数列。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值