Skip to content

Instantly share code, notes, and snippets.

@hucancode
Created August 25, 2022 00:26
Show Gist options
  • Save hucancode/21bee9142bbbe48436b0a5a0ea95f985 to your computer and use it in GitHub Desktop.
Save hucancode/21bee9142bbbe48436b0a5a0ea95f985 to your computer and use it in GitHub Desktop.
c++ thread demo
#include "stdafx.h"
#include <iostream>
#include <thread>
#include <mutex>
using namespace std;
int thread_count = 0;
const int max_thread = 200;
const int n = 200000;
int a[n];
int b[n];
void print(int i, int j)
{
for (; i <= j; i++)
{
printf("%d ", b[i]);
}
printf("\n");
}
void generate()
{
for (int i = 0; i < n; i++)
{
a[i] = rand() % 100000;
}
memcpy(b, a, n*sizeof(int));
}
void merge(int left, int mid1, int mid2, int right)
{
int i = left;
int j = mid2;
memcpy(b + left, a + left, (right - left + 1)*sizeof(int));
int k = i;
while (i <= mid1 || j <= right)
{
if (i > mid1)
{
b[k++] = a[j++];
}
else if (j > right)
{
b[k++] = a[i++];
}
else if (a[i] < a[j])
{
b[k++] = a[i++];
}
else
{
b[k++] = a[j++];
}
}
memcpy(a + left, b + left, (right - left + 1)*sizeof(int));
}
void sort(int i, int j)
{
if (i >= j)
{
return;
}
int u = (i + j)*0.5f;
int v = u + 1;
bool use_thread = thread_count < max_thread;
if (use_thread)
{
mutex lock;
lock.lock();
{
thread_count += 2;
}
lock.unlock();
thread left(sort, i, u);
thread right(sort, v, j);
left.join();
right.join();
lock.lock();
{
thread_count -= 2;
}
lock.unlock();
}
else
{
sort(i, u);
sort(v, j);
}
merge(i, u, v, j);
}
int _tmain(int argc, _TCHAR* argv[])
{
printf("generating array of %d members...\n", n);
generate();
printf("generation finished.\nsorting started, with max %d threads used\n", max_thread);
clock_t start = clock();
sort(0, n - 1);
clock_t timeElapsed = clock() - start;
printf("sorting finished, execution time = %ld miliseconds \n", timeElapsed);
system("pause");
return 0;
}
#include <thread>
#include <vector>
#include <iostream>
#include <mutex>
#include <string>
using namespace std;
class Map
{
private:
int data[100];
public:
Map()
{
memset(data, 0, 100 * sizeof(int));
}
void build()
{
for (int i = 0; i < 100; i++)
{
if (data[i] < 100)
{
data[i] = 100;
}
}
}
};
//mutex g_lock;
vector<Map*> maps;
int map_count = 100;
int download_interupt_at = 10;
void interupt_download()
{
mutex g_lock;
g_lock.lock();
int size = maps.size();
g_lock.unlock();
while (size < download_interupt_at)
{
g_lock.lock();
size = maps.size();
g_lock.unlock();
this_thread::sleep_for(chrono::milliseconds(100));
}
g_lock.lock();
cout << "interupt_download: download interupted at " << size << endl;
map_count = size;
g_lock.unlock();
}
void download_map()
{
mutex g_lock;
int downloaded = 0;
while (downloaded < map_count)
{
g_lock.lock();
Map* map = new Map();
maps.push_back(map);
cout << "download_map: downloaded " << downloaded << " maps" << endl;
downloaded++;
g_lock.unlock();
this_thread::sleep_for(chrono::milliseconds(400));
}
g_lock.lock();
cout << "download_map: done" << endl;
g_lock.unlock();
}
void build_map()
{
mutex g_lock;
int i = 0;
while (i < map_count)
{
g_lock.lock();
int size = maps.size();
g_lock.unlock();
if (i >= size)
{
g_lock.lock();
cout << "build_map: maps are being downloaded..." << endl;
g_lock.unlock();
this_thread::sleep_for(chrono::milliseconds(100));
continue;
}
g_lock.lock();
Map* map = maps[i];
g_lock.unlock();
map->build();
g_lock.lock();
cout << "build_map: map "<<i<<" are built." << endl;
g_lock.unlock();
i++;
}
g_lock.lock();
cout << "build_map: done" << endl;
g_lock.unlock();
}
int _tmain(int argc, _TCHAR* argv[])
{
thread download_thread(download_map);
thread build_thread(build_map);
thread interupt_thread(interupt_download);
interupt_thread.join();
download_thread.join();
build_thread.join();
system("pause");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment