简介

Linux线程是需要连接pthreat库,线程的使用比进程更灵活,需要注意的是线程间的互斥,或者说是资源共享问题。

C++11之后,C++标准库也引入了线程,并且使用非常方便,以后再介绍,这里先发一个简单的线程示例代码。

代码

#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <stdlib.h> char message[32]={"Hello world!"};
void *thread_function(void *arg); void *thread_function(void *arg)
{
printf("thread_fonction is runing , argument is %s\n", (char *)arg);
strcpy(message, "marked by thread"); printf("thread_function finished\n");
pthread_exit("Thank you for the cpu time\n");
} int
main(int argc, char **argv)
{
pthread_t a_thread;
void *thread_result; if(pthread_create(&a_thread, NULL, thread_function, (void*)message ) < 0)
{
perror("pthread_create error:");
exit(-1);
} printf("writing for the thread to finish\n");
if(pthread_join(a_thread, &thread_result) < 0)
{
perror("pthread_join error:");
exit(0);
} printf("in main, thread is exist, marked msg: %s \n", message); exit(0);
}

编译

编译的时候,需要加上pthread线程库

gcc pthreat.c -o test -lpthread

运行

程序启动后,主程序中,创建线程,然后等待线程退出,在线程函数里,会把message字符串修改掉。

./test
in main, writing for the thread to finish
in thread, thread_fonction is runing , argument is Hello world!
in thread, thread_function finished
in main, thread is exist, marked msg: marked by thread

LInux下Posix的传统线程示例的更多相关文章

  1. Linux下c开发 之 线程通信(转)

    Linux下c开发 之 线程通信(转) 1.Linux“线程” 进程与线程之间是有区别的,不过Linux内核只提供了轻量进程的支持,未实现线程模型.Linux是一种“多进程单线程”的操作系统.Linu ...

  2. Linux下c开发 之 线程通信

    Linux下c开发 之 线程通信 1.Linux“线程” 进程与线程之间是有区别的,不过Linux内核只提供了轻量进程的支持,未实现线程模型.Linux是一种“多进程单线程”的操作系统.Linux本身 ...

  3. Linux下的进程与线程(二)—— 信号

    Linux进程之间的通信: 本文主要讨论信号问题. 在Linux下的进程与线程(一)中提到,调度器可以用中断的方式调度进程. 然而,进程是怎么知道自己需要被调度了呢?是内核通过向进程发送信号,进程才得 ...

  4. linux下进程的最大线程数、进程最大数、进程打开的文件数

    linux下进程的最大线程数.进程最大数.进程打开的文件数   ===========最大线程数============== linux 系统中单个进程的最大线程数有其最大的限制 PTHREAD_TH ...

  5. Linux下查看进程和线程

    在linux中查看线程数的三种方法 1.top -H 手册中说:-H : Threads toggle 加上这个选项启动top,top一行显示一个线程.否则,它一行显示一个进程. 2.ps xH 手册 ...

  6. Linux下的进程与线程(一)—— 进程概览

    进程是操作系统分配资源的基本单位.线程是操作系统进行运行和调度的基本单位. 进程之间可以切换,以便轮流占用CPU,实现并发.一般进程运行在用户模式下,只能执行指令集中的部分指令. 当进程进行上下文切换 ...

  7. Linux下使用两个线程协作完成一个任务的简易实现

    刚解决了之前的那个Linux下Pthread库的问题,这次就来使用两个线程来协作,共同完成一个求和的任务. 打一下基础吧 本次需要使用到的知识点有: lpthread,编译的时候用到,相当于一个声明的 ...

  8. linux下的进程,子进程,线程

    1.相同点:(a)二者都具有ID,一组寄存器,状态,优先级以及所要遵循的调度策略.(b) 每个进程都有一个进程控制块,线程也拥有一个线程控制块.(c) 线程和子进程共享父进程中的资源:线程和子进程独立 ...

  9. Linux下的strerror是否线程安全?

    下列是glibc-2.14中的源代码: 点击(此处)折叠或打开 char * strerror (errnum) int errnum; { char *ret = __strerror_r (err ...

随机推荐

  1. DVWA学习记录 PartⅠ

    DVWA介绍 DVWA(Damn Vulnerable Web Application)是一个用来进行安全脆弱性鉴定的PHP/MySQL Web应用,旨在为安全专业人员测试自己的专业技能和工具提供合法 ...

  2. python 网络爬虫报错“UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position”解决方案

    Python3.x爬虫, 发现报错“UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 1:invalid sta ...

  3. Flask 基础组件(十):中间件

    from flask import Flask, flash, redirect, render_template, request app = Flask(__name__) app.secret_ ...

  4. nodejs之EventEmitter实现

    Node.js 所有的异步 I/O 操作在完成时都会发送一个事件到事件队列. Node.js 里面的许多对象都会分发事件:一个 net.Server 对象会在每次有新连接时触发一个事件, 一个 fs. ...

  5. Go Pentester - HTTP CLIENTS(3)

    Interacting with Metasploit Early-stage Preparation: Setting up your environment - start the Metaspl ...

  6. 紧急处理RAC环境有一个监听down 的情况

    初步处理 1. grid 登录查看是监听是否down掉 srvctl status listener -n node1 或者oracle登录 lsnrctl status 查看 如果掉了 grid 用 ...

  7. css :clip rect 正确的使用方法

    CSS clip 是一个极少使用,但又确实存在的属性. 而且,它其实在CSS2时代就有了. w3school 上有这么一句话: clip 属性剪裁绝对定位元素. 也就是说,只有 position:ab ...

  8. python 中文乱码 list 乱码处理

    list 乱码 data_list = ["中文"] print str(data_list).decode("string_escape") mysql 获取 ...

  9. vue中v-for

    在vue中我们只要操作数据,就可以渲染和更新数据,这背后的boss就是diff算法 vue和react的虚拟DOM的Diff算法大致相同,其核心是基于两个简单的假设: 1. 俩个相同组件产生类似DOM ...

  10. Flutter防止布局溢出

    添加一层可滑动View(Widget)的布局, 将之前进行包裹: return new Scaffold(      appBar: new AppBar(        title: new Tex ...