Skip to content

Instantly share code, notes, and snippets.

@sailist
Created March 13, 2021 12:50
Show Gist options
  • Save sailist/2dc5549919f374429db36f282b2aae76 to your computer and use it in GitHub Desktop.
Save sailist/2dc5549919f374429db36f282b2aae76 to your computer and use it in GitHub Desktop.
Windows MultiThread code
#include <windows.h>
#include <iostream>
using namespace std;
//Thread Function
DWORD WINAPI ThreadProc(LPVOID lpParameter)
{
for (int i = 0; i < 5; ++ i)
{
cout << "Thread:i = " << i << endl;
Sleep(100);
}
return 0L;
}
int main()
{
// Create Thread
HANDLE thread = CreateThread(NULL, 0, ThreadProc, NULL, 0, NULL);
//close Thread
CloseHandle(thread);
// MainThread
for (int i = 0; i < 5; ++ i)
{
cout << "MainThread:i = " << i << endl;
Sleep(100);
}
return 0;
}
#include <windows.h>
#include <iostream>
#define NAME_LINE 40
using namespace std;
// DataStructure passed in thread
typedef struct __THREAD_DATA
{
int nMaxNum;
char strThreadName[NAME_LINE];
__THREAD_DATA() : nMaxNum(0)
{
memset(strThreadName, 0, NAME_LINE * sizeof(char));
}
}THREAD_DATA;
HANDLE g_hMutex = NULL; //Define a Mutex Object
CRITICAL_SECTION cs;
// Thread Function
DWORD WINAPI ThreadProc(LPVOID lpParameter)
{
THREAD_DATA* pThreadData = (THREAD_DATA*)lpParameter;
for (int i = 0; i < pThreadData->nMaxNum; ++ i)
{
EnterCriticalSection(&cs);
cout << pThreadData->strThreadName << " --- " << i << endl;
Sleep(100);
LeaveCriticalSection(&cs);
}
return 0L;
}
int main()
{
// Initialize Mutex Object
InitializeCriticalSection(&cs);
//Initialize Threads data
THREAD_DATA threadData1, threadData2;
threadData1.nMaxNum = 5;
strcpy(threadData1.strThreadName, "Thread1");
threadData2.nMaxNum = 10;
strcpy(threadData2.strThreadName, "Thread2");
// Create Threads
HANDLE hThread1 = CreateThread(NULL, 0, ThreadProc, &threadData1, 0, NULL);
HANDLE hThread2 = CreateThread(NULL, 0, ThreadProc, &threadData2, 0, NULL);
// Close Threads
CloseHandle(hThread1);
CloseHandle(hThread2);
// Main Function
for (int i = 0; i < 5; ++ i)
{
// Request Mutex object
EnterCriticalSection(&cs);
cout << "MainThread === " << i << endl;
Sleep(100);
// Release Mutex object
LeaveCriticalSection(&cs);
}
system("pause");
return 0;
}
#include <windows.h>
#include <iostream>
#define NAME_LINE 40
using namespace std;
// DataStructure passed in thread
typedef struct __THREAD_DATA
{
int nMaxNum;
char strThreadName[NAME_LINE];
__THREAD_DATA() : nMaxNum(0)
{
memset(strThreadName, 0, NAME_LINE * sizeof(char));
}
}THREAD_DATA;
HANDLE g_hMutex = NULL; //Define a Mutex Object
// Thread Function
DWORD WINAPI ThreadProc(LPVOID lpParameter)
{
THREAD_DATA* pThreadData = (THREAD_DATA*)lpParameter;
for (int i = 0; i < pThreadData->nMaxNum; ++ i)
{
WaitForSingleObject(g_hMutex, INFINITE);
cout << pThreadData->strThreadName << " --- " << i << endl;
Sleep(100);
ReleaseMutex(g_hMutex);
}
return 0L;
}
int main()
{
// Initialize Mutex Object
g_hMutex = CreateMutex(NULL, FALSE, NULL);
//Initialize Threads data
THREAD_DATA threadData1, threadData2;
threadData1.nMaxNum = 5;
strcpy(threadData1.strThreadName, "Thread1");
threadData2.nMaxNum = 10;
strcpy(threadData2.strThreadName, "Thread2");
// Create Threads
HANDLE hThread1 = CreateThread(NULL, 0, ThreadProc, &threadData1, 0, NULL);
HANDLE hThread2 = CreateThread(NULL, 0, ThreadProc, &threadData2, 0, NULL);
// Close Threads
CloseHandle(hThread1);
CloseHandle(hThread2);
// Main Function
for (int i = 0; i < 5; ++ i)
{
// Request Mutex object
WaitForSingleObject(g_hMutex, INFINITE);
cout << "MainThread === " << i << endl;
Sleep(100);
// Release Mutex object
ReleaseMutex(g_hMutex);
}
system("pause");
return 0;
}
#include <windows.h>
#include <iostream>
using namespace std;
#define NAME_LINE 40
// DataStructure passed in thread
typedef struct __THREAD_DATA
{
int nMaxNum;
char strThreadName[NAME_LINE];
__THREAD_DATA() : nMaxNum(0)
{
memset(strThreadName, 0, NAME_LINE * sizeof(char));
}
}THREAD_DATA;
// Thread Function
DWORD WINAPI ThreadProc(LPVOID lpParameter)
{
THREAD_DATA* pThreadData = (THREAD_DATA*)lpParameter;
for (int i = 0; i < pThreadData->nMaxNum; ++ i)
{
cout << pThreadData->strThreadName << " --- " << i << endl;
Sleep(100);
}
return 0L;
}
int main()
{
// Initialize Threads Data
THREAD_DATA threadData1, threadData2;
threadData1.nMaxNum = 5;
strcpy(threadData1.strThreadName, "Thread1");
threadData2.nMaxNum = 10;
strcpy(threadData2.strThreadName, "Thread2");
// Create Threads
HANDLE hThread1 = CreateThread(NULL, 0, ThreadProc, &threadData1, 0, NULL);
HANDLE hThread2 = CreateThread(NULL, 0, ThreadProc, &threadData2, 0, NULL);
//Close Threads
CloseHandle(hThread1);
CloseHandle(hThread2);
//Main Function
for (int i = 0; i < 5; ++ i)
{
cout << "MainThread === " << i << endl;
Sleep(100);
}
system("pause");
return 0;
}
@sailist
Copy link
Author

sailist commented Mar 13, 2021

Reference: herehere

@sailist
Copy link
Author

sailist commented Mar 16, 2021

performence: here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment