간단한 thread 예제 코드

- 동기화 없음



--- code start ---

#include <iostream>

#include <Windows.h>


DWORD WINAPI ThreadWrite(LPVOID param);

DWORD WINAPI ThreadRead(LPVOID param);


static int g_num = 0;


void main()

{

HANDLE thread_write;

HANDLE thread_read;

DWORD thread_ID_write;

DWORD thread_ID_read;


thread_write = CreateThread(

NULL,

0,

ThreadWrite,

NULL,// data,

0,

&thread_ID_write);

if (NULL == thread_ID_write) {

printf("ERROR: Cant create thread1\n");

//ExitProcess(3);

return;

}


thread_read = CreateThread(

NULL,

0,

ThreadRead,

NULL,// data,

0,

&thread_ID_read);

if (NULL == thread_ID_read) {

printf("ERROR: Cant creat thread2\n");

ExitThread(thread_ID_write);

return;

}


while (1) {

;


if (g_num > 50) {

break;

}

}


ExitThread(thread_ID_read);

CloseHandle(thread_read);

ExitThread(thread_ID_write);

CloseHandle(thread_write);

}


DWORD WINAPI ThreadWrite(LPVOID param)

{

while (1) {

printf("thread write (%d)\n", ++g_num);

Sleep(1*1000/2);

}

return 0;

}


DWORD WINAPI ThreadRead(LPVOID param)

{

while (1) {

printf("thread read (%d)\n", g_num);

Sleep(1*1000/2);

}

return 0;

}

--- end ---

'Programming > C,C++' 카테고리의 다른 글

bubble sort 3 / int  (0) 2017.04.17
strstr 구현하기  (0) 2017.04.17
C++, const with function [펌]  (0) 2017.04.07
C, const vs 가변인자 [펌]  (0) 2017.04.07
Stack (Double Linked), C++  (0) 2017.04.07
Posted by 루나s
,