---
title: "为什么linux下多线程编程，每次执行结果都不一样"
author: "sipgreen"
date: "2013-01-03T21:41:22Z"
tags: []
categories: ["编程语言"]
slug: "linux"
draft: false
lastmod: "2013-01-03T21:41:22Z"
id: 1759296
url: "https://sipgreen.iteye.com/blog/1759296"
---

```
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>


#include <iostream>
using namespace std;


void* myThread(void* arg);


int main() {

	int ret=0;
	pthread_t mythread;

	ret=pthread_create(&mythread,NULL,myThread,NULL);

	if(ret !=0)
	{
		printf("Can't create pthread (%s) \n", strerror(errno));
	}

	int status=0;

	pthread_join(ret,(void ** ) status);

	cout<<status<<endl;
	cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
	return 0;
}


void* myThread(void* arg)
{
	printf("Thread ran\n");

	printf("fjsdlkjfkdslfkjlsklds");

	//pthread_exit(arg);
}
```

 

 

 

andrewu@andrewu-VirtualBox:~/workspace/cpp/PTthreadeg/src$ ../Debug/PTthreadeg 0

!!!Hello World!!!

Thread ran

andrewu@andrewu-VirtualBox:~/workspace/cpp/PTthreadeg/src$ ../Debug/PTthreadeg 

0

!!!Hello World!!!

andrewu@andrewu-VirtualBox:~/workspace/cpp/PTthreadeg/src$ ../Debug/PTthreadeg 

0

!!!Hello World!!!

andrewu@andrewu-VirtualBox:~/workspace/cpp/PTthreadeg/src$ ../Debug/PTthreadeg 

0

!!!Hello World!!!

andrewu@andrewu-VirtualBox:~/workspace/cpp/PTthreadeg/src$ ../Debug/PTthreadeg 

Thread ran

0

!!!Hello World!!!

andrewu@andrewu-VirtualBox:~/workspace/cpp/PTthreadeg/src$ ../Debug/PTthreadeg 

0Thread ran

fjsdlkjfkdslfkjlsklds

!!!Hello World!!!

andrewu@andrewu-VirtualBox:~/workspace/cpp/PTthreadeg/src$ ../Debug/PTthreadeg 

0

!!!Hello World!!!

Thread ran

Thread ran

andrewu@andrewu-VirtualBox:~/workspace/cpp/PTthreadeg/src$