×

c多线程编程

c多线程编程(宏程序编程)

admin admin 发表于2023-04-10 07:05:09 浏览51 评论0

抢沙发发表评论

本文目录一览:

c语言 多线程套接字编程

#include stdlib.h

#include stdio.h

#include errno.h

#include string.h

#include sys/types.h

#include netinet/in.h

#include sys/wait.h

#include sys/socket.h

#define PORT 5000 // The port which is communicate with server

#define BACKLOG 10

#define LENGTH 512 // Buffer length -c多线程编程

int main ()

{ int sockfd; // Socket file descriptor

int nsockfd; // New Socket file descriptor

int num;

int sin_size; // to store struct size

char sdbuf[LENGTH]; // Send buffer

struct sockaddr_in addr_local;

struct sockaddr_in addr_remote;

char sendstr[16]= {"123456789 abcde"};

/* Get the Socket file descriptor */

if( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1 )

{

printf ("ERROR: Failed to obtain Socket Despcritor.\n");

return (0);

}

else

{

printf ("OK: Obtain Socket Despcritor sucessfully.\n");

}

/* Fill the local socket address struct */

addr_local.sin_family = AF_INET; // Protocol Family

addr_local.sin_port = htons(PORT); // Port number

addr_local.sin_addr.s_addr = INADDR_ANY; // AutoFill local address

bzero((addr_local.sin_zero), 8); // Flush the rest of struct

/* Blind a special Port */

if( bind(sockfd, (struct sockaddr*)addr_local, sizeof(struct sockaddr)) == -1 )

{

printf ("ERROR: Failed to bind Port %d.\n",PORT);

return (0);

}

else

{

printf("OK: Bind the Port %d sucessfully.\n",PORT);

}

/* Listen remote connect/calling */

if(listen(sockfd,BACKLOG) == -1)

{

printf ("ERROR: Failed to listen Port %d.\n", PORT);

return (0);

}

else

{

printf ("OK: Listening the Port %d sucessfully.\n", PORT);

}

while(1)

{

sin_size = sizeof(struct sockaddr_in);

/* Wait a connection, and obtain a new socket file despriptor for single connection */

if ((nsockfd = accept(sockfd, (struct sockaddr *)addr_remote, sin_size)) == -1)

{

printf ("ERROR: Obtain new Socket Despcritor error.\n");

continue;

}

else

{

printf ("OK: Server has got connect from %s.\n", inet_ntoa(addr_remote.sin_addr));

}

/* Child process */

if(!fork())

{

printf("You can enter string, and press 'exit' to end the connect.\n");

while(strcmp(sdbuf,"exit") != 0)

{

scanf("%s", sdbuf);

if((num = send(nsockfd, sdbuf, strlen(sdbuf), 0)) == -1)

{

printf("ERROR: Failed to sent string.\n");

close(nsockfd);

exit(1);

}

printf("OK: Sent %d bytes sucessful, please enter again.\n", num);

}

}

close(nsockfd);

while(waitpid(-1, NULL, WNOHANG) 0);

}

}

c语言怎么同时运行4段

可以使用多线程的办法,同时运行的方法如下:

1)使用void*myfunc(void*args){;

2)在intmain(){limian写四组pthread,pthread_create(),pthread_join();

3)最后return0。

多线程(multithreading),是指从软件或者硬件上实现多个线程并发执行的技术。具有多线程能力的计算机因有硬件支持而能够在同一时间执行多于一个线程,进而提升整体处理性能。具有这种能力的系统包括对称多处理机、多核心处理器以及芯片级多处理或同时多线程处理器。在一个程序中,这些独立运行的程序片段叫作“线程”(Thread),利用它编程的概念就叫作“多线程处理”。-c多线程编程

程序语言有多种分类方法,大部分程序语言都是算法描述型语言,如C/C++、Java等,还有一部分是数据描述型语言,如HTML等标记语言。按照编程技术难易程度可分为低级语言(机器语言、汇编语言)和高级语言;按照程序语言设计风格可分为命令式语言(过程化语言)、结构化语言、面向对象语言、函数式语言、脚本语言等;按照语言应用领域可分为通用程序语言(GPPL)和专用程序语言(DSL);按照程序执行方式,可分为解释型语言(如JavaScript、Python、Perl、R等),编译型语言(如C/C++等),编译+解释型语言(如Java、PHP等)。-c多线程编程

c/c++多线程编程中,为什么基本类型不用加锁

先 你描述的不对 严格来说是在对应的cpu体系下遵循了正确的内存对齐的方式的数据才有不需要加锁的可能

加锁主要是防止partial read/.1;write 请参考《intel开发者手册》第三卷8;write以及cpu乱序带来的cpu操作可见性问题

举x86-64的例子来说 一个基本类型如果地址无cache splite(落在两个cacheline中) 都可以保证无partial read/.1

linux系统下,c语言pthread多线程编程传参问题

3个线程使用的都是同一个info

代码 Info_t *info = (Info_t *)malloc(sizeof(Info_t));只创建了一个info

pthread_create(threads[i],NULL,calMatrix,(void *)info); 三个线程使用的是同一个

我把你的代码改了下:

#include stdio.h

#include stdlib.h

#include pthread.h

int mtc[3] = { 0 }; // result matrix

typedef struct

{

    int prank;

    int *mta; 

    int *mtb;

}Info_t;

void* calMatrix(void* arg)

{

    int i;

    Info_t *info = (Info_t *)arg;

    int prank = info-prank;

    fprintf(stdout,"calMatrix : prank is %d\n",prank);

    for(i = 0; i  3; i++)

        mtc[prank] += info-mta[i] * info-mtb[i];

    return NULL;

}

int main(int argc,char **argv)

{

    int i,j,k = 0;

    int mta[3][3];

    int mtb[3] = { 1 };

    Info_t *info = (Info_t *)malloc(sizeof(Info_t)*3);

    for(i = 0; i  3; i++)

        for(j = 0; j  3; j++)

            mta[i][j] = k++;

    /* 3 threads */

    pthread_t *threads = (pthread_t *)malloc(sizeof(pthread_t)*3);

    fprintf(stdout,"\n");fflush(stdout);

    for(i = 0; i  3; i++)

    {

        info[i].prank = i;

        info[i].mta = mta[i];

        info[i].mtb = mtb;

        pthread_create(threads[i],NULL,calMatrix,(void *)(info[i]));

    }

    for(i = 0; i  3; i++)

        pthread_join(threads[i],NULL);

    fprintf(stdout,"\n==== the matrix result ====\n\n");

    fflush(stdout);

    for(i = 0; i  3; i++)

    {

        fprintf(stdout,"mtc[%d] = %d\n",i,mtc[i]);

        fflush(stdout);

    }

    return 0;

}

矩阵的计算我忘记了,你运行看看结果对不对

c++ 多线程编程常用的几个函数

1、C++多线程也可以使用UNIX C的库函数,pthread_mutex_t,pthread_create,pthread_cond_t,pthread_detach,pthread_mutex_lock/unlock,等等。在使用多线程的时候,你需要先创建线程,使用pthread_create,你可以使主线程等待子线程使用pthread_join,也可以使线程分离,使用pthread_detach。线程使用中最大的问题就是同步问题,一般使用生产着消费者模型进行处理,使用条件变量pthread_cond_t,pthread_mutex,pthread_cond_wait来实现。-c多线程编程

2、例程:

//创建5个线程

#include pthread.h

#include stdlib.h

void* work_thread(void* arg)

{

//线程执行体

return 0;

}

int main(int argc,char* argv[])

{

int nthread = 5;//创建线程的个数

pthread_t tid;//声明一个线程ID的变量;

for(int i=0;inthread;i++)

{

pthread_create(tid,NULL,work_thread,NULL);

}

sleep(60);//睡眠一分钟,你可以看下线程的运行情况,不然主进程会很快节结束了。

}

pthread_create(tid,NULL,work_thread,NULL);//创建线程的函数,第一个参数返回线程的ID;第二个参数是线程的属性,一般都置为NULL;第三个参数是线程函数,线程在启动以后,会自动执行这个函数;第四个参数是线程函数的参数,如果有需要传递给线程函数的参数,可以放在这个位置,可以是基础类型,如果你有不止一个参数想传进线程函数,可以做一个结构体,然后传入。-c多线程编程

关于C++多线程编程教学

在Windows NT和Windows 9x中,多线程的编程实现需要调用一系列的API函数,如CreateThread、ResumeThread等,比较麻烦而且容易出错。我们使用Inprise公司的新一代RAD开发工具C++Builder,可以方便地实现多线程的编程。与老牌RAD工具Visual Basic和Delphi比,C++Builer不仅功能非常强大,而且它的编程语言是C++,对于系统开发语言是C的Windows系列操作系统,它具有其它编程语言无可比拟的优势。利用C++Builder提供的TThread对象,多线程的编程变得非常简便易用。那么,如何实现呢?且待我慢慢道来,让你体会一下多线程的强大功能。 -c多线程编程

1. 创建多线程程序:

首先,先介绍一下实现多线程的具体步骤。在C++Builder中虽然用Tthread对象说明了线程的概念,但是Tthread对象本身并不完整,需要在TThread下新建其子类,并重载Execute方法来使用线程对象。在C++Builder下可以很方便地实现这一点。 -c多线程编程

在C++Builder IDE环境下选择菜单File|New,在New栏中选中Thread Object,按OK,接下来弹出输入框,输入TThread对象子类的名字MyThread,这样C++Builder自动为你创建了一个名为TMyThread的TThread子类。同时编辑器中多了一个名为Unit2.cpp的单元,这就是我们创建的TMyThread子类的原码,如下: -c多线程编程

#include

#pragma hdrstop

#include “Unit2.h”

#pragma package(smart_init)

//---------------------

// Important: Methods and properties of objects in VCL can only be

// used in a method called using Synchronize, for example:

//

// Synchronize(UpdateCaption);

//

// where UpdateCaption could look like:

//

// void __fastcall MyThread::UpdateCaption()

// {

// Form1-Caption = “Updated in a thread”;

// }

//--------------------

__fastcall MyThread::MyThread(bool CreateSuspended)

: TThread(CreateSuspended)

{

}

//--------------------

void __fastcall MyThread::Execute()

{

//---- Place thread code here ----

}

//---------------------

其中的Execute()函数就是我们要在线程中实现的任务的代码所在处。在原代码中包含Unit2.cpp,这个由我们创建的TMyThread对象就可以使用了。使用时,动态创建一个TMyThread 对象,在构造函数中使用Resume()方法,那么程序中就增加了一个新的我们自己定义的线程TMyThread,具体执行的代码就是Execute()方法重载的代码。要加载更多的线程,没关系,只要继续创建需要数量的TMyThread 对象就成。-c多线程编程