#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *printMessage(void *threadId) {
long tid;
tid = (long)threadId;
printf("Hello World! It's me, thread #%ld!\n", tid);
pthread_exit(NULL);
}
int main (int argc, char *argv[]) {
pthread_t threads[5];
int rc;
long t;
for(t = 0; t < 2; t++) {
printf("In main: creating thread %ld\n", t);
rc = pthread_create(&threads[t], NULL, printMessage, (void *)t);
if (rc) {
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
/* Wait for all threads to complete */
for (t = 0; t < 2; t++) {
pthread_join(threads[t], NULL);
}
pthread_exit(NULL);
}
C 中多线程任务
最新推荐文章于 2024-07-09 18:38:23 发布