POSIX 스레드 코딩을 위해 Windows 7 컴퓨터에서 MINGW를 사용하고 있습니다. 내 시스템에서 실행될 때 다음과 같은 출력을 보여주고있다루프에서 스레드를 생성하면 처음 2 개의 스레드가 실행되지 않습니다.
#include <stdio.h>
#include <pthread.h>
#include <process.h>
#define NUM_THREADS 5
void *PrintHello(void *threadid)
{
long tid;
tid = (long)threadid;
printf("Hello Dude...!!!\t I am thread no #%ld\n",tid);
pthread_exit(NULL);
}
int main()
{
pthread_t thread[NUM_THREADS];
int rc;
long t;
for(t=0;t<NUM_THREADS;t++)
{
printf("Inside the Main Thread...\nSpawning Threads...\n");
rc=pthread_create(&thread[t],NULL,PrintHello,(void*)t);
if(rc)
{
printf("ERROR: Thread Spawning returned code %d\n",rc);
exit(-1);
}
}
return 0;
}
프로그램 위 :
는 다음과 같은 간단한 코드를 고려
Inside the Main Thread...
Spawning Threads...
Inside the Main Thread...
Spawning Threads...
Hello Dude...!!! I am thread no #0
Inside the Main Thread...
Spawning Threads...
Hello Dude...!!! I am thread no #1
Inside the Main Thread...
Spawning Threads...
Hello Dude...!!! I am thread no #2
Inside the Main Thread...
Spawning Threads...
이 프로그램은 5 개 스레드를 생성하기로했다. 그러나 그것은 단지 2 개의 스레드만을 생성했습니다. 처음 2와 마지막 2 행은 pthread_create() 루틴이 호출 될 예정임을 시사합니다. 그리고 "rc"변수가 "1"이 아니기 때문에 쓰레드 생성시에 아무런 에러도 없다. 그렇지 않으면 "if (rc)"부분에 충돌했을 것이다.
그래서 어디서 오류가 있습니까? 아니면 내 Windows 머신과 관련이 있습니다.
long을 int로 변경했지만 여전히 첫 번째 스레드가 실행되지 않고 있습니다 ... – Shantanu
고마워요 ... – Shantanu