原文:http://blog.csdn.net/cn_wk/article/details/62236057

hello thread!

  1. 声明线程A的端口号


    #include <pthread.h> pthread_t tid;
  2. 定义线程运行函数

    void thread_function(void *args)
    {
    printf("thread_function is called!\n");
    //注意区别 cout << "thread_function is called! << endl; 和上一行
    }
  3. 创建线程

    pthread_create(&tid, NULL, &thread_function, NULL);
  4. (optional)阻塞: 挂起当前线程,直到tid线程结束

    pthread_join(tid, NULL);

好了,这样就可以启一个线程了,完整代码如下:

#include<unistd.h>
#include<stdio.h>
#include<cstring>
#define MAX_THREAD 2 pthread_t thread[MAX_THREAD];
pthread_mutex_t mut; int num = ;
void* thread1(void *args)
{
printf("hi, I am thread1\n");
for(int i=; i < ; i++){
pthread_mutex_lock(&mut);
printf("[thread1]num=%d, i=%d\n", num, i);
num ++;
pthread_mutex_unlock(&mut);
sleep();
}
pthread_exit(NULL);
} void* thread2(void *args)
{
printf("hi, I am thread2\n");
for(int i = ; i < ; i++)
{
pthread_mutex_lock(&mut);
printf("[thread2]num=%d, i=%d\n", num, i);
num ++;
pthread_mutex_unlock(&mut);
sleep();
}
pthread_exit(NULL);
} void thread_create()
{
memset(&thread, , sizeof(thread));
if( !=pthread_create(&thread[], NULL, thread1, NULL))
printf("thread1 create faild\n");
else
printf("thread1 established\n"); if( != pthread_create(&thread[], NULL, thread2, NULL))
printf("thread2 create faild\n");
else
printf("thread2 established\n"); } void thread_wait()
{
if( != thread[])
{
pthread_join(thread[], NULL);
printf("thread 1 is over\n");
} if( != thread[])
{
pthread_join(thread[], NULL);
printf("thread 2 is over\n");
}
} int main()
{
pthread_mutex_init(&mut, NULL);
printf("main thread: creating threads...\n");
thread_create();
printf("main thread: waiting threads to accomplish task...\n");
thread_wait();
return ;
}

代码,得到结果:

tmain thread: creating threads… 
thread1 established 
hi, I am thread1 
[thread1]num=0, i=0 
thread2 established 
main thread: waiting threads to accomplish task… 
hi, I am thread2 
[thread2]num=1, i=0 
[thread1]num=2, i=1 
[thread2]num=3, i=1 
[thread1]num=4, i=2 
[thread2]num=5, i=2 
[thread1]num=6, i=3 
[thread1]num=7, i=4 
[thread2]num=8, i=3 
[thread1]num=9, i=5 
[thread2]num=10, i=4 
thread 1 is over 
[thread2]num=11, i=5 
[thread2]num=12, i=6 
[thread2]num=13, i=7 
[thread2]num=14, i=8 
[thread2]num=15, i=9 
thread 2 is over

OK,现在说一下pthread_create方法的原型

pthread_create方法

作用

  • 创建线程

原型

```
int pthread_create(pthread_t *restrict tidp,const pthread_attr_t *restrict_attr,void*(*start_rtn)(void*),void *restrict arg);
```

参数

  • 第一个参数是指向线程标识符的指针
  • 第二个参数用来设置线程的属性
  • 第三个参数是线程运行函数的首地址
  • 第四个参数是线程运行函数的参数

返回值

  • 若成功,则返回0;否则,返回错误编号。

pthread_join方法

作用

  • 这个函数是一个线程阻塞的函数
  • 等待线程结束再继续往下执行,要不然主进程和下面的线程并行执行

原型

extern int pthread_join __P ((pthread_t __th, void **__thread_return))

参数

  • 第一个参数是被等待线程的标识符
  • 第二个参数是用户自定义的指针。用来存储被等待线程的返回值

[C++]多线程: 教你写第一个线程的更多相关文章

  1. 用JAVA写一个多线程程序,写四个线程,其中二个对一个变量加1,另外二个对一个变量减1

    package com.ljn.base; /** * @author lijinnan * @date:2013-9-12 上午9:55:32 */ public class IncDecThrea ...

  2. Java 多线程详解(五)------线程的声明周期

    Java 多线程详解(一)------概念的引入:http://www.cnblogs.com/ysocean/p/6882988.html Java 多线程详解(二)------如何创建进程和线程: ...

  3. 网络编程懒人入门(八):手把手教你写基于TCP的Socket长连接

    本文原作者:“水晶虾饺”,原文由“玉刚说”写作平台提供写作赞助,原文版权归“玉刚说”微信公众号所有,即时通讯网收录时有改动. 1.引言 好多小白初次接触即时通讯(比如:IM或者消息推送应用)时,总是不 ...

  4. Android多线程编程<一>Android中启动子线程的方法

          我们知道在Android中,要更新UI只能在UI主线程去更新,而不允许在子线程直接去操作UI,但是很多时候,很多耗时的工作都交给子线程去实现,当子线程执行完这些耗时的工作后,我们希望去修改 ...

  5. Python GUI之tkinter窗口视窗教程大集合(看这篇就够了) JAVA日志的前世今生 .NET MVC采用SignalR更新在线用户数 C#多线程编程系列(五)- 使用任务并行库 C#多线程编程系列(三)- 线程同步 C#多线程编程系列(二)- 线程基础 C#多线程编程系列(一)- 简介

    Python GUI之tkinter窗口视窗教程大集合(看这篇就够了) 一.前言 由于本篇文章较长,所以下面给出内容目录方便跳转阅读,当然也可以用博客页面最右侧的文章目录导航栏进行跳转查阅. 一.前言 ...

  6. iOS多线程全套:线程生命周期,多线程的四种解决方案,线程安全问题,GCD的使用,NSOperation的使用

    目的 本文主要是分享iOS多线程的相关内容,为了更系统的讲解,将分为以下7个方面来展开描述. 多线程的基本概念 线程的状态与生命周期 多线程的四种解决方案:pthread,NSThread,GCD,N ...

  7. 多线程的基本概念和Delphi线程对象Tthread介绍

    多线程的基本概念和Delphi线程对象Tthread介绍 作者:xiaoru    WIN 98/NT/2000/XP是个多任务操作系统,也就是:一个进程可以划分为多个线程,每个线程轮流占用CPU运行 ...

  8. 手把手教你写LKM rookit! 之 第一个lkm程序及模块隐藏(一)

    唉,一开始在纠结起个什么名字,感觉名字常常的很装逼,于是起了个这<手把手教你写LKM rookit> 我觉得: 你们觉得:...... 开始之前,我们先来理解一句话:一切的操作都是系统调用 ...

  9. 手把手教你写电商爬虫-第三课 实战尚妆网AJAX请求处理和内容提取

    版权声明:本文为博主原创文章,未经博主允许不得转载. 系列教程: 手把手教你写电商爬虫-第一课 找个软柿子捏捏 手把手教你写电商爬虫-第二课 实战尚妆网分页商品采集爬虫 看完两篇,相信大家已经从开始的 ...

随机推荐

  1. 【初探】java性能火焰图的生成

    前言 开始之前,你需要准备的环境: Linux系统机器或者虚拟机一台,里面需要安装的软件:git.jdk.perl. 简单介绍: java性能分析火焰图的所做的事情就是能够分析出java程序运行期间存 ...

  2. 牛客Wannafly挑战赛26E 蚂蚁开会(树链剖分+线段树)

    传送门 题面描述 一颗n个节点的树,m次操作,有点权(该节点蚂蚁个数)和边权(相邻节点的距离). 三种操作: 操作1:1 i x将节点i的点权修改为x.(1 <= i <= n; 1 &l ...

  3. 洛谷P3980 [NOI2008]志愿者招募

    题解 最小费用最大流 每一天是一条边\((inf-a[i], 0)\) 然后对于一类志愿者, 区间两端连一条\((inf, c[i])\) \(S\)向第一个点连\((inf, 0)\) 最后一个点向 ...

  4. 3Q大战现高潮,360 推出Android "3Q" IM即时通讯,岁末年初3Q大战惊现高潮

    岁末年初3Q大战惊现高潮,360震撼推出Android "3Q" IM即时通讯       看过了QQ和360斗争的开端高潮,当然现在还不能说这场斗争已经结束,在我看来这次的事件未 ...

  5. PSR2规范

    为了尽可能的提升阅读其他人代码时的效率,下面例举了一系列的通用规则,特别是有关于PHP代码风格的.各个成员项目间的共性组成了这组代码规范.当开发者们在多个项目中合作时,本指南将会成为所有这些项目中共用 ...

  6. java.util.Collections.synchronizedSet()方法的使用

    下面的例子显示java.util.Collections.synchronizedSet()方法的使用 package com.; import java.util.*; public class C ...

  7. thinkPHP5配置nginx环境无法打开(require(): open_basedir restriction in effect. File(/mnt/hgfs/root/tp5/thinkphp/start.php) is not within the allowed path(s)

    今天想把玩一下tp5,结果怎么都无法访问,每次都是报500错误,我把错误提示都打开看到下面的错误 require(): open_basedir restriction in effect. File ...

  8. solr集群的搭建教程和使用入门

    1 什么是SolrCloud? SolrCloud(solr 云)是Solr提供的分布式搜索方案,当你需要大规模,容错,分布式索引和检索能力时使用 SolrCloud. 当一个系统的索引数据量少的时候 ...

  9. maven的安装配置超详细教程【含nexus】

    1 下载 下载地址:http://maven.apache.org/download.cgi 界面效果如下: 点击之后进入的apache 软件基金的发布目录,在这里你可以下载apache的所有项目. ...

  10. JavaScript数据结构-19.拓扑排序

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...