no-mutex-demo.c 1.5 KB
Newer Older
H
hypox64 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
#include <malloc.h>
#define thread_num 16
#define MB 1024 * 1024

int *array;
int length;//array length

int count;
int t; //number of thread

void *count3s_thread(void* id);
int main()
{
    int i;
    int tid[thread_num];
    pthread_t threads[thread_num];
    length = 64 * MB;
    array = malloc(length*4);     //256MB
    for (i = 0; i < length; i++)     //initial array
    array[i] = i % 2 ? 4 : 3;     //偶数 i 对应数值 3

    for (t = 0; t < thread_num; t++)
    //循环创建 16 个线程
    {
        count = 0;
        tid[t]=t;
        int err = pthread_create(&(threads[t]), NULL, count3s_thread,&(tid[t]) );
        if (err)
        {
            printf("create thread error!\n");
            return 0;
        }
    }
    for (t = 1; t < thread_num; t++)
        pthread_join(threads[t], NULL);    //等待前面创建的计数线程结束

    printf("Total count= %d \n",count);
    return 0;
}
void *count3s_thread(void* id)
//计数线程执行的函数
{
    /*compute portion of the array that this thread should work on*/
    int length_per_thread = length / thread_num;
    //length of every thread
    int start = *(int *)id * length_per_thread;
    //every thread start position
    int i;
    for (i = start; i < start + length_per_thread; i++)
    {
        if (array[i] == 3)
        {
            count++;         //计数,为加入互斥保护
        }
    }
H
4-14  
hypox64 已提交
59
}