#include<pthread.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define NUM_THREADS 8
char *messages[NUM_THREADS];
struct thread_data
{
int thread_id;
int sum;
char *message;
};
struct thread_data thread_data_array[NUM_THREADS];
void *PrintHello(void *threadarg)
{
int taskid, sum;
char *hello_msg;
struct thread_data *my_data;
sleep(1);
my_data = (struct thread_data *) threadarg;
taskid = my_data->thread_id;
sum = my_data->sum;
hello_msg = my_data->message;
printf("Thread %d: %s Sum=%d\n", taskid, hello_msg, sum);
pthread_exit(NULL);
}
int main(int argc, char *argv[])
{
pthread_t threads[NUM_THREADS];
//int *taskids[NUM_THREADS];
int rc, t, sum;
sum=0;
messages[0] = "English: Hello World!";
messages[1] = "French: Bonjour, le monde!";
messages[2] = "Spanish: Hola al mundo";
messages[3] = "Klingon: Nuq neH!";
messages[4] = "German: Guten Tag, Welt!";
messages[5] = "Russian: Zdravstvytye, mir!";
messages[6] = "Japan: Sekai e konnichiwa!";
messages[7] = "Latin: Orbis, te saluto!";
for(t=0;t<NUM_THREADS;t++) {
sum = sum + t;
thread_data_array[t].thread_id = t;
thread_data_array[t].sum = sum;
thread_data_array[t].message = messages[t];
printf("Creating thread %d\n", t);
rc = pthread_create(&threads[t], NULL, PrintHello, (void *)&thread_data_array[t]);
if (rc) {
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
pthread_exit(NULL);
}
위의 코드 조각은 내가 스레드의 pthreads 문제와 몇 가지 질문
- 3를하는 경우에 대한 마음에 몇 가지 질문이 초보자로서 GCC
를 사용하여 나에게 리눅스에서 세그먼트 오류를 제공합니다 스레드가 동시에 생성되고 종료해야하는 경우 먼저 생성 된 스레드가 끝에 종료됩니다.
- 는 오/p를 스레드 생성과 종료 프로그램마다 다를 수 있습니다 경우 왜? (나는 그들이 "그렇게 나타났습니다)
내가 ", QNX의 프로그램 해봤 잘 완벽하게 작동 컴파일을 시도하고 실행 간단한 것 int main() {printf ("Hello world \ n"); return 0;} ? –