2
0

huge-threads.cpp 955 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /*
  2. g++ huge-threads.cpp ../../objs/st/libst.a -g -O0 -o huge-threads && ./huge-threads 60000
  3. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include "../../objs/st/st.h"
  7. void* pfn(void* arg) {
  8. char v[32*1024]; // 32KB in stack.
  9. for (;;) {
  10. v[0] = v[sizeof(v) - 1] = 0xf;
  11. st_usleep(1000 * 1000);
  12. }
  13. return NULL;
  14. }
  15. int main(int argc, char** argv) {
  16. if (argc < 2) {
  17. printf("Usage: %s nn_coroutines [verbose]\n", argv[0]);
  18. exit(-1);
  19. }
  20. st_init();
  21. int nn = ::atoi(argv[1]);
  22. printf("pid=%d, create %d coroutines\n", ::getpid(), nn);
  23. for (int i = 0; i < nn; i++) {
  24. st_thread_t thread = st_thread_create(pfn, NULL, 1, 0);
  25. if (!thread) {
  26. printf("create thread fail, i=%d\n", i);
  27. return -1;
  28. }
  29. if (argc > 2) {
  30. printf("thread #%d: %p\n", i, thread);
  31. }
  32. }
  33. printf("done\n");
  34. st_thread_exit(NULL);
  35. return 0;
  36. }