大多数线程函数以pthread_开头,.h为pthread.h,   用-lpthread来链接线程库。

编写多线程时,定义宏_REENTRANT告诉编译器需要可重入,此宏必须位于任何#include之前。#include <pthread.h>

int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
                          void *(*start_routine) (void *), void *arg);

//#define _REENTRANT
//#define _POSIX_C_SOURCE
#include <iostream>
#include <string>
#include <cstdio>
#include <assert.h>
#include <cstring>
#include <error.h>
#include <unistd.h>
#include <sys/types.h>
#include <signal.h>
#include <pthread.h>

using namespace std;

void *thread_fun(void *argument);

char message[] = "hello world";

int main(int argc, char* argv[])
{
    int res;
    pthread_t a_thread;
    void *thread_result;

res = pthread_create(&a_thread, NULL, thread_fun, (void *)message);
    if(res != 0)
    {
        perror("Thread creation failed");
        exit(EXIT_FAILURE);
    }

cout << "Waiting for thread to finish\n";
    res = pthread_join(a_thread, &thread_result);
    if(res != 0)
    {
        perror("Thread join failed");
        exit(EXIT_FAILURE);
    }

printf("Thread joined,it returned %s\n",(char *)thread_result);
    printf("Message is now %s\n",message);
    exit(EXIT_SUCCESS);
    return 0;
}

void *thread_fun(void *argument)
{
    printf("thread_fun is running. Arg was %s\n",(char *)argument);
    sleep(2);
    strcpy(message,"Bye!");
    pthread_exit(const_cast<char *>("Thank you for the CPU time"));
}

POSIX线程的更多相关文章

  1. Posix线程编程指南(5) 杂项

    在Posix线程规范中还有几个辅助函数难以归类,暂且称其为杂项函数,主要包括pthread_self().pthread_equal()和pthread_once()三个,另外还有一个LinuxThr ...

  2. Posix线程编程指南(4) 线程终止

    线程终止方式 一般来说,Posix的线程终止有两种情况:正常终止和非正常终止.线程主动调用pthread_exit()或者从线程函数中return都将使线程正常退出,这是可预见的退出方式:非正常终止是 ...

  3. Posix线程编程指南(2) 线程私有数据

    概念及作用 在单线程程序中,我们经常要用到"全局变量"以实现多个函数间共享数据.在多线程环境下,由于数据空间是共享的,因此全局变量也为所有线程所共有.但有时应用程序设计中有必要提供 ...

  4. POSIX 线程详解 一种支持内存共享的简捷工具

    线程是有趣的 了解如何正确运用线程是每一个优秀程序员必备的素质.线程类似于进程.如同进程,线程由内核按时间分片进行管理.在单处理器系统中,内核使用时间分片来模拟线程的并发执行,这种方式和进程的相同.而 ...

  5. Posix线程编程指南(3) 线程同步

    互斥锁 尽管在Posix Thread中同样可以使用IPC的信号量机制来实现互斥锁mutex功能,但显然semphore的功能过于强大了,在Posix Thread中定义了另外一套专门用于线程同步的m ...

  6. posix 线程(一):线程模型、pthread 系列函数 和 简单多线程服务器端程序

    posix 线程(一):线程模型.pthread 系列函数 和 简单多线程服务器端程序 一.线程有3种模型,分别是N:1用户线程模型,1:1核心线程模型和N:M混合线程模型,posix thread属 ...

  7. 通用线程:POSIX 线程详解,第 3 部分 条件互斥量(pthread_cond_t)

    使用条件变量提高效率 本文是 POSIX 线程三部曲系列的最后一部分,Daniel 将详细讨论如何使用条件变量.条件变量是 POSIX 线程结构,可以让您在遇到某些条件时“唤醒”线程.可以将它们看作是 ...

  8. 浅谈POSIX线程的私有数据

    当线程中的一个函数需要创建私有数据时,该私有数据在对函数的调用之间保持一致,数据能静态地分配在存储器中,当我们采用命名范围也许可以实现它使用在函数或是文件(静态),或是全局(EXTERN).但是当涉及 ...

  9. POSIX 线程取消点的 Linux 实现

    http://blog.csdn.net/stevenliyong/article/details/4364039 原文链接:http://blog.solrex.cn/articles/linux- ...

  10. POSIX 线程详解

    一种支持内存共享的简捷工具 POSIX(可移植操作系统接口)线程是提高代码响应和性能的有力手段.在本系列中,Daniel Robbins 向您精确地展示在编程中如何使用线程.其中还涉及大量幕后细节,读 ...

随机推荐

  1. POJ1836Alignment(LCA)

    Alignment Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 15135   Accepted: 4911 Descri ...

  2. Java_观察者模式(Observable和Observer)

    http://blog.csdn.net/tianjf0514/article/details/7475164/ 一.观察者模式介绍 在Java中通过Observable类和Observer接口实现了 ...

  3. Activator.CreateInstance 反射实例化对象

    public class CommonReq { private String TransNo { get; set;} public String SubmitData { get; set; } ...

  4. User表格式

    "_id":基本是700多 "name":"xx01" "pwd":"123"

  5. 神经网络训练中的Tricks之高效BP(反向传播算法)

    神经网络训练中的Tricks之高效BP(反向传播算法) 神经网络训练中的Tricks之高效BP(反向传播算法) zouxy09@qq.com http://blog.csdn.net/zouxy09 ...

  6. Android文件保存和读取

    public class DataActivity extends Activity { private EditText filenameText; private EditText content ...

  7. matlab怎么定义一个数组

    A=[];n=input('n=');%数组的长度for i=1:n fprintf('a%.0f=',i); x=input('');%分别输入各个数的值 A=[A,x];endA就可以得到长度为n ...

  8. HNU 12827 NASSA’s Robot

    题目链接:http://acm.hnu.cn/online/?action=problem&type=show&id=12827&courseid=268 #include&l ...

  9. [Effective JavaScript 笔记]第4章:对象和原型--个人总结

    前言 对象是js中的基本数据结构.对象在js语言编码中也随处可见,比如经常会用到的函数,也是一个Function构造函数,Function.prototype原型对象.每当声明一个函数时,都会继承Fu ...

  10. js判断元素是否隐藏的方法

    代码如下: JavaScript代码如下: if( document.getElementById("div").css("display")==='none' ...