c언어 초보학생인데 이 코드좀 분석해주실수있나요 ㅠㅠ??
1.
include 스트드오.h
#include 스탠라이브러리.h
#include 셋점프.h
void jmp(jmp_buf env)
{
longjmp(env, 10);
}
int main()
{
int val;
jmp_buf env;
val = setjmp( env);
if( val != 0 )
{
printf("Returned from a longjmp() with value = %d\n", val);
exit(0);
}
printf("Call Jump function : ");
jmp( env);
printf(“May I see this message ?\n”);
return(0);
}
2.
#include 피스레드.h
#include 스트드오.h
#define NUM_THREADS 3
void *PrintHello(void *threadid)
{
long tid;
tid = (long)threadid;
printf("Hello World! It's me, thread #%ld!\n", tid);
pthread_exit(NULL);
}
int main (int argc, char *argv[])
{
pthread_t threads[NUM_THREADS];
int rc;
long t;
for(t=0; 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);
}
}
/* Last thing that main() should do */
pthread_exit(NULL);
}
3.
#include 피스레드.h
#include 스트드오.h
#define NUM_THREADS 2
int extVar;
void *PrintValue(void *threadid)
{
int localVar = 0;
extVar ++;
localVar++;
printf("Print in PrintHello ext = %d, local = %d\n", extVar,localVar);
}
int main (int argc, char *argv[])
{
pthread_t threads[NUM_THREADS];
for(t=0; t rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);
if (rc){
printf("ERROR; pthread_create() is %d\n", rc);
exit(-1);
}
}
}
간단한 설명..
1. setjmp, longjmp 예제. setjmp는 longjmp가 호출됐을때 다시 되돌아올 곳을 지정하는 역할을 합니다. longjmp는 setjmp로 설정한 곳으로 제어를 넘기는 역할이고요. 이 과정에서 값을 하나 넘길 수 있고 이외의 환경들은 setjmp를 호출하던 당시로 되돌아갑니다.
2. 멀티스레딩 예제.
3. 멀티스레딩 하에서 전역변수와 지역변수가 어떻게 다르게 동작하는지를 보여주는 예제.
--
댓글 달기