首页 > 编程知识 正文

c语言之多线编程,c++多线程编程实例

时间:2023-12-29 13:16:59 阅读:330422 作者:BVMX

本文目录一览:

C语言多线程编程为什么要用pthread

3个线程使用的都是同一个info 代码 Info_t *info = (Info_t *)malloc(sizeof(Info_t));只创建了一个info pthread_create(threads[i],NULL,calMatrix,(void *)info); 三个线程使用的是同一个 我把你的代码改了下

c语言编程问题题目,给定3条线的长度,求出用这3条线组成的三角形的面积

#include stdio.h

#include math.h

int main()

{

    int a,b,c;

    double area,p;

    while(scanf("%d%d%d",a,b,c)==3)

    {

        if(a+bcb+caa+cb)

        {

            p=(a+b+c)/2.0;

            area=sqrt(p*(p-a)*(p-b)*(p-c));

        }

        else

        {

            area=-1;

        }

        printf("%.0fn",area);

    }

    return 0;

}

在C语言的多线程编程中一般volatile应该用在什么地方?

一般用在多线程程序中,由于某个变量可能被多个线程修改,因此,修饰为volatile,使其每次读取,都是从存储volatile变量的地址中去取,而不是取寄存器中的值。

volatile int a;

你的这种设想,都是最好要用volatile的地方,只要这个变量被反复频繁的修改,最好用volatile

C语言程序编程

10.

#includestdio.h

main()

{

int i,x,y,z;

printf("所有的水仙花数为:n");

for(i=100;i1000;i++)

{

x=i/100;y=i/10%10;z=i%10;

if(i==x*x*x+y*y*y+z*z*z)

printf("%5d",i);

}

printf("n");

}

c语言多进程编程

多进程这个词用得比较少,听过来有点不熟悉。你这个程序在linux下应该很容易实行,就是个进程间通信的问题,管道、消息队列、共享内存都可以,可以找找相关资料。昨天失言不好意思。

三个源文件分别为1.c、2.c、3.c一个头文件share.h。

share.h:

//共享的内存,两个数组

typedef struct{

int a[2];

int b[2];

int id;

}share_use;

1.c:

#includeunistd.h

#includestdio.h

#includestdlib.h

#includestring.h

#includesys/types.h

#includesys/ipc.h

#includesys/shm.h

#include"share.h"

int main(){

void *shared_memory = (void *)0;

share_use *share_stuff;

int shmid;

shmid=shmget((key_t)1234,sizeof(share_use),0666|IPC_CREAT);//创建共享内存

if(shmid==-1){

fprintf(stderr,"共享内存创建失败!n");

exit(1);

}

shared_memory = shmat(shmid, (void *)0,0);//让进程可以访问共享内存

if(shared_memory == (void *)-1){

fprintf(stderr,"启用共享内存失败!n)";

exit(1);

}

printf("Memory attached at %Xn",(int)shared_memory);

share_stuff = (share_use *)shared_memory;

share_stuff-id=0;

share_stuff-a[0]=1;

share_stuff-a[1]=2;

while(1){

if(share_stuff-id)

exit(0);

sleep(10);

}

}

2.c:

#includeunistd.h

#includestdio.h

#includestdlib.h

#includestring.h

#includesys/types.h

#includesys/ipc.h

#includesys/shm.h

#include"share.h"

int main(){

void *shared_memory = (void *)0;

share_use *share_stuff;

int shmid;

shmid=shmget((key_t)1234,sizeof(share_use),0666|IPC_CREAT);//创建共享内存

if(shmid==-1){

fprintf(stderr,"共享内存创建失败!n");

exit(1);

}

shared_memory = shmat(shmid, (void *)0,0);//让进程可以访问共享内存

if(shared_memory == (void *)-1){

fprintf(stderr,"启用共享内存失败!n");

exit(1);

}

printf("Memory attached at %Xn",(int)shared_memory);

share_stuff = (share_use *)shared_memory;

share_stuff-b[0]=share_stuff-a[0]*100;

share_stuff-b[1]=share_stuff-a[1]*100;

while(1)

{

if(share_stuff-id)

exit(0);

sleep(10);

}

}

3.c:

#includeunistd.h

#includestdio.h

#includestdlib.h

#includestring.h

#includesys/types.h

#includesys/ipc.h

#includesys/shm.h

#include"share.h"

int main(){

void *shared_memory = (void *)0;

share_use *share_stuff;

int shmid;

shmid=shmget((key_t)1234,sizeof(share_use),0666|IPC_CREAT);//创建共享内存

if(shmid==-1){

fprintf(stderr,"共享内存创建失败!n");

exit(1);

}

shared_memory = shmat(shmid, (void *)0,0);//让进程可以访问共享内存

if(shared_memory == (void *)-1){

fprintf(stderr,"启用共享内存失败!n");

exit(1);

}

printf("Memory attached at %Xn",(int)shared_memory);

share_stuff = (share_use *)shared_memory;

printf("共享内存中有元素:%d , %d",share_stuff-b[0],share_stuff-b[1]);

share_stuff-id=1;

return 0;

}

linux或unix环境下编译

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。