多线程实例

#include "stdio.h"
#include "unistd.h"
#include "pthread.h"
 
void *func(void *p)
{
    *(int*)p=200;
    int i;
    for (i=0; i<20; i++)
    {
        write(1, ".", 1);
        sleep(1);
    }
    return (void*)9999;
}
 
int main()
{
    pthread_t id;
    int x = 100;
    pthread_create(&id, NULL, func, &x);
    int i;
    for (i=0; i<10; i++)
    {
        write(1, "*", 1);
        sleep(1);
    }
    printf("x=%d\n", x);
    void *v;
    puts("Wait for thread over");
    pthread_join(id, &v);
    printf("The thread returned %d\n", (int)v);
 
    return 0;
}
posted on 2014-12-30 16:22  张武亮  阅读(149)  评论(0编辑  收藏  举报