Saturday, September 11, 2010

Moving to wordpress

Because of the irritating editing bugs that blogspot has had for as long time as I have used it I have decided to give Wordpress a try. A nice benefit of wordpress is that I can easily updated blog posts and manage it through a nifty iphone app.

So until I know better ;) you can find me at

Thanks for reading my blog
Kjell Hedström (a.k.a. KjellKod)

Thursday, August 12, 2010

Active Object

Active Object a.k.a Actor is a great design pattern that should be used way more in my opinion. Instead of using raw threads you have them encapsuled in an object. This gives you easy concurrency and good handling of threads that avoids many of the pitfalls of using raw threads (races, etc). I've put together an example expired by Herb Sutter's article "Prefer using active objects instead of naked threads" but using generic callbacks since I do not have access to any C++0x yet 

Herb shows how you with C++0x can do this:

class Backgrounder {
  // Save and Print will execute the lambdas in the 
  // background thread (active object thread)
  void Save(string filename){a.Send([=]{ … }); } 
  void Print(Data& data) {a.Send([=, &data] { … } ); }
  Active a;
Mmm, nice way of utilizing lambda expressions, don't you think? However, with just plain old pre-C++0x (like most of us use in 2010) You can with normal C++ and a touch of template magic get this:

class Backgrounder {
  void Save(string filename){
    a.send(bind(this, &Backgrounder::BgSave, filename));  }

  void Print( Data& data ){
    a.send(bind(this, &Backgrounder::BgPrint, data));
  void BgSave(string filename){...}
  void BgPrint(Data&; data){...}
  Active a;

Easy as 1, 2, 3 don't you think? And that's all it takes for working with an Active object that will execute jobs in the background. Why EVER use naked threads again for normal mundane tasks like bakground processing, saving files etc?

For more details and information how the Active works you can find my article here. It is heavily influenced by Herbs article and his Gotw 83 (Generic Callbacks :)

Till next time. Cheers

Wednesday, June 30, 2010

Bevisat: Humlan KAN flyga

Som en liten sidolänk på HiQ bloggen så hittade jag till en kul artikel som visade att humlor nu vetenskapligen bevisat kan flyga enligt de fysikaliska lagarna. Äntligen har forskarna kommit ikapp med sin matematiska modell och bevisat att det faktiskt är vetenskapligt, tekniskt möjligt för humlan att flyga.

Detta irritationsmoment har gäckat forskare sedan 1930 då man kom fram till att Humlan inte kan flyga, men gör det trots vetenskapligt bevisade fysikaliska lagar. Nu förra året så blev det då bevisat - om ni inte redan hängt med i humlesvängen så har ni chansen nu: Smoke visualization of free-flying bumblebees indicates independent leading-edge vortices on each wing pair

Sunday, November 15, 2009

Article at CodeProject C++ Lock Free Circular Queue

I attended Herb Sutter's Effective Concurrency Seminar (Europe 2009), a very inspiring seminar where he approached multi-core targeted programming. One thing we worked with was the dangers of Lock-Free programming and hazards of volatile in C++.

I got curious since I've seen a couple of simple single producer -> single consumer Circular Queue implementations that are implemented as Lock-Free and using just that, the important state variables defined as volatile. So how come they work (being used for years), or do they?

Volatile as you might know is not intended for multi-threading but for accessing hardware devices (and more). It turns out that volatile plays a minor role and that it's mostly the compiler and computer platform that decides whether or not this kind of Circular Queues will be safe to use.

I have implemented a thread safe circular queue in C++ and explain in this article when it works and when it will not work. This is an area outside the scope of the C++ standard (until C++0x) and is suitable for (at least) x86 or x64 platforms.

For those of you who want to read up on the inner workings of the Circular Queue, I have provided a short description of it and my implementation. For those of you who are already familiar with it and just want to know if this thread safe circular queue is for real, just go straight to the Implementation section. You can find the whole article either at CodeProject or at my page.

Saturday, September 6, 2008

Hunting in the North of Sweden

So finally after many, many hours of studying, even more hours at the shooting range practising with both clay pigeons & shotgun or on the moose range with my Howa 1500 (.308 Winchester) it is time for my first moose hunting trip. It'll be near Luleå - actually closer to Boden - which is very much in the North of Sweden.

I trust my Howa will be up for the task (_ref_) although I'm sure it's more in the Hunter's skill than in the weapon of choice whether or not the hunt is successful or not.

Today we scouted the area where I'll be on my first hunt ever. It seems to have a lot of wild life and even some brown bears can show up. We have license to hunt brown bear too,. but unless I'm up in one of the towers where a wounded bear can't easily get to me I will simply never even try for that game.

If opportunity arrise I hope that I can take a side trip to see if there are any hazelhen ( a kind of woodland groose) that can be dinner ;-) but that's more for the challenge - I don't think I would get lucky in this area.

I honestly don't think I'll manage to even glimpse one of big northern moose but I sure hope so.

Sunday, June 29, 2008

FRA Lagen och dess verkningar

(This one is in Swedish folks ....)

Angående FRA lagen och även mina kommentarer kring FRA chefens utlägg där han inte gillar att folket säger vad de tycker.

Min åsikt är att lagen är vidrig i det att FRA får makten att bygga ut en infrastruktur och medel för att kunna göra avlyssning på en helt annan nivå än de kan idag.

Syftet är gott nog IDAG,. problemet är att klimatet i Sverige kan ändras. För inte så länge sedan så avlyssnade Polisen och Säpo väldigt många personer som tillhörde vänsterpartiet. Med en lika liten ändring i dagens politiska klimat från då till nu så skulle FRA kunna använda det snart utbyggda spanar-infrastrukturen till att avlyssna/spana på de nu intressanta personerna ,... kanske de är miljödebattörer, veganer, vänsterpartister, torrent användare, you name it

Frågan är inte OM utan NÄR det kommer att missbrukas. Finns ramverket och strukturen så är steget väldigt litet att gå från att avlyssna en viss typ av kommunikation till att avlyssna en grupp av människor.

Jag hoppas det här blir en valfråga. Om det blir det så är chansen väldigt stor att jag röstar på det parti som omedelbart vill ta bort denna stygghet - oavsett vilket parti det handlar om. Så viktigt tycker jag detta är.

Kjell Hedström, Moderat och MUF-vän.

Sunday, October 7, 2007

KSignal - A Signal and Slot (Observer Design Pattern) implementation

Signal and slots is a concept developed from Qt. It is basically a generalized implementation of the Observer pattern. The purpose of the KjellKod signal-n-slot is to have the power of Observer pattern - but made with generic function callback. The most famous implementations of Signals and Slots are made by Qt and Boost. My own implementation of signals and slots (KSignal) were made in 2006-2007 when I wanted to learn more about C++ generic function callbacks. Now it's a fully functional library that is in use in multiple projects.

My own signal-n-slot definition
The KjellKod signal-slot mechanism is a C++, cross platform compatible implementation of the Observer design pattern. Signals are basically notifications of an (observable) event. Signals are connected to Slots which each store a function callback to an object. A signal can be connected to many slots and all slots/receivers are notified when the signal is emitted.

A signal can be just a notification, or it can pass along information. This make's it very handy when creating loosely coupled software systems.

The only requirement on an objects function callback that is to be stored within a slot is that it must be able to receive the same argument(s) as the publishing signal is sending. I.e. If it is a void signal, then the slot (stored callback function) must have a zero argument list. Likewise, if the signal sends out an argument, the receiving function must have that argument type, and only that argument in its argument list. If this requirement is not adhered to, the compiler will generate an error message. I.e. signal/slot is typesafe.

Modified below: 2010-08-12
Thanks to " andy_t_roo" (Andrew)  comment below which directed me to FastDelegate which in its turn made me look at GotW 83 where Herb Sutter gives an excellent (as always) example of how to encapsulate generic callbacks.

Back when I made KSignals version 1 (dynamic memory) and version 2 (static memory for embedded systems) I had not read GotW_83: Generic Callbacks  but I'm still kind of happy that I unbeknowst of much still managed to on my own come up with something very similar, although lacking some of the finer points Herb makes.

Either way I strongly recommend you to read Herbs Gotw article since it's very, very easy read and explains better than any other function callback or function pointer text that I've read how to setup the basics that are needed for Signals and Slots. If you think this is something good, then please go ahead and use my versions and tailor make them as you please (which should be easy since they're only few lines with bare bone code)