// 编译方法:gcc cg_test.c -lpthread -o cg_test
#include <sys/syscall.h>
#include <stdio.h>
#include <pthread.h>
typedef struct thread_arg
{
int num;
} thread_arg;
void *my_thread_func(void* arg)
{
thread_arg* thd_arg = (thread_arg*)arg;
pid_t lwpid = syscall(SYS_gettid);
printf("[%d] New thread created. LWPID: %d\n", thd_arg->num, lwpid);
while (1)
{
usleep(2);
}
return NULL;
}
#define THD_NUM 2
int main()
{
pid_t pid = getpid();
pid_t lwpid = syscall(SYS_gettid);
printf("[M] Main thread. PID: %d, LWPID: %d\n", pid, lwpid);
pthread_t tids[THD_NUM];
thread_arg args[THD_NUM];
int i = 0;
for (i = 0; i < THD_NUM; ++i)
{
args[i].num = 0;
int ret = pthread_create(&tids[i], NULL, (void*)my_thread_func, &args[i]);
if (ret)
{
printf("Create thread error!\n");
return 1;
}
}
for (i = 0; i < THD_NUM; ++i)
{
pthread_join(tids[i], NULL);
}
return 0;
}