-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
de1fdde
commit 39f6871
Showing
7 changed files
with
170 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"configurations": [ | ||
{ | ||
"name": "Win32", | ||
"includePath": [ | ||
"${workspaceFolder}/**" | ||
], | ||
"defines": [ | ||
"_DEBUG", | ||
"UNICODE", | ||
"_UNICODE" | ||
], | ||
"compilerPath": "E:\\cpp\\mingw64\\bin\\gcc.exe", | ||
"cStandard": "c17", | ||
"cppStandard": "gnu++14", | ||
"intelliSenseMode": "windows-gcc-x64" | ||
} | ||
], | ||
"version": 4 | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
{ | ||
"files.associations": { | ||
"dirent.h": "c", | ||
"unordered_map": "cpp" | ||
} | ||
"unordered_map": "cpp", | ||
"cstdio": "c", | ||
"thread.h": "c" | ||
}, | ||
"C_Cpp.errorSquiggles": "disabled" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#include "thread.h" | ||
|
||
void T_a() { | ||
while (1) { | ||
printf("a"); | ||
} | ||
} | ||
|
||
void T_b() { | ||
while (1) { | ||
printf("b"); | ||
} | ||
} | ||
|
||
// pthead库不是标准linux库 -> 执行gcc thread.c -lpthread | ||
int main() { | ||
create(T_a); | ||
create(T_b); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#include "thread.h" | ||
|
||
|
||
// 多个线程之间是共享内存 | ||
int n; | ||
|
||
void T_hello(int id) { | ||
int i = n++; | ||
printf("%d\n", i); | ||
} | ||
|
||
int main() { | ||
for (int i = 0; i < 10; i++) { | ||
create(T_hello); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#include "thread.h" | ||
|
||
|
||
#define N 4 | ||
|
||
char * volatile low[N]; | ||
char * volatile high[N]; | ||
|
||
|
||
void update_range(int T, char *ptr) { | ||
if (ptr < low[T]) { | ||
low[T] = ptr; | ||
} | ||
if (ptr > high[T]) { | ||
high[T] = ptr; | ||
} | ||
} | ||
|
||
|
||
void probe(int T, int n) { | ||
char scratch[64]; | ||
update_range(T, scratch); | ||
|
||
printf("Stack(T%d) >= %ld KB\n", T, (high[T] - low[T]) / 1024); | ||
|
||
probe(T, n + 1); | ||
} | ||
|
||
void T_probe(int T) { | ||
T -= 1; | ||
low[T] = (char *)-1; | ||
high[T] = (char *)0; | ||
probe(T, 0); | ||
} | ||
|
||
int main() { | ||
setbuf(stdout, NULL); | ||
|
||
for (int i = 0; i < N; i++) { | ||
create(T_probe); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#include <stdlib.h> | ||
#include <stdio.h> | ||
#include <string.h> | ||
#include <assert.h> | ||
#include <unistd.h> | ||
#include <pthread.h> | ||
|
||
|
||
#define LENGTH(arr) (sizeof(arr) / sizeof(arr[0])) | ||
|
||
enum { | ||
T_FREE = 0, // This slot is not used yet. | ||
T_LIVE, // This thread is running. | ||
T_DEAD, // This thread has terminated. | ||
}; | ||
|
||
struct thread { | ||
int id; // Thread number: 1, 2, ... | ||
int status; // Thread status: FREE/LIVE/DEAD | ||
pthread_t thread; // Thread struct | ||
void (*entry)(int); // Entry point | ||
}; | ||
|
||
static struct thread threads_[4096]; | ||
static int n_ = 0; | ||
|
||
static inline | ||
void *wrapper_(void *arg) { | ||
struct thread *t = (struct thread *)arg; | ||
t->entry(t->id); | ||
return NULL; | ||
} | ||
|
||
// Create a thread that calls function fn. | ||
static inline | ||
void create(void *fn) { | ||
assert(n_ < LENGTH(threads_)); | ||
|
||
threads_[n_] = (struct thread) { | ||
.id = n_ + 1, | ||
.status = T_LIVE, | ||
.entry = fn, | ||
}; | ||
pthread_create( | ||
&(threads_[n_].thread), | ||
NULL, | ||
wrapper_, | ||
&threads_[n_] | ||
); | ||
n_++; | ||
}; | ||
|
||
static inline | ||
void join() { | ||
for (int i = 0; i < LENGTH(threads_); i++) { | ||
struct thread *t = &threads_[i]; | ||
if (t->status == T_LIVE) { | ||
pthread_join(t->thread, NULL); | ||
t->status = T_DEAD; | ||
} | ||
} | ||
} | ||
|
||
// Join all threads when main() returns; | ||
__attribute__((destructor)) | ||
static void cleanup() { | ||
join(); | ||
} |