ITeye博客

为什么linux下多线程编程,每次执行结果都不一样

sipgreen · 2013-01-03 21:41 · Linux · C++ · 编程语言 · Markdown

#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$

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。