Я изучаю темы, используя C, и нашел следующую программу «Hello World».Когда я печатаю значение pthread_value () внутри потока и вне потока с помощью pthread_join (), значения, возвращаемые двумя потоками, совершенно разные.
/******************************************************************************
* FILE: hello.c
* DESCRIPTION:
* A "hello world" Pthreads program. Demonstrates thread creation and
* termination.
* AUTHOR: Blaise Barney
* LAST REVISED: 08/09/11
******************************************************************************/
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#define NUM_THREADS 5
void *PrintHello(void *threadid)
{
long tid;
tid = (long)threadid;
printf("Hello World! It's me, thread #%ld!, %ld\n", tid, pthread_self());
long a= pthread_self();
//pthread_exit(NULL);
long *p = &a;
return p;
}
int main(int argc, char *argv[])
{
pthread_t threads[NUM_THREADS];
int rc;
void *status;
long t;
FILE* file = fopen("test.txt", "r");
for(t=0;t<NUM_THREADS;t++){
printf("In main: creating thread %ld\n", t);
rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);
if (rc){
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
for(t=0; t<NUM_THREADS;t++){
pthread_join(threads[t], &status);
printf("%ld\n",(long)status);
}
/* Last thing that main() should do */
pthread_exit(NULL);
}