Create Two Threads and End with ESC

840阅读 0评论2010-03-06 victorgreat
分类:C/C++

Here is a simple sample for running two threads in a process, and each one prints a message until you key in ESC and ENTER.

May you tell me how to just key in ESC without ENTER to exit main()? Please send the answer to prinz.tigris@gmail.com. Thank you first.

#include
#include

void* a_thread()
{
    while (1) {
        printf("This is a thread.\n");
        sleep(1);
    }

    return;
}

void* b_thread()
{
    while (1) {
        printf("This is b thread.\n");
        sleep(1);
    }

    return;
}

int main()
{
    pthread_t a_tid;
    pthread_t b_tid;

    pthread_create(&a_tid, NULL, a_thread, NULL);
    pthread_create(&b_tid, NULL, b_thread, NULL);

    while ( 0x1b != getc(stdin)) {
        printf("I am the main thread, and Enter ESC to quit.\n");
        sleep(1);
    }
}
上一篇:Components of C Program
下一篇:Process and Thread