1004编写一个程序,建立0~4共5个线程,而后每一个线程输出一个hello

/*ide

  • 编写一个程序,建立0~4共5个线程,而后每一个线程输出一个hello
    */

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>线程

#define NUM_THREADS 8code

void PrintHello(void args)
{
int thread_arg;
sleep(1);
thread_arg=(int)args;
printf("Hello from thread %d\n",thread_arg);
return NULL;
}
int main(int argc, char *argv[])
{
int rc,t;
pthread_t thread[NUM_THREADS];it

for(t=0;t<NUM_THREADS;t++)
{
    printf("Creating thread %d\n",t);
    rc=pthread_create(&thread[t],NULL,PrintHello,(void *)t);
    if(rc)
    {
        printf("ERROR;return code is %d\n",rc);
        return EXIT_FAILURE;
    }
}
for(t=0;t<NUM_THREADS;t++)
{
    pthread_join(thread[t],NULL);
}
return EXIT_SUCCESS;

}io

相关文章
相关标签/搜索