⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 multithread.c

📁 rtlinux下面的多线程程序
💻 C
字号:
#include <rtl.h>#include <time.h>//#include <rtl_time.h>#include <pthread.h>pthread_t thread0;pthread_t thread1;pthread_t thread2;int i,j,k;void * start_routine1(void *arg){   pthread_make_periodic_np (pthread_self(), gethrtime(), 500000);     for(i=0;i<10;i++)   {      pthread_wait_np ();     // rtl_printf("now i = %d\n",i);   }   rtl_printf("I am the first thread! now i = %d\n",i);   return 0;}void * start_routine2(void *arg){   pthread_make_periodic_np (pthread_self(), gethrtime(), 1000000);     for(j=0;j<20;j++)   {      pthread_wait_np ();     // rtl_printf("now j = %d\n",j);   }   rtl_printf("I am the second thread! now j = %d\n",j);   return 0;}void * start_routine3(void *arg){   pthread_make_periodic_np (pthread_self(), gethrtime(), 1500000);     for(k=0;k<30;k++)   {      pthread_wait_np ();      //rtl_printf("now k = %d\n",k);   }   rtl_printf("I am the third thread! now k = %d\n",k);   return 0;}int init_module(void){pthread_attr_t attr;struct sched_param sched_param;int ret;pthread_attr_init (&attr);sched_param.sched_priority = 1;pthread_attr_setschedparam (&attr, &sched_param);ret = pthread_create (&thread0, &attr, start_routine1, (void *)1);if(ret){ rtl_printf("failed to create thread 0!\n"); return -1;}else{  rtl_printf("create thread 0 success!\n");}pthread_attr_init (&attr);sched_param.sched_priority = 2;pthread_attr_setschedparam (&attr, &sched_param);ret = pthread_create (&thread1, &attr, start_routine2, (void *)2);if(ret){ rtl_printf("failed to create thread 1!\n"); return -1;}else{  rtl_printf("create thread 1 success!\n");}pthread_attr_init (&attr);sched_param.sched_priority = 3;pthread_attr_setschedparam (&attr, &sched_param);ret = pthread_create (&thread2, &attr, start_routine3, (void *)2);if(ret){ rtl_printf("failed to create thread 2!\n"); return -1;}else{  rtl_printf("create thread 2 success!\n");}return 0;}void cleanup_module(void) {pthread_cancel (thread0);pthread_join (thread0, NULL);pthread_cancel (thread1);pthread_join (thread1, NULL);pthread_cancel (thread2);pthread_join (thread2, NULL);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -