线程传递参数

#include "stdio.h"
#include "unistd.h"
#include "pthread.h"
 
void *func(void *p)
{
    int i;
    int *a=(int*)p;
    for (i=0; i<20; i++)
    {
        a[i]=100+i;
        sleep(1);
    }
    return NULL;
}
 
int main()
{
    int a[20];
    pthread_t id;
    pthread_create(&id, NULL, func, a);
    sleep(2);            //让辅助线程先完成
    int i;
    for (i=0; i<20; i++)
    {
        printf("%d ", a[i]);
        fflush(stdout);
        sleep(1);
    }
    return 0;
}
posted on 2014-12-26 15:50  张武亮  阅读(170)  评论(0编辑  收藏  举报