当前位置:首页 » 操作系统 » linuxcpthread

linuxcpthread

发布时间: 2023-02-02 18:04:27

‘壹’ linux C线程崩溃的原因!!!

gcc xxx.c -lpthread 其中的-l是指包含的lib库,具体写法可以man gcc看下
多线程函数除了要包含头文件pthread.h外还必须要包含lib库pthread
pthread_create是创建线程,但具体的线程里面做什么事是在void *create(void *arg)里,这个函数名是自己任意区的,但返回值和参数一般都是void*类型,因为pthread_create函数的定义就是这样
int pthread_create(pthread_t*restrict tidp,const pthread_attr_t *restrict_attr,void*(*start_rtn)(void*),void *restrict arg);

‘贰’ Linux C 怎么实现两个线程同步读取两个内存的数据

在Linux系统中使用C/C++进行多线程编程时,我们遇到最多的就是对同一变量的多线程读写问题,大多情况下遇到这类问题都是通过锁机制来处理,但这对程序的性能带来了很大的影响,当然对于那些系统原生支持原子操作的数据类型来说,我们可以使用原子操作来处理,这能对程序的性能会得到一定的提高。那么对于那些系统不支持原子操作的自定义数据类型,在不使用锁的情况下如何做到线程安全呢?本文将从线程局部存储方面,简单讲解处理这一类线程安全问题的方法。

一、数据类型
在C/C++程序中常存在全局变量、函数内定义的静态变量以及局部变量,对于局部变量来说,其不存在线程安全问题,因此不在本文讨论的范围之内。全局变量和函数内定义的静态变量,是同一进程中各个线程都可以访问的共享变量,因此它们存在多线程读写问题。在一个线程中修改了变量中的内容,其他线程都能感知并且能读取已更改过的内容,这对数据交换来说是非常快捷的,但是由于多线程的存在,对于同一个变量可能存在两个或两个以上的线程同时修改变量所在的内存内容,同时又存在多个线程在变量在修改的时去读取该内存值,如果没有使用相应的同步机制来保护该内存的话,那么所读取到的数据将是不可预知的,甚至可能导致程序崩溃。
如果需要在一个线程内部的各个函数调用都能访问、但其它线程不能访问的变量,这就需要新的机制来实现,我们称之为Static memory local to a thread (线程局部静态变量),同时也可称之为线程特有数据(TSD: Thread-Specific Data)或者线程局部存储(TLS: Thread-Local Storage)。这一类型的数据,在程序中每个线程都会分别维护一份变量的副本(),并且长期存在于该线程中,对此类变量的操作不影响其他线程。如下图:

二、一次性初始化
在讲解线程特有数据之前,先让我们来了解一下一次性初始化。多线程程序有时有这样的需求:不管创建多少个线程,有些数据的初始化只能发生一次。列如:在C++程序中某个类在整个进程的生命周期内只能存在一个实例对象,在多线程的情况下,为了能让该对象能够安全的初始化,一次性初始化机制就显得尤为重要了。——在设计模式中这种实现常常被称之为单例模式(Singleton)。Linux中提供了如下函数来实现一次性初始化:
#include <pthread.h>

// Returns 0 on success, or a positive error number on error
int pthread_once (pthread_once_t *once_control, void (*init) (void));
利用参数once_control的状态,函数pthread_once()可以确保无论有多少个线程调用多少次该函数,也只会执行一次由init所指向的由调用者定义的函数。init所指向的函数没有任何参数,形式如下:
void init (void)
{
// some variables initializtion in here
}
另外,参数once_control必须是pthread_once_t类型变量的指针,指向初始化为PTHRAD_ONCE_INIT的静态变量。在C++0x以后提供了类似功能的函数std::call_once (),用法与该函数类似。使用实例请参考https://github.com/ApusApp/Swift/blob/master/swift/base/singleton.hpp实现。

‘叁’ 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>

intmtc[3]={0};//resultmatrix

typedefstruct
{
intprank;
int*mta;
int*mtb;
}Info_t;

void*calMatrix(void*arg)
{
inti;
Info_t*info=(Info_t*)arg;
intprank=info->prank;
fprintf(stdout,"calMatrix:prankis%d ",prank);

for(i=0;i<3;i++)
mtc[prank]+=info->mta[i]*info->mtb[i];

returnNULL;
}

intmain(intargc,char**argv)
{
inti,j,k=0;
intmta[3][3];
intmtb[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++;
/*3threads*/
pthread_t*threads=(pthread_t*)malloc(sizeof(pthread_t)*3);
fprintf(stdout," ");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," ====thematrixresult==== ");
fflush(stdout);

for(i=0;i<3;i++)
{
fprintf(stdout,"mtc[%d]=%d ",i,mtc[i]);
fflush(stdout);
}
return0;
}

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

‘肆’ linux C语言进程

1、fork 是用来创建子进程的, 而不是线程( 线程创建需要用到 pthread_create )。
需要根据 fork() 的返回值来判断下面的代码是在父进程(返回pid>0)中还是子进程(返回0)中. 像上面的代码中 if 中的代码被在子进程中执行, else 中的代码在父进程中执行。
2、例程:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main()
{
pid_t id; //定义一个进程号变量
int i=0;
printf("start fork/n");
id = fork(); //调用fork函数新建一个进程
i ++;
printf("end fork/n");
//判断当前进程
if(id < 0){ //出错
perror("fork failed/n");
exit(1);
}
else if(id == 0){ //子进程
printf("In child/n");
printf("i = %d/n", i++);
exit(0);
}
else{ //父进程
printf("In father/n");
printf("i = %d/n", i++);
exit(0);
}
return 0;
}
}
else if(WIFEXITED(status))
{
printf(");
while(((child=wait(&status))==-1)&(errno==EINTR));
exit(1).h>;Fork Error,strerror(errno));;*请解释一下*/
if((child=fork())==-1)
{
printf("
#include<I am the child;
int main(int argc;n"n".h>:%d\,strerror(errno)).h>
printf(",getpid());1000000!status)
{
printf("n".h>
int status.h>;sys/;;errno;
exit(i);n"wait;,WEXITSTATUS(status)),child);
#include<*请解释一下*/
if(child==-1)
{
printf("Child %d terminated normally return status is %d\
}
else if(;i<:%s\n"
i=5.h>,child;n"Child %d terminated e to signal %d znot caught\
printf("sys/I exit with%d\math;stdio;
#include<
printf("n"
#include<
}
else if(child==0)
{
int i=0;
}
/
}
else if(WIFSIGNALED(status))
{
printf("types:%s";unistd,i),char **argv)
{
pid_t child;
/Child %d terminated normally return status is zero\Wait Error;This will demostrate how to get the child status\
for(i=0;i++);
#include<,WTERMSIG(status));
}
return 0能不能帮忙解释一下这段代码中的一部分
#include<,child

‘伍’ linux c编程中,使用pthread_create函数创建线程时,函数的第3个参数的是void

可以这样声明,但是在调用pthread_create函数的时候需要将线程函数的指针强制类型转换成void *(pthread)(void*),否则编译器会报错。

热点内容
pythonforinkeys 发布:2024-05-19 01:55:44 浏览:792
电脑如何局域网共享文件夹 发布:2024-05-19 01:25:01 浏览:68
手机存储越大性能越好吗 发布:2024-05-19 01:14:28 浏览:176
我的世界hyp服务器怎么玩 发布:2024-05-19 00:51:25 浏览:801
手机如何解压百度云文件 发布:2024-05-19 00:32:24 浏览:905
centos使用python 发布:2024-05-18 23:39:48 浏览:869
幻影天龙脚本 发布:2024-05-18 23:38:17 浏览:714
编程的py 发布:2024-05-18 23:36:22 浏览:76
安卓系统怎么改序列号 发布:2024-05-18 23:28:16 浏览:785
c语言中实数 发布:2024-05-18 23:21:03 浏览:897