posted by Kyleslab 2010. 12. 10. 22:08


// Test.cpp : 콘솔 응용 프로그램에 대한 진입점을 정의합니다.
//

#include "stdafx.h"

#include <string>
#include <vector>
#include <algorithm>
#include <iostream>

using namespace std;

#include "Concurrency/Thread.h"
#include "Concurrency/SpinLock.h"

//이거 연결은 정적 라이브러리 연결 동적이 나올수도 있음

string g_buffer;
SpinLock g_lock; //1000번 찍는데 락을 걸어주기 위해서 우리가 만든 락 클래스를 이용한다.

int _tmain(int argc, _TCHAR* argv[])
{
 vector<Thread * >threads; //힙을 이용하면 new를 이용하면 생성하고 delete를 하면 우리가 생성 소멸 타이밍을 조절 할 수 있다.

 for(int i =0; i < 5; ++i)
 {
  char ch = 'a'+i;
  threads.push_back(new Thread( // 푸시백은 백터 맨뒤에 이것들을 추가하는것
  [ ch ]()->void //[]여기 안에는 클로져 특성을 가지는 변수, 캡쳐할 리스트들 ()인자값이 들어감
  {
   g_lock.lock();
   for(int i =0; i < 1000; ++i)
   {
    g_buffer +=ch;
   }
   g_lock.unlock(); //락을 걸어준다.

  }));
 }
 for_each(threads.begin(), //이것은 외우기
  threads.end(),
  [](Thread * thread)  //포위치로 쓰래드를 처음부터 끝까지 훌터서 지울려고 하는거임
 {
  delete thread; //벡터에는 포인터가 들어있기 때문에 벡터의 원소는 변화가 없다. 쓰레드가 종료 되면 지워버림
 });
   
 cout<<g_buffer<<endl;
 return 0;
}

 

'Operations Dev > Concurrency' 카테고리의 다른 글

자체 뮤텍스락 이용  (0) 2010.12.10
Concurrency  (0) 2010.12.10