cond.c
来自「linux下的多线程调试工具」· C语言 代码 · 共 46 行
C
46 行
#define _XOPEN_SOURCE 600#include <pthread.h>#include <stdio.h>#include <time.h>#include "alias.h"struct timespec wait = {0, 10000000};struct timespec rem;#define WAIT nanosleep (&wait, &rem)pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;pthread_cond_t condition;FILE *fd;void *func () { new_thread_alias (fd, "func"); pthread_cond_wait (&condition, &mutex); pthread_cond_wait (&condition, &mutex);}int main () { pthread_t thread; pthread_condattr_t attr; fd = alias_init ("alias"); new_thread_alias (fd, "main"); new_object_alias (fd, &condition, "condition"); new_object_alias (fd, &mutex, "mutex"); pthread_cond_init (&condition, &attr); pthread_create (&thread, NULL, func, NULL); WAIT; pthread_cond_signal (&condition); WAIT; pthread_cond_broadcast (&condition); WAIT; pthread_cond_signal (&condition); pthread_cond_broadcast (&condition); pthread_join (thread, NULL); pthread_cond_destroy (&condition); return 0;}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?