Monday 22 January 2007

pthread on slackware on vmware on windows xp

Yesterday, I had to write a multithreaded application using posix threads on linux, for one of my friends.
We had thought that we could do it by installing slackware linux on a vmware virtual machine running on windows xp. brillant idea, huh?
For early 4 hours, everything was ok. We drawn up state diagrams and wrote trivial code parts to use later.
After that, we set up threading and thread syncronisation using mutexes. Then the nightmare started. Results were inconsistent. Sometimes things went well, and sometimes deadlocks occured.
We could not obviate managing of the conditional variables (pthreads mutexes), and worked different solutions, algorithms for about 4 hours. It was suspicious that Sleep(int) method worked seconds-based, not miliseconds-based as expected on a linux. and bigger values like sleep(1000) did not cause a sleep.
We renounced at late of the evening.
The day after, we requested one of our friends to try the same source on his native linux (pardus).
And a dramatic notice! It had given the expected(correct) results.
-
We think it is vmware. But already so many multithreaded applications work on it. Is it posix threads library version on slackware? We don't know yet.

Conditional("Debug")

There's a pretty cool feature in C#(or MSIL compiler?). On a method, you can put a conditional attribute, and calls to that method are only compiled on debug mode. This gives a perfect way to trace how are things going on.

public class Test{
[Conditional("Debug")]
public void Method()
{
// ....
}
}


Today, I've found a bug in my code, associated with this.
[Conditional("DEBUG")]

I had written the string in capital letters. But in runtime or compile-time, there's not an error about it. It simply and silently "doesn't work."

It took an hour to realize this. It would be great if they used an enum, for examle:
[Conditional(ConditionalModes.Debug)]

or different attribute types for each mode:
[DebugConditional]

This is not a big thing of course, but these kind of design decisions are important.
I always prefer compile time errors -where possible- :)

Thursday 18 January 2007

SqlDataReader

Just like DbCommand objects, using DataReader objects is a pain.
Everytime the same routine: check value, cast, set.
I've automated this with RTTI, too:

public class DbDataReaderReviver
{
public object ActivateObject(DbDataReader reader, Type typeOfObject)
{
object myObj = System.Activator.CreateInstance(typeOfObject);
FieldInfo[] objFields = typeOfObject.GetFields(BindingFlags.SetField);
string fieldName;
object fieldValue;
foreach (FieldInfo finfo in objFields)
{
fieldName = finfo.Name;
fieldValue = reader[fieldName];
if (fieldValue == null) continue;
finfo.SetValue(myObj, fieldValue);
}
return myObj;
}
}

sqlcommand

I'm very tired of adding parameters to sqlcommand (or oracledommand, or -best- dbcommand) objects.

I have written a smart code with RTTI, that fills the parameters of dbcommand objects using an "object instance". First it searches for the member fields of the object, then populates the dbcommand object with these name-value pairs.

I didn't tested this code, but I know this approach works well.


public class DbCommandBuilder
{
/// summary
/// Populates a DbCommand object with the public fields of
/// the given object as parameters.
/// summary
/// param name="command" DbCommand reference to fill
/// param name="obj" Object to get fields of
public void BuildCommand(DbCommand command, object obj)
{
Type type = obj.GetType();
FieldInfo[] finfos = type.GetFields(BindingFlags.Public);
string fieldName;
object fieldValue;
foreach (FieldInfo fin in finfos)
{
fieldName = fin.Name;
fieldValue = fin.GetValue(obj);
command.Parameters.Add(fieldName);
command.Parameters[fieldName].Value = fieldValue;
}
}
}

my desk


This is where I work at home.
I love my desk.
Posted by Picasa

fresh start

Or better, from now on I will start a seperate blog in English.

In fact, I don't like writing in English so much, writing in Turkish is much more joy for me.

Apologizes to good-english-knowers for my non-perfect English. I hope somebody corrects me when I make mistake.

wordpress

Aslına bakarsanız blogspot'ta günlük tutmayı düşünmüştüm. Ama bir hesap açıp biraz kurcaladıktan sonra (yavaşlığını bir yana bırakırsak) wordpress'ten buraya taşınmanın attan inip eşeğe binmek olacağını farkettim.

Export import bile yapmaktan aciz :(

Velhasıl-ı kelam:
http://gorkem.wordpress.com