linux 多线程
#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include int tty_fd[3] = {0};pthread_t tty_pid[6] = {0};pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER;static void *thread_start_tty1(void *arg){ puts("-------thread1-------"); while(1) { pthread_mutex_lock(&g_mutex); puts("-------1----------"); tty_fd[0] = 1; sleep(3); pthread_mutex_unlock(&g_mutex); }}static void *thread_start_tty2(){ puts("--------thread2--------"); pthread_mutex_lock(&g_mutex); puts("--------2-----------"); tty_fd[1] = 2; sleep(3); pthread_mutex_unlock(&g_mutex);}static void *thread_start_tty3(void *arg){ puts("--------thread3-----------"); while(1) { }}void CreateUartPhtread(){ int ret = -1; ret = pthread_create(&tty_pid[0], NULL, &thread_start_tty1, NULL); if(0 != ret) { printf("create thread1 fail!\n"); return ; } ret = pthread_create(&tty_pid[1], NULL, &thread_start_tty2, NULL); if(0 != ret) { printf("create thread2 fail!\n"); return ; } ret = pthread_create(&tty_pid[2], NULL, &thread_start_tty3, NULL); if(0 != ret) { printf("create thread3 fail!\n"); return ; }/* ret = pthread_create(&tty_pid[3], NULL, &thread_start_ttys4_read, NULL); if(0 != ret) { printf("create thread4 fail!\n"); return ; } ret = pthread_create(&tty_pid[4], NULL, &thread_start_ttys3_read, NULL); if(0 != ret) { printf("create thread5 fail!\n"); return ; } ret = pthread_create(&tty_pid[5], NULL, &thread_start_ttys2_read, NULL); if(0 != ret) { printf("create thread6 fail!\n"); return ; }*/}int main(){ CreateUartPhtread(); while(1); return 0;}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
暂时没有评论,来抢沙发吧~