Howdy folks,
I recently purchased a book on C++ MUD creation and it features alot of
nifty tidbits.
The book is
MUD GAME PROGRAMMING by Ron Penton
Publisher: Premier Press
Anyways of particular interest and what drew me to the book was the
crossplatform threading library.
But the problem is it appears to be broken at a single line of code,
and I can't seem to refactor it to get it work. It's likely something
incredibly simple I'm missing here.
There are only 2 files involved, one is the header and one is the
implementation of the header a "testapp" as it were.
The test app fails to compile properly with this message
ThreadLibTest.c pp: In function 'void PrintThread(voi d*)':
ThreadLibTest.c pp:8: error: cast from 'void*' to 'char' loses
precision
Here is the code for the testapp
#include <iostream>
#include <unistd.h>
#include "ThreadLib. h"
int main() {
ThreadLib::Thre adID a, b;
a = ThreadLib::Crea te( PrintThread, (void*)'a' );
b = ThreadLib::Crea te( PrintThread, (void*)'b' );
ThreadLib::Wait ForFinish( b );
ThreadLib::Wait ForFinish( a );
char c;
cin >c;
return 0;
}
void PrintThread( void* data ) {
// convert the data passed in into a character.
char c = (char)data;
for( int i = 0; i < 10000; i++ ) {
cout << c;
cout.flush();
}
}
And here is the code for the ThreadLib.h file
#include <unistd.h>
#ifdef WIN32 // Windows 95 and above
#include <windows.h>
#else // Linux
#include <pthread.h>
#endif
namespace ThreadLib{
typedef void (*ThreadFunc)(v oid*);
#ifdef WIN32 // Windows 95 and above
typedef DWORD ThreadID;
std::map< DWORD, HANDLE g_handlemap;
#else // Linux
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
{
// convert the dummy data
DummyData* data = (DummyData*)p_d ata;
// run the function with the given data
data->m_func( data->m_data );
// now delete the data
delete data;
// and return 0.
return 0;
}
inline ThreadID Create(ThreadFu nc p_func, void* p_param ){
ThreadID t;
// create a new dummy data block
DummyData* data = new DummyData;
data->m_func = p_func;
data->m_data = p_param;
#ifdef WIN32 // create a WIN32 thread
HANDLE h;
h = CreateThread( NULL, 0, DummyRun, data, 0, &t );
if( h != 0 ) {
// insert the handle into the handlemap
g_handlemap[t] = h;
}
#else // create a Linux thread
pthread_create( &t, 0, DummyRun, data );
#endif
if( t == 0 ) {
// delete the data first
delete data;
// throw an error
//throw Exception( CreationFailure );
}
return t;
}
inline ThreadID GetID() {
#ifdef WIN32
return GetCurrentThrea dId();
#else
return pthread_self();
#endif
}
inline void WaitForFinish( ThreadID p_thread ) {
#ifdef WIN32
// look up the handle and wait for the thread to finish
WaitForSingleOb ject( g_handlemap[p_thread], INFINITE );
// close the handle of the thread
CloseHandle( g_handlemap[p_thread] );
// remove the handle from the map
g_handlemap.era se( p_thread );
#else
// "join" the thread. This essentially transfers control over to
// the thread and waits for it to finish.
pthread_join( p_thread, NULL );
#endif
}
inline void Kill( ThreadID& p_thread ) {
#ifdef WIN32
// terminate the thread
TerminateThread ( g_handlemap[p_thread], 0 );
// close the handle of the thread
CloseHandle( g_handlemap[p_thread] );
// remove the handle from the map
g_handlemap.era se( p_thread );
#else
// cancel the thread.
pthread_cancel( p_thread );
#endif
}
inline void YieldThread( int p_milliseconds = 1 ) {
#ifdef WIN32
Sleep( p_milliseconds );
#else
usleep( p_milliseconds * 1000 );
#endif
}
}
Any help on this would be appreciated, I tried to go to the authors
website but it appears to be down.
I'm just so frustrated because to me at least this code looks exactly
right.
Anyways thanks in advance!
Regards, 3 2002
Hi,
This cast,
(void*)'a'
means you are casting a character (byte) value to a void pointer. This
is where your error is coming from.
I think you should probably try double quotes:
(void*)"a"
Then you will be casting a char pointer to a void pointer, which seems
like what you intended to do.
Double quotes mean a string (array of chars, accessed via char*), and
single quotes mean a single character value (byte).
Regards,
Markus. sm*****@gmail.c om wrote:
Howdy folks,
I recently purchased a book on C++ MUD creation and it features alot of
nifty tidbits.
The book is
MUD GAME PROGRAMMING by Ron Penton
Publisher: Premier Press
Anyways of particular interest and what drew me to the book was the
crossplatform threading library.
But the problem is it appears to be broken at a single line of code,
and I can't seem to refactor it to get it work. It's likely something
incredibly simple I'm missing here.
There are only 2 files involved, one is the header and one is the
implementation of the header a "testapp" as it were.
The test app fails to compile properly with this message
ThreadLibTest.c pp: In function 'void PrintThread(voi d*)':
ThreadLibTest.c pp:8: error: cast from 'void*' to 'char' loses
precision
Here is the code for the testapp
#include <iostream>
#include <unistd.h>
#include "ThreadLib. h"
int main() {
ThreadLib::Thre adID a, b;
a = ThreadLib::Crea te( PrintThread, (void*)'a' );
b = ThreadLib::Crea te( PrintThread, (void*)'b' );
ThreadLib::Wait ForFinish( b );
ThreadLib::Wait ForFinish( a );
char c;
cin >c;
return 0;
}
void PrintThread( void* data ) {
// convert the data passed in into a character.
char c = (char)data;
for( int i = 0; i < 10000; i++ ) {
cout << c;
cout.flush();
}
}
And here is the code for the ThreadLib.h file
#include <unistd.h>
#ifdef WIN32 // Windows 95 and above
#include <windows.h>
#else // Linux
#include <pthread.h>
#endif
namespace ThreadLib{
typedef void (*ThreadFunc)(v oid*);
#ifdef WIN32 // Windows 95 and above
typedef DWORD ThreadID;
std::map< DWORD, HANDLE g_handlemap;
#else // Linux
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
{
// convert the dummy data
DummyData* data = (DummyData*)p_d ata;
// run the function with the given data
data->m_func( data->m_data );
// now delete the data
delete data;
// and return 0.
return 0;
}
inline ThreadID Create(ThreadFu nc p_func, void* p_param ){
ThreadID t;
// create a new dummy data block
DummyData* data = new DummyData;
data->m_func = p_func;
data->m_data = p_param;
#ifdef WIN32 // create a WIN32 thread
HANDLE h;
h = CreateThread( NULL, 0, DummyRun, data, 0, &t );
if( h != 0 ) {
// insert the handle into the handlemap
g_handlemap[t] = h;
}
#else // create a Linux thread
pthread_create( &t, 0, DummyRun, data );
#endif
if( t == 0 ) {
// delete the data first
delete data;
// throw an error
//throw Exception( CreationFailure );
}
return t;
}
inline ThreadID GetID() {
#ifdef WIN32
return GetCurrentThrea dId();
#else
return pthread_self();
#endif
}
inline void WaitForFinish( ThreadID p_thread ) {
#ifdef WIN32
// look up the handle and wait for the thread to finish
WaitForSingleOb ject( g_handlemap[p_thread], INFINITE );
// close the handle of the thread
CloseHandle( g_handlemap[p_thread] );
// remove the handle from the map
g_handlemap.era se( p_thread );
#else
// "join" the thread. This essentially transfers control over to
// the thread and waits for it to finish.
pthread_join( p_thread, NULL );
#endif
}
inline void Kill( ThreadID& p_thread ) {
#ifdef WIN32
// terminate the thread
TerminateThread ( g_handlemap[p_thread], 0 );
// close the handle of the thread
CloseHandle( g_handlemap[p_thread] );
// remove the handle from the map
g_handlemap.era se( p_thread );
#else
// cancel the thread.
pthread_cancel( p_thread );
#endif
}
inline void YieldThread( int p_milliseconds = 1 ) {
#ifdef WIN32
Sleep( p_milliseconds );
#else
usleep( p_milliseconds * 1000 );
#endif
}
}
Any help on this would be appreciated, I tried to go to the authors
website but it appears to be down.
I'm just so frustrated because to me at least this code looks exactly
right.
Anyways thanks in advance!
Regards,
Oh my god!
I can't believe I missed that, this has had me tearing my hair out for
days!
Anyways thank you very, very much!
Markus Svilans wrote:
Hi,
This cast,
(void*)'a'
means you are casting a character (byte) value to a void pointer. This
is where your error is coming from.
I think you should probably try double quotes:
(void*)"a"
Then you will be casting a char pointer to a void pointer, which seems
like what you intended to do.
Double quotes mean a string (array of chars, accessed via char*), and
single quotes mean a single character value (byte).
Regards,
Markus. sm*****@gmail.c om wrote:
Howdy folks,
I recently purchased a book on C++ MUD creation and it features alot of
nifty tidbits.
The book is
MUD GAME PROGRAMMING by Ron Penton
Publisher: Premier Press
Anyways of particular interest and what drew me to the book was the
crossplatform threading library.
But the problem is it appears to be broken at a single line of code,
and I can't seem to refactor it to get it work. It's likely something
incredibly simple I'm missing here.
There are only 2 files involved, one is the header and one is the
implementation of the header a "testapp" as it were.
The test app fails to compile properly with this message
ThreadLibTest.c pp: In function 'void PrintThread(voi d*)':
ThreadLibTest.c pp:8: error: cast from 'void*' to 'char' loses
precision
Here is the code for the testapp
#include <iostream>
#include <unistd.h>
#include "ThreadLib. h"
int main() {
ThreadLib::Thre adID a, b;
a = ThreadLib::Crea te( PrintThread, (void*)'a' );
b = ThreadLib::Crea te( PrintThread, (void*)'b' );
ThreadLib::Wait ForFinish( b );
ThreadLib::Wait ForFinish( a );
char c;
cin >c;
return 0;
}
void PrintThread( void* data ) {
// convert the data passed in into a character.
char c = (char)data;
for( int i = 0; i < 10000; i++ ) {
cout << c;
cout.flush();
}
}
And here is the code for the ThreadLib.h file
#include <unistd.h>
#ifdef WIN32 // Windows 95 and above
#include <windows.h>
#else // Linux
#include <pthread.h>
#endif
namespace ThreadLib{
typedef void (*ThreadFunc)(v oid*);
#ifdef WIN32 // Windows 95 and above
typedef DWORD ThreadID;
std::map< DWORD, HANDLE g_handlemap;
#else // Linux
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
{
// convert the dummy data
DummyData* data = (DummyData*)p_d ata;
// run the function with the given data
data->m_func( data->m_data );
// now delete the data
delete data;
// and return 0.
return 0;
}
inline ThreadID Create(ThreadFu nc p_func, void* p_param ){
ThreadID t;
// create a new dummy data block
DummyData* data = new DummyData;
data->m_func = p_func;
data->m_data = p_param;
#ifdef WIN32 // create a WIN32 thread
HANDLE h;
h = CreateThread( NULL, 0, DummyRun, data, 0, &t );
if( h != 0 ) {
// insert the handle into the handlemap
g_handlemap[t] = h;
}
#else // create a Linux thread
pthread_create( &t, 0, DummyRun, data );
#endif
if( t == 0 ) {
// delete the data first
delete data;
// throw an error
//throw Exception( CreationFailure );
}
return t;
}
inline ThreadID GetID() {
#ifdef WIN32
return GetCurrentThrea dId();
#else
return pthread_self();
#endif
}
inline void WaitForFinish( ThreadID p_thread ) {
#ifdef WIN32
// look up the handle and wait for the thread to finish
WaitForSingleOb ject( g_handlemap[p_thread], INFINITE );
// close the handle of the thread
CloseHandle( g_handlemap[p_thread] );
// remove the handle from the map
g_handlemap.era se( p_thread );
#else
// "join" the thread. This essentially transfers control over to
// the thread and waits for it to finish.
pthread_join( p_thread, NULL );
#endif
}
inline void Kill( ThreadID& p_thread ) {
#ifdef WIN32
// terminate the thread
TerminateThread ( g_handlemap[p_thread], 0 );
// close the handle of the thread
CloseHandle( g_handlemap[p_thread] );
// remove the handle from the map
g_handlemap.era se( p_thread );
#else
// cancel the thread.
pthread_cancel( p_thread );
#endif
}
inline void YieldThread( int p_milliseconds = 1 ) {
#ifdef WIN32
Sleep( p_milliseconds );
#else
usleep( p_milliseconds * 1000 );
#endif
}
}
Any help on this would be appreciated, I tried to go to the authors
website but it appears to be down.
I'm just so frustrated because to me at least this code looks exactly
right.
Anyways thanks in advance!
Regards,
sm*****@gmail.c om wrote:
Oh my god!
I can't believe I missed that, this has had me tearing my hair out for
days!
Anyways thank you very, very much!
Regards,
Glad to hear it :D This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Jon Skeet [C# MVP] |
last post by:
Please excuse the cross-post - I'm pretty sure I've had interest in the
article on all the groups this is posted to.
I've finally managed to finish my article on multi-threading - at least
for the moment. I'd be *very* grateful if people with any interest in
multi-threading would read it (even just bits of it - it's somewhat
long to go through the whole thing!) to check for accuracy,
effectiveness of examples, etc.
Feel free to mail...
|
by: Steven T. Hatton |
last post by:
Now that I have a better grasp of the scope and capabilities of the C++
Standard Library, I understand that products such as Qt actually provide
much of the same functionality through their own libraries. I'm not sure
if that's a good thing or not. AFAIK, most of Qt is compatable with the
Standard Library. That is, QLT can interoperate with STL, and you can
convert back and forth between std::string and Qt::QString, etc.
Are there any...
|
by: Tom B |
last post by:
I've written the code below to try and figure out how threading works.
My assumption is that when starting a new thread it would run at the same
time as the original thread.
Am I wrong?
I've made a simple form with two text labels and a button. When I click the
button, it creates a thread that updates one label, and the main code
updates the other label.
It appears that the created thread doesn't run until the main thread is
|
by: Z D |
last post by:
Hello,
I'm having a strange problem that is probably due to my lack of
understanding of how threading & COM Interop works in a WinForms.NET
application.
Here's the situation:
I have a 3rd party COM component that takes about 5 seconds to run one of
its functions (Network IO bound call). Since I dont want my GUI to freeze
|
by: KC |
last post by:
Hey,
I'm trying to implement a cancel button in my app (this is after the bulk of
the program has been built). To do this I, of course, need to use a thread
to run the time consuming method while the UI continues on it's merry way. I
created a basic thread just as an initial test and I half expected it to
crash - and it did. My problem is I don't understand why and that's making
it hard to debug.
The whole thing crashes (I get the old...
| |
by: cj |
last post by:
I need more information to read about threading in Visual Basic.
Hopefully something linear w/o too many links to get lost in. I've been
to
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconThreading.asp
but it is too confusing. I can't find any references to STA threading
model or what exactly this is doing:
<STAThread()> Public Shared Sub Main(ByVal CmdArgs() As String)
Application.Run(New Form1)
|
by: Steve Smith |
last post by:
I have written a winforms application that launches approximately 150
threads with
Thread.ThreadStart()
Each thread uses CDO 1.21 to logon to a different Exchange mailbox and
send/receive a number of mail messages, reporting back to the UI thread
through the use of a Queue object. When all messages that are expected have
been received, each thread sends a final update to the UI and the method
should exit, which should terminate the...
|
by: Miro |
last post by:
I will try my best to ask this question correctly. I think in the end the
code will make more sence of what I am trying to accomplish. I am just not
sure of what to search for on the net.
I have a form that has a button. ( this form is a child form of a parent
form ( main form ).
Anway...in this child form I have a button, and if clicked a bunch of code
will get executed.
I would like to show a Progress Bar / form in modal/ShowDialog...
|
by: Dann Corbit |
last post by:
Rather than create a new way of doing things:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2497.html
why not just pick up ACE into the existing standard:
http://www.cse.wustl.edu/~schmidt/ACE.html
the same way that the STL (and subsequently BOOST) have been subsumed?
Since it already runs on zillions of platforms, they have obviously worked
most of the kinks out of the generalized threading and processes idea (along
with many...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| | |