ReactOS  0.4.13-dev-259-g5ca9c9c
mutex.c
Go to the documentation of this file.
1 #include <windows.h>
2 #include <string.h>
3 #include <stdio.h>
4 
5 
8 
12 
13 
14 void dprintf(char* fmt, ...)
15 {
16  va_list args;
17  char buffer[255];
18 
19  va_start(args,fmt);
22  va_end(args);
23 }
24 
25 
27 {
28  DWORD dwError = 0;
29  DWORD i;
30 
31  dprintf("Thread 1 running!\n");
32 
33  for (i = 0; i < 10; i++)
34  {
36  if (dwError == WAIT_FAILED)
37  {
38  dprintf("Thread2: WaitForSingleObject failed!\n");
39  return 1;
40  }
41  else if (dwError == WAIT_ABANDONED_0)
42  {
43  dprintf("Thread2: WaitForSingleObject returned WAIT_ABANDONED_0\n");
44  }
45 
46  dprintf("Thread1: dwCounter : %lu -->", dwCounter);
47  dwCounter++;
48  dprintf(" %lu\n", dwCounter);
50  }
51 
52  dprintf("Thread 1 done!\n");
53 
54  return 1;
55 }
56 
58 {
59  DWORD dwError = 0;
60  DWORD i;
61  dprintf("Thread 2 running!\n");
62 
63  for (i = 0; i < 10; i++)
64  {
66  if (dwError == WAIT_FAILED)
67  {
68  dprintf("Thread2: WaitForSingleObject failed!\n");
69  return 1;
70  }
71  else if (dwError == WAIT_ABANDONED_0)
72  {
73  dprintf("Thread2: WaitForSingleObject returned WAIT_ABANDONED_0\n");
74  }
75 
76  dprintf("Thread2: dwCounter : %lu -->", dwCounter);
77  dwCounter++;
78  dprintf(" %lu\n", dwCounter);
80  }
81 
82  dprintf("Thread 2 done!\n");
83 
84  return 1;
85 }
86 
87 
88 int main(int argc, char* argv[])
89 {
90  DWORD dwError;
91  DWORD id1,id2;
92 
93  AllocConsole();
96 
97  dprintf("Calling CreateMutex()\n");
98  hMutex = CreateMutexW(NULL, FALSE, L"TestMutex");
100  {
101  dprintf("CreateMutex() failed! Error: %lu\n", GetLastError());
102  return 0;
103  }
104  dprintf("CreateMutex() succeeded!\n");
105 
106  hThread[0] = CreateThread(0, 0, thread1, 0, 0, &id1);
107  hThread[1] = CreateThread(0, 0, thread2, 0, 0, &id2);
108 
109  dprintf("Calling WaitForMultipleObject()\n");
111  dprintf("WaitForMultipleObject() Error: %lu\n", dwError);
112 
113  CloseHandle(hThread[0]);
114  CloseHandle(hThread[1]);
115 
117 
118  dprintf("Main thread done!\n");
119 
120  return 0;
121 }
static int argc
Definition: ServiceArgs.c:12
#define TRUE
Definition: types.h:120
BOOL WINAPI AllocConsole(VOID)
Definition: console.c:48
#define CloseHandle
Definition: compat.h:398
HANDLE InputHandle
Definition: mutex.c:7
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
#define INVALID_HANDLE_VALUE
Definition: compat.h:391
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
HANDLE WINAPI GetStdHandle(IN DWORD nStdHandle)
Definition: console.c:152
GLuint buffer
Definition: glext.h:5915
#define argv
Definition: mplay32.c:18
DWORD WINAPI WaitForMultipleObjects(IN DWORD nCount, IN CONST HANDLE *lpHandles, IN BOOL bWaitAll, IN DWORD dwMilliseconds)
Definition: synch.c:151
Definition: match.c:390
DWORD dwCounter
Definition: mutex.c:10
HANDLE OutputHandle
Definition: mutex.c:6
DWORD WINAPI WaitForSingleObject(IN HANDLE hHandle, IN DWORD dwMilliseconds)
Definition: synch.c:82
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
DWORD WINAPI thread2(LPVOID crap)
Definition: mutex.c:57
#define va_end(ap)
Definition: acmsvcex.h:90
BOOL WINAPI DECLSPEC_HOTPATCH WriteConsoleA(IN HANDLE hConsoleOutput, IN CONST VOID *lpBuffer, IN DWORD nNumberOfCharsToWrite, OUT LPDWORD lpNumberOfCharsWritten, LPVOID lpReserved)
Definition: readwrite.c:1470
int main(int argc, char *argv[])
Definition: mutex.c:88
HANDLE WINAPI DECLSPEC_HOTPATCH CreateThread(IN LPSECURITY_ATTRIBUTES lpThreadAttributes, IN DWORD dwStackSize, IN LPTHREAD_START_ROUTINE lpStartAddress, IN LPVOID lpParameter, IN DWORD dwCreationFlags, OUT LPDWORD lpThreadId)
Definition: thread.c:111
smooth NULL
Definition: ftsmooth.c:416
char * va_list
Definition: acmsvcex.h:78
HANDLE hMutex
Definition: mutex.c:11
#define STD_INPUT_HANDLE
Definition: winbase.h:264
HANDLE hThread[2]
Definition: mutex.c:9
#define WAIT_ABANDONED_0
Definition: winbase.h:388
DWORD WINAPI thread1(LPVOID crap)
Definition: mutex.c:26
#define WINAPI
Definition: msvc.h:8
unsigned long DWORD
Definition: ntddk_ex.h:95
#define WAIT_FAILED
Definition: winbase.h:394
BOOL WINAPI DECLSPEC_HOTPATCH ReleaseMutex(IN HANDLE hMutex)
Definition: synch.c:564
static const WCHAR L[]
Definition: oid.c:1250
#define STD_OUTPUT_HANDLE
Definition: winbase.h:265
void dprintf(char *fmt,...)
Definition: mutex.c:14
int __cdecl vsprintf(char *_Dest, const char *_Format, va_list _Args)
Definition: sprintf.c:733
HANDLE WINAPI DECLSPEC_HOTPATCH CreateMutexW(IN LPSECURITY_ATTRIBUTES lpMutexAttributes OPTIONAL, IN BOOL bInitialOwner, IN LPCWSTR lpName OPTIONAL)
Definition: synch.c:522
#define va_start(ap, A)
Definition: acmsvcex.h:91
#define INFINITE
Definition: serial.h:102
#define args
Definition: format.c:66
Definition: dsound.c:943