私は自分自身にpthreadsスレッドを教えようとしています。私はコンパイルし、正しく実行以下のソースを、持っている:この例でpthread_joinがスレッドの配列を正しく閉じないのはなぜですか?
#include <stdio.h>
#include <pthread.h>
#define PTHREAD_COUNT 10
#define FREQ 5
void *thread_function(void *arg) {
int *incoming = (int *)arg;
int freqIdx;
for (freqIdx = 0; freqIdx < FREQ; freqIdx++)
fprintf(stdout, "Hello, world (thread %d)\n", *incoming);
return NULL;
}
int main(int argc, char **argv) {
pthread_t thread_IDs[PTHREAD_COUNT];
void *exit_status;
int threadIdx;
for (threadIdx = 0; threadIdx < PTHREAD_COUNT; threadIdx++) {
pthread_create(&thread_IDs[threadIdx], NULL, thread_function, &threadIdx);
pthread_join(thread_IDs[threadIdx], &exit_status);
}
return 0;
}
私は、次の結果が得られます。
Hello, world (thread 0)
Hello, world (thread 0)
Hello, world (thread 0)
Hello, world (thread 0)
Hello, world (thread 0)
Hello, world (thread 1)
...
Hello, world (thread 9)
I pthread_create
場合は、別のループを超えるpthread_t
型の配列、その後、pthread_join
ループすると、物事は失敗します:
#include <stdio.h>
#include <pthread.h>
#define PTHREAD_COUNT 10
#define FREQ 5
void *thread_function(void *arg) {
int *incoming = (int *)arg;
int freqIdx;
for (freqIdx = 0; freqIdx < FREQ; freqIdx++)
fprintf(stdout, "Hello, world (thread %d)\n", *incoming);
return NULL;
}
int main(int argc, char **argv) {
pthread_t thread_IDs[PTHREAD_COUNT];
void *exit_status;
int threadIdx;
/* here I split the thread _create and _join steps into separate loops */
for (threadIdx = 0; threadIdx < PTHREAD_COUNT; threadIdx++)
pthread_create(&thread_IDs[threadIdx], NULL, thread_function, &threadIdx);
for (threadIdx = 0; threadIdx < PTHREAD_COUNT; threadIdx++)
pthread_join(thread_IDs[threadIdx], &exit_status);
return 0;
}
これからの出力はかなり間違っています。各スレッドから5つのfprintf
ステートメントを取得する代わりに、スレッド2と3から1つまたは2つ、スレッド0から約20から25 Hello, world
ステートメントを取得します。
これはなぜ失敗しますか?