You can use rvalues for creating move semantics - remove copy constructors for temporaries. Example:
#include <iostream>
class A
{
public:
A()
{
x = new int[1];
x[0] = 1;
std::cout << "new\n";
}
~A()
{
delete[] x;
}
A(const A& a)
{
x = new int[1];
x[0] = a.x[0];
std::cout << "new\n";
}
/* A(A&& a) //move constructor
{
x = a.x;
a.x = 0;
}*/
private:
int *x;
};
A GetA()
{
A a;
return a;
}
//if you want to use movable object as member in another object you should declare move constructor
//and use std::move
class B
{
public:
B(){}
B(B&& b) //move constructor
{
a = std::move(b.a);
}
A a;
};
B GetB()
{
B b;
return b;
}
int main(int, char*[])
{
B b = GetB();
{
A a = GetA();
A a2(a);
std::cout << "scope\n";
}
return 0;
}
четвер, 12 серпня 2010 р.
понеділок, 7 червня 2010 р.
четвер, 3 червня 2010 р.
C++0x multi-threading
If you want to play with new functionality for multi-threading in C++ for free you need gcc-4.5 and Linux, MinGW currently doesn't support this functionality (but it has all required headers). To compile programm you need to specify complier flag -std=c++0x and linker flag -pthread. VC++ currently have not this functionality too. Here is small example:
#include <thread>
#include <mutex>
#include <iostream>
#include <unistd.h>
class MyJob
{
public:
MyJob(std::mutex* m) : m(m){}
void operator()()
{
for (int i = 0; i < 5; ++i)
{
{
std::lock_guard<std::mutex> lock(*m);
std::thread::id id = std::this_thread::get_id();
std::cout << "Thread " << id << std::endl;
}
sleep(1);
}
}
private:
std::mutex* m;
};
int main()
{
std::mutex m;
std::thread t1{MyJob(&m)};
std::thread t2{MyJob(&m)};
t1.join();
t2.join();
return 0;
}
#include <thread>
#include <mutex>
#include <iostream>
#include <unistd.h>
class MyJob
{
public:
MyJob(std::mutex* m) : m(m){}
void operator()()
{
for (int i = 0; i < 5; ++i)
{
{
std::lock_guard<std::mutex> lock(*m);
std::thread::id id = std::this_thread::get_id();
std::cout << "Thread " << id << std::endl;
}
sleep(1);
}
}
private:
std::mutex* m;
};
int main()
{
std::mutex m;
std::thread t1{MyJob(&m)};
std::thread t2{MyJob(&m)};
t1.join();
t2.join();
return 0;
}
четвер, 13 травня 2010 р.
вівторок, 27 квітня 2010 р.
CMake + VC++2010
There is now the bug in CMake 2.8.1 with Visual Studio 2010 Express - compiler checks doesn't work. Here is http://www.itk.org/Bug/view.php?id=10539 patch for this bug.
вівторок, 20 квітня 2010 р.
Time convertions with daylight saving
I've found how to convert time from UTC to local time with daylight saving correction:
1. Use mktime() to create time from string(or some thing else)
2. get time from gmtime() and localtime()
3. calculate difference between time to get timezone offset (or use _get_timezone)
4. use tm::tm_isdst to determine apply or not daylight saving
5. use _get_daylight() to get value in hours of daylight correction.
1. Use mktime() to create time from string(or some thing else)
2. get time from gmtime() and localtime()
3. calculate difference between time to get timezone offset (or use _get_timezone)
4. use tm::tm_isdst to determine apply or not daylight saving
5. use _get_daylight() to get value in hours of daylight correction.
понеділок, 19 квітня 2010 р.
Підписатися на:
Дописи (Atom)
