Tag: 加入

线程终止问题(c编程)

我正在使用C语言编写一个使用multithreading的Linux应用程序。 由main函数生成的线程完成大部分工作,因此通常最后完成。 我看到一些奇怪的行为,我相信这是由于主线程在生成的线程有机会完成其工作之前终止。 这里有一些示例代码来说明我在说什么: #define _POSIX_C_SOURCE 200112L #define _ISOC99_SOURCE #define __EXTENSIONS__ #define _GNU_SOURCE #include #include void my_cleanup(void *arg) { printf(“cleanup: %s\n”, (char *)arg); } void * thread_stuff(void *arg) { printf(“thread started\n”); pthread_cleanup_push(cleanup, “running”); if (arg) pthread_exit((void *)2); pthread_cleanup_pop(0); pthread_exit((void *)2); } int main() { int err; pthread_t tid1, tid2; err = pthread_create(&tid1, NULL, thread_stuff, (void […]