瀏覽代碼

test: thread tests

simon 10 月之前
父節點
當前提交
64ec69d672
共有 1 個文件被更改,包括 20 次插入0 次删除
  1. 20 0
      test/thread_test.c

+ 20 - 0
test/thread_test.c

@@ -0,0 +1,20 @@
+// /usr/bin/gcc -g -Wall -Wextra -Werror -std=c17 -o thread_test.bin thread_test.c -lpthread
+#include <stdio.h>
+#include <pthread.h>
+#include <unistd.h>
+
+void *thread_function(void *_arg)
+{
+    unsigned int *arg = (unsigned int *)_arg;
+    printf(" new thread: my tid is %u\n", *arg);
+    return NULL;
+}
+
+int main()
+{
+    pthread_t thread;
+    pthread_create(&thread, NULL, thread_function, &thread); // pass the thread id to the new thread
+    printf("main thread: my tid is %lu\n", (unsigned long)pthread_self());
+    usleep(2000);
+    return 0;
+}