Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <thread>
- #include <conio.h>
- #include <Windows.h>
- using namespace std;
- #define MAX_THREADS 100
- CRITICAL_SECTION cs;
- void task(void)
- {
- while (true)
- {
- char *buffer;
- EnterCriticalSection(&cs);
- buffer = (char *)malloc(4096);
- LeaveCriticalSection(&cs);
- if (buffer == NULL)
- {
- cout << "malloc error" << endl;
- }
- EnterCriticalSection(&cs);
- free(buffer);
- LeaveCriticalSection(&cs);
- }
- }
- int main(int argc, char** argv)
- {
- if (!InitializeCriticalSectionAndSpinCount(&cs, 0x00000400))
- {
- printf("Error\n");
- _getch();
- }
- thread some_threads[MAX_THREADS];
- for (int i = 0; i < MAX_THREADS; i++)
- {
- some_threads[i] = thread(task);
- }
- for (int i = 0; i < MAX_THREADS; i++)
- {
- some_threads[i].join();
- }
- _getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement