首页 > 其他分享 >多线程测试

多线程测试

时间:2022-11-10 09:35:38浏览次数:32  
标签:多线程 测试 void 打印 线程 随机数 include

编写多线程程序,主线程中开启两个线程,一个线程打印十个偶数随机数,一个线程打印十个奇数随机数.
代码如下:
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
 
void *mythread1(void)
{
   int i,j,n=0;
   
    for(i=0;i<50;i++)
    {
      j=rand()%100;
      if(j % 2==0){
        printf("This is the 1st pthread,the number is %d\n",j);
        n++;
        }
      sleep(1);
          if(n==10)
             {
             return 0;
             }
        }
}        
void *mythread2(void){
   int i,j,n=0;
    for(i=0;i<50;i++)
    {
      j=rand()%100;
      if(j % 2!=0){
        printf("This is the 2nd pthread,the number is %d\n",j);
        n++;
        }
      sleep(1);
      if(n==10)
             {
             return 0;
             }
        }
}
 
int main(int argc, const char *argv[])
{
    int i = 0;
    int ret = 0;
    pthread_t id1,id2;
 
    ret = pthread_create(&id1, NULL, (void *)mythread1,NULL);
    if(ret)
    {
        printf("Create pthread error!\n");
        return 1;
    }
 
    ret = pthread_create(&id2, NULL, (void *)mythread2,NULL);
    if(ret)
    {
        printf("Create pthread error!\n");
        return 1;
    }
    
    pthread_join(id1,NULL);
    pthread_join(id2,NULL);
 
    return 0;
}

运行结果如图:

 

标签:多线程,测试,void,打印,线程,随机数,include
From: https://www.cnblogs.com/syf0105/p/16876009.html

相关文章

  • 多线程
    编写多线程程序,主线程中开启两个线程,一个线程打印十个偶数随机数,一个线程打印十个奇数随机数.......
  • 20201317 读者-写者(多线程)问题的思考与研究
    操作系统中"读者-写者"问题1.OS“读者-写者”问题,理解问题的本质,提交你理解或查找到的文本资料1、允许多个读者可以同时对文件执行读操作。2、只允许一个写者往文件中......
  • 进程间通信测试-signal
    任务详情基于sigaction编写一个信号处理程序,按下CTRL+C,打印奇数随机数,按下CTRL+\,打印偶数随机数,提交代码和运行结果截图。代码点击查看代码#include <stdio.h>#inclu......
  • 20201317-Linux-Thread 互斥测试
    #include<stdio.h>#include<stdlib.h>#include<pthread.h>//linux线程库#include<ctype.h>//测试和映射字符的库structarg_set{ char*fname; int......
  • 多进程测试
    推荐在OpenEuler中实现编写程序rxx(xx为你学号后两位),rxx-o生成并打印一个奇数随机数,rxx-e生成并打印一个偶数随机数。提交代码和运行结果截图。编写一个多进......
  • 多线程测试
    截图代码#include<stdio.h>#include<pthread.h>void*Thread1(void*arg){printf("线程1:");intn=0;intA[10]={-1};intTemp=-1;......
  • 《上海悠悠接口自动化平台》-5.测试计划与定时任务
    前言一个项目的自动化用例写完后,会根据不同的需求,定制不同的测试计划,可以运行整个项目的用例也可以运行自定义的测试计划用例。创建测试计划我的项目下有2个测试用例集......
  • 进程间通信测试-signal
    基于sigaction编写一个信号处理程序,按下CTRL+C,打印奇数随机数,按下CTRL+\,打印偶数随机数,提交代码和运行结果截图。#include <stdio.h>#include<string.h>#include <s......
  • 多进程测试
    1.编写程序rxx(xx为你学号后两位),rxx-o生成并打印一个奇数随机数,rxx-e生成并打印一个偶数随机数。提交代码和运行结果截图。代码: #include<stdio.h> #incl......
  • C# GUI(Winform)测试
    测试连接Mysql数据库参考博客:VisualStudio2017,C#winform项目连接Mysql数据库代码如下:usingMySql.Data.MySqlClient;privatevoidbutton1_Click(objectsender,Ev......