Jesse Maurais

  • Content count

  • Joined

  • Last visited

Community Reputation

122 Neutral

About Jesse Maurais

  • Rank
  1. Multithreading (with DevIL)

    Quote:Original post by gjaegy FYI I am using boost::mutex and their implementation is quite good. I guess you don't want to introduce any dependency to boost in the library, but it might give you another implementation inspiration ;) Can't believe I missed this before, but you could use boost::mutex without dedicating yourself to a long-term dependency problem, because it's being adopted as the standard in C++09 (as well as boost::thread). And from what I understand a portable sockets implementation for inter-process communication will be in there as well.
  2. Multithreading (with DevIL)

    The only change I would make to the code above is the use of strerror to format the return values of pthread functions. And to write them using cerr rather than cout.
  3. I went through the chapter (in the online book @ about creating C data arrays that can be written to and read from in lua. I modified it a bit to use float instead of double, like so... #include <lua.hpp> typedef struct { int size; float values[1]; } NumArray; static NumArray *check( lua_State *vm, int index ) { void *user = luaL_checkudata( vm,index,"array" ); luaL_argcheck( vm,user!=0,index,"array expected" ); return (NumArray*) user; } static float &getvalue( lua_State *vm ) { NumArray *a = check( vm,1 ); int index = luaL_checkint( vm,2 ); luaL_argcheck( vm,1<=index<=a->size,2,"out of range" ); return a->values[index-1]; } static int newarray( lua_State *vm ) { int n = luaL_checkint( vm,1 ); size_t nbytes = sizeof( NumArray ) + ( n-1 )*sizeof( float ); NumArray * a = (NumArray*) lua_newuserdata( vm,nbytes ); luaL_getmetatable( vm,"array" ); lua_setmetatable( vm,-2 ); a->size = n; return 1; } static int setarray( lua_State *vm ) { getvalue(vm) = (float) luaL_checknumber( vm,3 ); return 0; } static int getarray( lua_State *vm ) { lua_pushnumber( vm,(lua_Number)getvalue(vm) ); return 1; } static int getsize( lua_State *vm ) { NumArray *a = check( vm,1 ); lua_pushnumber( vm,a->size ); return 1; } static int tostring( lua_State *vm ) { NumArray *a = check( vm,1 ); lua_pushfstring( vm,"array(%d)",a->size ); return 1; } int main( int argc, char **argv ) { lua_State * vm = lua_open(); luaL_openlibs( vm ); luaL_newmetatable( vm,"array" ); lua_pushstring( vm,"__tostring" ); lua_pushcfunction( vm,tostring ); lua_settable( vm,-3 ); lua_pushstring( vm,"__index" ); lua_pushcfunction( vm,getarray ); lua_settable( vm,-3 ); lua_pushstring( vm,"__newindex" ); lua_pushcfunction( vm,setarray ); lua_settable( vm,-3 ); lua_register( vm,"array",newarray ); luaL_dofile( vm,"test5.lua" ); lua_close( vm ); return 0; } And here's the test code "test5.lua" a = array(1000) print(a) for i=1,1000 do a[i]=1/i end print(a[10]) a[1] = 3.14 print(a[1]) Finally, my output array(1000) 0.10000000149012 3.1400001049042 But obviously this is not what one expects. What it should be, and what it was when using double instead of float is array(1000) 0.1 3.14 I'm wondering where the error crops up. I thought at first it was due to casting, so I put some explicit casting in the code, which did not help. Any ideas out there?
  4. Multithreading (with DevIL)

    As mentioned above, its true that mutexes will block if the memory is already locked, but this should not be a problem. When blocked the other threads wake up. Thats a good thing. Blocking gives process management over to the kernel. Let the kernel do its job.
  5. Multithreading (with DevIL)

    Just lock/unlock a mutex around the sensitive memory to prevent it from being clobbered. Easiest solution I can think of. This is Unix specific but has good information on the topic.