ugexe
December 5th, 2004, 06:15 AM
Ok the windows part of the code works fine, but the rest causes a segmentation fault at runtime. I am calling it with ThreadLib::Create(functionName,(void*)'a');
Also, since im not sure if anyone will be able to track down the problem, I would be willing to use a different wrapper as long as it is a cross platform, VERY simple (I dont want a bunch of extra features.. I really only need to be able to create 1 thread other then the main thread), and preferablly 1 file. :(
#ifdef __WIN32__
#include <windows.h>
#include <map>
#else
#include <pthread.h>
#include <unistd.h>
#endif
namespace ThreadLib
{
typedef void (*ThreadFunc)(void*);
#ifdef __WIN32__
typedef DWORD ThreadID;
extern std::map< DWORD, HANDLE > g_handlemap;
#else
typedef pthread_t ThreadID;
#endif
class DummyData
{
public:
ThreadFunc m_func;
void* m_data;
};
#ifdef __WIN32__
DWORD WINAPI DummyRun( void* p_data );
#else
void* DummyRun( void* p_data );
#endif
inline ThreadID Create( ThreadFunc p_func, void* p_param )
{
ThreadID t;
DummyData* data = new DummyData;
data->m_func = p_func;
data->m_data = p_param;
#ifdef __WIN32__
HANDLE h;
h = CreateThread( NULL, 0, DummyRun, data, 0, &t );
if( h != 0 )
{
g_handlemap[t] = h;
}
#else
//ERROR HAPPENS HERE
pthread_create( &t, 0, DummyRun, data );
#endif
if( t == 0 )
{
delete data;
// throw an error
}
return t;
}
inline ThreadID GetID()
{
#ifdef __WIN32__
return GetCurrentThreadId();
#else
return pthread_self();
#endif
}
inline void WaitForFinish( ThreadID p_thread )
{
#ifdef __WIN32__
WaitForSingleObject( g_handlemap[p_thread], INFINITE );
CloseHandle( g_handlemap[p_thread] );
g_handlemap.erase( p_thread );
#else
pthread_join( p_thread, NULL );
#endif
}
inline void Kill( ThreadID& p_thread )
{
#ifdef __WIN32__
TerminateThread( g_handlemap[p_thread], 0 );
CloseHandle( g_handlemap[p_thread] );
g_handlemap.erase( p_thread );
#else
pthread_cancel( p_thread );
#endif
}
inline void YieldThread( int p_milliseconds = 1 )
{
#ifdef __WIN32__
Sleep( p_milliseconds );
#else
usleep( p_milliseconds * 1000 );
#endif
}
#ifdef __WIN32__
std::map< DWORD, HANDLE > g_handlemap;
#endif
#ifdef __WIN32__
DWORD WINAPI DummyRun( void* p_data )
#else
void* DummyRun( void* p_data )
#endif
{
DummyData* data = (DummyData*)p_data;
data->m_func( data->m_data );
delete data;
return 0;
}
}
Also, since im not sure if anyone will be able to track down the problem, I would be willing to use a different wrapper as long as it is a cross platform, VERY simple (I dont want a bunch of extra features.. I really only need to be able to create 1 thread other then the main thread), and preferablly 1 file. :(
#ifdef __WIN32__
#include <windows.h>
#include <map>
#else
#include <pthread.h>
#include <unistd.h>
#endif
namespace ThreadLib
{
typedef void (*ThreadFunc)(void*);
#ifdef __WIN32__
typedef DWORD ThreadID;
extern std::map< DWORD, HANDLE > g_handlemap;
#else
typedef pthread_t ThreadID;
#endif
class DummyData
{
public:
ThreadFunc m_func;
void* m_data;
};
#ifdef __WIN32__
DWORD WINAPI DummyRun( void* p_data );
#else
void* DummyRun( void* p_data );
#endif
inline ThreadID Create( ThreadFunc p_func, void* p_param )
{
ThreadID t;
DummyData* data = new DummyData;
data->m_func = p_func;
data->m_data = p_param;
#ifdef __WIN32__
HANDLE h;
h = CreateThread( NULL, 0, DummyRun, data, 0, &t );
if( h != 0 )
{
g_handlemap[t] = h;
}
#else
//ERROR HAPPENS HERE
pthread_create( &t, 0, DummyRun, data );
#endif
if( t == 0 )
{
delete data;
// throw an error
}
return t;
}
inline ThreadID GetID()
{
#ifdef __WIN32__
return GetCurrentThreadId();
#else
return pthread_self();
#endif
}
inline void WaitForFinish( ThreadID p_thread )
{
#ifdef __WIN32__
WaitForSingleObject( g_handlemap[p_thread], INFINITE );
CloseHandle( g_handlemap[p_thread] );
g_handlemap.erase( p_thread );
#else
pthread_join( p_thread, NULL );
#endif
}
inline void Kill( ThreadID& p_thread )
{
#ifdef __WIN32__
TerminateThread( g_handlemap[p_thread], 0 );
CloseHandle( g_handlemap[p_thread] );
g_handlemap.erase( p_thread );
#else
pthread_cancel( p_thread );
#endif
}
inline void YieldThread( int p_milliseconds = 1 )
{
#ifdef __WIN32__
Sleep( p_milliseconds );
#else
usleep( p_milliseconds * 1000 );
#endif
}
#ifdef __WIN32__
std::map< DWORD, HANDLE > g_handlemap;
#endif
#ifdef __WIN32__
DWORD WINAPI DummyRun( void* p_data )
#else
void* DummyRun( void* p_data )
#endif
{
DummyData* data = (DummyData*)p_data;
data->m_func( data->m_data );
delete data;
return 0;
}
}