C++ Multithread Tutorial

---恢复内容开始---

Example 1

Creating and terminating thread by using

pthread_create, pthread_exit(status)

?





1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

#include <iostream>

#include <cstdlib>

#include <pthread.h>

using
namespace std;

#define NUM_THREADS     5

void *PrintHello(void
*threadid)

{

   long
tid;

   tid = (long)threadid;

   cout << "Hello World! Thread ID, "
<< tid << endl;

   pthread_exit(NULL);

}

int
main ()

{

   pthread_t threads[NUM_THREADS];

   int
rc;

   int
i;

   for( i=0; i < NUM_THREADS; i++ ){

      cout << "main() : creating thread, "
<< i << endl;

      rc = pthread_create(&threads[i], NULL,

                          PrintHello, (void
*)i);

      if
(rc){

         cout << "Error:unable to create thread,"
<< rc << endl;

         exit(-1);

      }

   }

   pthread_exit(NULL);

}

compile it with g++ xxx.cpp -o test -lphread

Example 2

Passing Arguments to Threads

?





1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

#include <iostream>

#include <cstdlib>

#include <pthread.h>

using
namespace std;

#define NUM_THREAD 5

struct
thread_data  {

    int
thread_id;

    char
*message;

};

void
*PrintHello(void
*threadarg)  {

    struct
thread_data *my_data;

    my_data = (struct
thread_data*) threadarg;

    cout << "Thread ID : "
<< my_data->thread_id << endl;

    cout << "message : "
<< my_data->message << endl;

    pthread_exit(NULL);

}

int
main()  {

    pthread_t threads[NUM_THREAD];

    struct
thread_data td[NUM_THREAD];

    int
rc;

    int
i;

    for(i = 0; i < NUM_THREAD; i ++)  {

        cout << "main() : creating thread "
<< i << endl;

        td[i].thread_id = i;

        td[i].message = "This is message";

        rc = pthread_create(&threads[i], NULL, PrintHello, (void*)&td[i]);

        

        if(rc)  {

            cout << "Error : unable to create thread "
<< rc << endl;

            exit(-1);

        }

    }

    pthread_exit(NULL);

}

Example 3

Joining and Detaching thread

---恢复内容结束---

时间: 2024-07-31 09:28:42

C++ Multithread Tutorial的相关文章

初译 Support Vector Machines:A Simple Tutorial(一)

从本次开始我将开始尝试着逐章翻译一下 Alexey Nefedov的<Support Vector Machines:A Simple Tutorial>这本教材,这可是我们导师极力推荐的SVM教材,看了好久一直感觉一脸懵逼,索性开坑翻译一下吧,也当是加深理解,毕竟我也是一知半解,如果翻译的有不对的地方还望大佬们斧正,欢迎提意见,欢迎讨论. 嗯,就是这样. (一)Introduction 在本章节中将会介绍一些用于定义支持向量机(SVM)的基础的概念,这些概念对于理解SVM至关重要,假定读者了

ZetCode PyQt4 tutorial Dialogs

#!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, we receive data from a QtGui.QInputDialog dialog. author: Jan Bodnar website: zetcode.com last edited: October 2011 """ import sys from PyQ

简单的LESS Tutorial

1. 什么是LESS? LESS是一种动态的CSS语言,更专业的称呼是CSS preprocessor.作为CSS的扩展语言,LESS可以让CSS文件逻辑上更清晰,从而更容易维护和更新.LESS是开源的,诞生于2009年,采用javascript开发, LESS深受另外一种动态CSS语言SASS/SCSS的影响(SCSS是SASS的升级版) .相对于SASS/SCSS或者其他CSS preprocessor, LESS的典型特征有两个, 支持实时编译,例如网页或者应用可以直接应用less文件,通

(翻译)deeplearning.net/tutorial —— 栈式去噪自编码器(SdA)

前言 栈式去噪自编码器是栈式自动编码器的扩展[Bengio07],并且它在[Vincent08]里有介绍. 这次教程建立在之前的去噪自编码器Denoising Autoencoders.如果你对自编码器没什么了解,建议你先了解一下. 栈式自编码器 通过把上一层去噪自编码器找到的隐藏输入(output code)当作下一层的输入,我们可以把去噪自编码器以栈的形式构成一个深度网络.这种无监督预训练的结构在一层里同时实现.每一层当作一个去噪自编码器,通过重构输入(上一层的输出)最小化损失.一旦前面 层

A Complete ActiveX Web Control Tutorial

? Introduction ActiveX is a Microsoft technology developed in the mid 90's, that allows for the creation of applet-like applications that can be downloaded and run within Microsoft's Web browser. This article is intended for Visual C++ developers who

PIC32MZ tutorial -- OC Interrupt

In my previous blog "PIC32MZ tutorial -- Output Compare", I shows how to apply Output Compare without interrupt to generate PWM signal. I also tried the Output Compare interrupt. I selected OC to be PWM mode without fault pin (OCM = "110&qu

akka---Getting Started Tutorial (Java): First Chapter

原文地址:http://doc.akka.io/docs/akka/2.0.2/intro/getting-started-first-java.html Introduction Welcome to the first tutorial on how to get started with Akka and Java. We assume that you already know what Akka and Java are and will now focus on the steps

NS3之路---Tutorial解读---Beginning&amp;&amp;Concept

鉴于前面已经有写过了ns3安装的部分,因此也就不重新介绍beginning部分了. 第四章介绍了几个网络中非常重要的概念.对于网络比较熟悉的基本上一看就能懂,理解这几个概念对于理解ns3十分重要.下面就是tutorial第四章的相关翻译工作. 相关概念介绍 首先,在进行ns3程序开发之前,我们有必要对相关概念进行介绍.它们是网络中最基本的对象,因此必须理解. 节点-Node 在因特网术语中,连接到网络的计算设备被称为主机或者终端.但是由于ns3是网络模拟器,而非因特网模拟器,因此我们更习惯使用图

ZetCode PyQt4 tutorial signals and slots

#!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, we connect a signal of a QtGui.QSlider to a slot of a QtGui.QLCDNumber. author: Jan Bodnar website: zetcode.com last edited: October 2011 ""&quo