Pthread共享內存編程

Pthread百科

POSIX線程(POSIX threads),簡稱Pthreads,是線程的POSIX標準。該標準定義了創建和操縱線程的一整套API。在類Unix操作系統(Unix、Linux、Mac OS X等)中,都使用Pthreads作爲操作系統的線程。Windows操作系統也有其移植版pthreads-win32

實例程序

Hello world

#include <stdio.h>
#include<stdlib.h>
#include<pthread.h>

int thread_count;
void* Hello(void* rank)
{
    long my_rank=(long)rank;

    printf("Hello from thread %ld of %d\n",my_rank,thread_count);
    return NULL;
}

int main(int argc,char* argv[])
{
    long thread;
    pthread_t* thread_handles;

    thread_count=strtol(argv[1],NULL,10);

    thread_handles=malloc(thread_count*sizeof(pthread_t));
 
    for(thread=0;thread<thread_count;thread++)
        pthread_create(&thread_handles[thread],NULL,Hello,(void*)thread);

    printf("Hello from the main thread\n");
    
    for(thread=0;thread<thread_count;thread++)
        pthread_join(thread_handles[thread],NULL);
    
    free(thread_handles);
    
    return 0;
}




編譯運行

gcc -g -Wall -o pth_hello pth_hello.c -lpthread && ./pth_hello 5

結果

在這裏插入圖片描述

矩陣向量乘法

#include <stdio.h>
#include<pthread.h>
#include<stdlib.h>
#define M 2
#define N 3
double A[M][N]={{1,2,3},{4,5,6}};
double x[N]={6,1,7};
double y[M]={0.0};
int thread_count; 
void* Pth_mat_vec(void* rank)
{

    long my_rank=(long)rank;
    int i,j;
    int local_m=M/thread_count;
    int mfr=my_rank*local_m;
    int mlr=mfr+local_m-1;

    for(i=mfr;i<=mlr;i++)
    {
        y[i]=0.0;
        for(j=0;j<N;j++)
            y[i]+=A[i][j]*x[j];
    }
    return NULL;
}
int  printResult()
{
    int i,j;
    printf("A:\n");
    for(i=0;i<M;i++)
    {
        for(j=0;j<N;j++)
            printf("%.2f\t",A[i][j]);
        printf("\n");
    }
    
    printf("x:\n");
    for(j=0;j<N;j++)
        printf("%.2f\t",x[j]);
    printf("\nAx:\n");
    
    for(i=0;i<M;i++)
        printf("%.2f\t",y[i]);
    printf("\n");
    return 0;
}
int main(int argc,char* argv[])
{

    long thread;

    pthread_t* thread_handles;

    thread_count=strtol(argv[1],NULL,10);

    thread_handles=malloc(thread_count*sizeof(pthread_t));
 
    for(thread=0;thread<thread_count;thread++)
        pthread_create(&thread_handles[thread],NULL,Pth_mat_vec,(void*)thread);

    for(thread=0;thread<thread_count;thread++)
        pthread_join(thread_handles[thread],NULL);
    free(thread_handles);
    
    printResult();
    return 0;
}


編譯運行

 gcc -g -Wall -o pth_mat_vec pth_mat_vec.c  -lpthread && ./pth_mat_vec 2

結果

在這裏插入圖片描述

π\pi 值估計(互斥量)

#include <stdio.h>
#include<stdlib.h>
#include<pthread.h>

int thread_count;
long long n;
double sum=0;
pthread_mutex_t*  mutex_p;
void* Thread_sum(void* rank)
{
  double factor,my_sum=0.0;
  long long i;
  long my_rank=(long)rank;
  long long my_n=n/thread_count;
  long long mfi=my_n*my_rank;
  long long mli=mfi+my_n;
  factor=mfi%2==0 ? 1.0:-1.0;

  for(i=mfi;i<mli;i++,factor*=-1.0)
      my_sum+=factor/(2*i+1);
  pthread_mutex_lock(&mutex_p);
  sum+=my_sum;
  pthread_mutex_unlock(&mutex_p);
  return NULL;
}

int main(int argc,char* argv[])
{
   
   long thread;
   printf("please enter n:\t");
   scanf("%lld",&n);

   pthread_t* thread_handles;
   
   pthread_mutex_init(&mutex_p,NULL);
   thread_count=strtol(argv[1],NULL,10);

   thread_handles=malloc(thread_count*sizeof(pthread_t));

   for(thread=0;thread<thread_count;thread++)
       pthread_create(&thread_handles[thread],NULL,Thread_sum,(void*)thread);

   for(thread=0;thread<thread_count;thread++)
       pthread_join(thread_handles[thread],NULL);
   free(thread_handles);
   
   printf("pi: %lf\n",4.0*sum);
   return 0;

}



編譯運行

gcc -g -Wall -o pth_pi pth_pi.c -lpthread && ./pth_pi 5

結果

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章