C++11中Thread类简单使用的例子

代码如下:

#include 
#include 
#include 
#include 
#include  
#include 
#include 

using namespace std;
void helloworld() {
	cout << "hello world" <
double visitRange(thread::id id, Iter iterBegin, Iter iterEnd, Fun fun) {

	auto curId = this_thread::get_id();
	if (id == this_thread::get_id()) {
		cout << curId << "main thread" << endl;
	}
	else {
		cout << curId << "worker thread" << endl;
	}
	double v = 0;
	for (auto iter = iterBegin; iter != iterEnd; ++iter) {
		v += fun(*iter);
	}
	return v;
}



int main()
{
	auto mainThreadId = this_thread::get_id();
	vector v;
	for (int i = 0; i < 1000; i++) {
		v.push_back(rand());
	}
	cout << v.size() << endl;
	double value = 0.0;
	auto st = clock();
	for (auto& info : v) {
		value += caculate(info);
	}
	auto ed = clock();
	cout << "single thread:" << value << " "<

Makefile:

CFLAGS=-std=c++11 -pthread
CC=g++
pThreadTest:pThreadTest.cpp
        $(CC) $(CFLAGS) $^ -o $@
.PHONY:clean
clean:
        $(RM) pThreadTest.o pThreadTest

 

你可能感兴趣的:(Makefile,C++)