线程 pthread_t tid


1.创建一个多线程程序,至少有10个子线程, 每个线程有会打印不同的数据,同时表明身份

2. typedef strcut { float a; float b; char c;//+ - * / float d; }JSQ; 主线程,接收一个表达式,填充结构体,传递给th1线程,th1线程结算结果,并返回给主线程。主线程输出结果。

1)

  1 #include<stdio.h>
  2 #include<unistd.h>
  3 #include<string.h>
  4 #include<stdlib.h>
  5 #include<pthread.h>                                                                                                                                
  6 void *th(void *arg)
  7 {
  8     int tmp=*(int *)arg;
  9     printf("num=%d pid %lu\n",tmp,pthread_self());
 10     pthread_exit(0);
 11     return NULL;
 12 }
 13 int main(int argc, const char *argv[])
 14 {
 15     pthread_t tid[10]={0};
 16     int i=0;
 17     for(i=0;i<10;i++)
 18     {
 19         int *p=(int *)malloc(sizeof(int));
 20         *p=i+1;
 21         pthread_create(&tid[i],NULL,th,p);
 22     }
 23     for(i=0;i<10;i++)
 24     {
 25         pthread_join(tid[i],NULL);
 26     }
 27     return 0;
 28 }
~                                                                                                                                                      
~                                                                                                                                                      
~      
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
typedef struct
{
    float a;
    float b;
    char c;//+ - * /
    float d;
}JSQ;
void* th3(void*arg)
{
    printf("subthread id%lu\n",pthread_self());
    JSQ*tmp = (JSQ*)arg;
    switch(tmp->c)
    {
        case '+' :
            tmp->d  = tmp->a+tmp->b;
            break;
        case '-' :
            tmp->d  = tmp->a-tmp->b;
            break;
        case '*' :
            tmp->d  = tmp->a*tmp->b;
            break;
        case '/' :
            tmp->d  = tmp->a/tmp->b;
            break;
    }

    pthread_exit(tmp);
}
int main(int argc, char *argv[])
{
    int i = 0 ;
    pthread_t tid;
    JSQ jsq;
    // 2.3 + 5.4  atof;
    jsq.a  =2.3;
    jsq.b  = 5.4;
    jsq.c = '+';

    pthread_create(&tid,NULL,th3,&jsq);
    void* ret;
    pthread_join(tid,&ret);
    printf("result is %f\n", ((JSQ*)ret)->d);
    return 0;
}
  1 #include<stdio.h>                                                                                                                        
  2 #include<unistd.h>
  3 #include<string.h>
  4 #include<stdlib.h>
  5 #include<pthread.h>
  6 
  7 
  8 typedef struct
  9 {
 10     float a;
 11     float b;
 12     char c;
 13     float d;
 14 }JSQ;
 15 JSQ jsq;
 16 float  result;
 17 void *th(void *arg)
 18 {
 19     switch(jsq.c)
 20     {
 21     case '+':
 22         jsq.d=jsq.a + jsq.b;
 23         break;
 24     case '-':
 25         jsq.d=jsq.a - jsq.b;
 26         break;
 27     case '*':
 28         jsq.d=jsq.a * jsq.b;
 29         break;
 30     case '/':
 31         jsq.d=jsq.a / jsq.b;
 32         break;
 33     }
 34     result = jsq.d;
 35 }
 36 
 37 int main(int argc, const char *argv[])
 38 {
 39     pthread_t tid;
 40     printf("输入公式\n");
 41     scanf("%f%c%f",&jsq.a,&jsq.c,&jsq.b);
 42     pthread_create(&tid,NULL,th,NULL);
 43     pthread_join(tid,NULL);
 44     printf("result = %f\n",result);
 45     return 0;
 46 }
 47 

 

9c2620a7363a48d6b54fc4fe57380e6f.png

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值