int v = 30, x; if (v > 30) x = 100; else x = 0;
위의 if / else 문을 한줄로,
x = (v > 30) ? 100 : 0;
코드가 길어지는 것이 싫을 때, 사용하면 좋지 아니할까?
int v = 30, x; if (v > 30) x = 100; else x = 0;
x = (v > 30) ? 100 : 0;
#include <stdio.h>
#include <io.h>
#include <conio.h>
void main()
{
_finddata_t fd;
long handle;
int result = 1;
handle = _findfirst(".\\*.*", &fd); //현재 폴더 내 모든 파일을 찾는다.
if (handle == -1)
{
printf("There were no files.\n");
return;
}
while (result != -1)
{
printf("File: %s\n", fd.name);
result = _findnext(handle, &fd);
}
_findclose(handle);
return;
}
m_hYUVFile = CreateFile(m_cFileName, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if(m_hYUVFile == NULL)
{
printf("CreateFile() file. Err=%d\n", GetLastError());
return -1;
}
DWORD dwFileSize = ::GetFileSize(m_hYUVFile, NULL);
m_hYUVMapFile = CreateFileMapping(m_hYUVFile, NULL, PAGE_READONLY, 0, dwFileSize, NULL);
if(m_hYUVMapFile == NULL)
{
printf("CreateFileMapping() fail. Err=%d\n", GetLastError());
return NULL;
}
DWORD m_dwMapUnit = systeminfo.dwAllocationGranularity //65536
DWORD m_dwFrameSize = dwWidth * dwHeight * 3 / 2; //한 프레임의 크기
DWORD dwMultiple = (DWORD)floor(((double)m_nPresentationCnt*(double)m_dwFrameSize)/(double)m_dwMapUnit);
//m_nPresentationCnt는 몇 번째 프레임인지를 지시한다(순차 증가).
//그러므로 m_nPresentationCnt*m_dwFrameSize는 실제 파일 내에서의 offset을 지시한다.
//앞서 말한대로, 지원하는 단위(m_dwMapUnit)로만 offset을 줄 수 있으므로, m_dwMapUnit에 대한 배수를 계산한다.
DWORD dwDiff = (m_dwFrameSize*m_nPresentationCnt) - (m_dwMapUnit*dwMultiple);
//dwDiff는 실제 파일의 offset과의 차이를 계산한다.
m_pcYUVFile = (char *)MapViewOfFile(m_hYUVMapFile, //CreateFileMapping을 통해 생성한 핸들러
FILE_MAP_READ, 0,
(dwMultiple*m_dwMapUnit), //m_dwMapUnit의 배수로 offset 지정
(m_dwFrameSize+dwDiff)); //파일에서 접근하고자 하는 크기
//m_pcYUVFile에 대한 처리 동작 수행
UnmapViewOfFile(m_pcYUVFile); //Unmaps a mapped view of a file from the calling process's address space
CloseHandle(m_hYUVMapFile); //Close file map handle
CloseHandle(m_hYUVFile); //Close file handle
#include <stdio.h>
#include <process.h>
#include <windows.h>
//#include <pthread.h> //유닉스/리눅스 계열에서 뮤텍스를 위한 헤더
#define MUTEX
int cnt = 0; //각 스레드에서 접근하는 전역변수
#ifdef MUTEX
HANDLE hMutex; //뮤텍스의 핸들러
//pthread_mutex_t hMutex; //유닉스/리눅스 계열에서의 뮤텍스 핸들러
#endif
DWORD WINAPI Thread1(void *arg)
{
int i, tmp;
for(i=0; i<1000; i++)
{
#ifdef MUTEX
WaitForSingleObject(hMutex, INFINITE); //뮤텍스에 대한 Lock
//pthread_mutex_lock(hMutex) //유닉스/리눅스 계열에서의 Lock
#endif
tmp = cnt;
Sleep(1000);
cnt = tmp + 1;
printf("[1]: %d\n", cnt);
#ifdef MUTEX
ReleaseMutex(hMutex); //뮤텍스에 대한 Unlock
//pthread_mutex_unlock(hMutex) //유닉스/리눅스 계열에서의 Unlock
#endif
}
printf("Thread1 End\n");
return 0;
}
DWORD WINAPI Thread2(void *arg)
{
int i, tmp;
for(i=0; i<1000; i++)
{
#ifdef MUTEX
WaitForSingleObject(hMutex, INFINITE); //뮤텍스에 대한 Lock
//pthread_mutex_lock(hMutex) //유닉스/리눅스 계열에서의 Lock
#endif
tmp = cnt;
Sleep(1000);
cnt = tmp + 1;
printf("[2]: %d\n", cnt);
#ifdef MUTEX
ReleaseMutex(hMutex); //뮤텍스에 대한 Unlock
//pthread_mutex_unlock(hMutex) //유닉스/리눅스 계열에서의 Unlock
#endif
}
printf("Thread2 End\n");
return 0;
}
int main(int argc, char *argv[])
{
HANDLE hThread[2];
#ifdef MUTEX
hMutex = CreateMutex(NULL, FALSE, NULL); //뮤텍스 생성 및 초기화
//pthread_mutex_t hMutex = PTHREAD_MUTEX_INITIALIZER; //유닉스/리눅스 계열에서의 뮤텍스 생성 및 초기화
//pthread_mutex_t hMutex = pthread_mutex_init(); //유닉스/리눅스 계열에서의 뮤텍스 초기화(동적 초기화)
#endif
hThread[0]=CreateThread(NULL, 0, Thread1, NULL, 0, NULL);
hThread[1]=CreateThread(NULL, 0, Thread2, NULL, 0, NULL);
WaitForMultipleObjects(2, hThread, TRUE, INFINITE); //두 개의 스레드가 종료될때까지 대기
printf("%d\n", cnt); //결과값 출력
CloseHandle(hThread[0]);
CloseHandle(hThread[1]);
#ifdef MUTEX
CloseHandle(hMutex); //뮤텍스 핸들러 파괴
//pthread_mutex_destroy(hMutex); //유닉스/리눅스 계열에서의 뮤텍스 핸들러 파괴
#endif
return 0;
}