صفحه نخست | سیستم وبلاگ | در مورد من | twitter
#include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <err.h> #include <string.h> #include <unistd.h> struct { int idx; pthread_t tid; } *thread; void * __attribute__((noreturn)) exitEN(int e, char *str) { if (str != NULL) fprintf(stderr, "%s: ", str); fprintf(stderr, "%s\n", strerror(e)); exit(e); } void * threadFunc(void *arg) { int i = (*(int *) arg); for (;;) { printf("thread %0*d\n", i, i); sleep(2); } } int main(int argc, char *argv[]) { int count, i, s; if (argc != 2) { fprintf(stderr, "usage: %s number-of-threads\n", argv[0]); exit(EXIT_FAILURE); } count = atoi(argv[1]); if (count < 1) errx(EXIT_FAILURE, "number-of-thread must be greater than 0"); thread = calloc(count, sizeof(*thread)); if (thread == NULL) err(EXIT_FAILURE, "calloc"); for (i = 0; i < count; i++) { thread[i].idx = i + 1; s = pthread_create(&thread[i].tid, NULL, threadFunc, &thread[i].idx); if (s != 0) exitEN(s, "pthread_create"); } pthread_exit(EXIT_SUCCESS); }
کلیه حقوق برای دارندهی سایت محفوظ است.