http://www.hackerschool.org/HS_Boards/zboard.php?id=QNA_programming&no=1994 [º¹»ç]
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
int count=0; // Ä«¿îÅÍ ÃʱⰪ 0ÁöÁ¤
void *thread1(void *arg)
{
while(count<9) // Ä«¿îÅÍ°¡ 9°¡ µÇ±â Àü±îÁö ¹Ýº¹
{
if(count%2==0) // ¦¼öÀÏ °æ¿ì
{
printf("thread1 = %d\n", count); // thread1°ª Ãâ·Â
++count; //Ä«¿îÅÍ Áõ°¡
}
}
return &count; //Ä«¿îÅÍ ÁÖ¼Ò ¹Ýȯ
}
void *thread2(void *arg)
{
while(count<10) Ä«¿îÅÍ°¡ 10ÀÌ µÇ±â Àü±îÁö ¹Ýº¹
{
if(count%2==1) //Ȧ¼öÀÏ °æ¿ì
{
printf("thread2 = %d\n", count); //thread2°ª Ãâ·Â
++count; //Ä«¿îÅÍ Áõ°¡
}
}
return &count; //Ä«¿îÅÍ ÁÖ¼Ò ¹Ýȯ
}
int main(int argc, char **argv)
{
pthread_t t_id1;
pthread_t t_id2;
void *retval1;
void *retval2;
pthread_create(&t_id1, NULL, thread1, NULL); //thread1 »ý¼º
pthread_create(&t_id2, NULL, thread2, NULL); //thread2 »ý¼º
pthread_join(t_id1, &retval1); // t_id1 Á¾·á ±â´Ù¸²
pthread_join(t_id2, &retval2); // t_id2 Á¾·á ±â´Ù¸²
free(retval1); // »ý¼ºÇÑ retval1¸Þ¸ð¸®¿µ¿ªÀ» ÇØÁ¦
free(retval2); // »ý¼ºÇÑ retval2¸Þ¸ð¸®¿µ¿ªÀ» ÇØÁ¦
return 0;
}
½º·¹µå 2°³ »ý¼ºÇؼ ¦¼ö Ȧ¼öÀ϶§ 1¾¿ Áõ°¡½ÃÅ°°í
¾²·¹µå1 2 °¡ Á¾·áµÉ¶§±îÁö ´ë±âÇÏ°í
Á¾·á ÈÄ Ä«¿îÆ®°ªÀ» Ãâ·ÂÇÏ´Â ÄÚµùÀÔ´Ï´Ù.
for¹®À¸·Î ½á¼ µ¹¸±°æ¿ì ¾ÈµÇ´Âµ¥
ÀÌÀ¯°¡ ¹»±î¿ä?.. Ä«¿îÅÍ°ªÀÌ ¾ÕÂÊ¿¡¼ Áõ°¡Çؼ ±×·±°Ç°¡¿ä
ÀÌ ÄÚµùÁ» ´õ °£°áÇÏ°Ô ¼öÁ¤Á» ºÎŹµå¸³´Ï´Ù!! |
Hit : 4088 Date : 2009/04/20 09:25
|