thread with callback procedure
| waldo (3) | |||
| hi, I want write a thread and I want take over a procedure which should be called out of the thread, but I dont know how :( Maybe, anybody can give me a hint ? What I have made: 1. Building of a class (not all is included) class CSerial { public: CSerial (char *Device, bool Debug=false); ~CSerial(); void setPort (char *Device) {g_device = Device;}; bool openPort (char *Device = 0/*NULL*/, speed_t Baud = B19200); void receive (int user_callback_proc(unsigned char*ReceivedData, int Size)); <-- this is the callback procedure of the class }; 2. Implementation of receive(...): void CSerial::receive (int user_callback_proc(unsigned char*ReceivedData, int Size)) { printf ("receive in CSerial start\n"); pthread_t receive_thread; int iret; ReceiveThreadProc ((void*)user_callback_proc); <--only a test if this callback in general ;) iret = pthread_create( &receive_thread, 0/*NULL*/, /*CSerial::*/ReceiveThreadProc, (void*) user_callback_proc); <-- here I take over the address of the callback procedure pthread_join( receive_thread, NULL); printf("ReceivedData returns: %d\n",iret); } 3. Implementation of the thread-procedure: void *ReceiveThreadProc( void *ptr ) { int i=1; printf("ReceivedData thread : \n"); while (i < 100) { printf("%d ",i++); //sleep (1000); } (void*)ptr ((char*)"hallo \n", 4);<--compile says "cannot use ptr as function", but how can I implement it ? printf("\n"); thanks for the attention regards | |||
| Zaita (1450) | |||
| http://www.boost.org/doc/libs/1_35_0/doc/html/thread.html You can download the boost library which comes with a bunch of thread examples :) Edit: If you could also post your complete code (so it can be attempted to be compiled) in proper [c0de] your code here [/c0de] (replace 0 with o) tags. That'd help too.
void *ReceiveThreadProc( void *ptr ). This is not a function pointer that is being passed into the function.Check out: http://www.newty.de/fpt/fpt.html | |||
| waldo (3) | |||
| Thanks for the links, I will try it, I know, that it was not a pointer to a function but I didnt know to explain it (the problem ;) regards | |||
| waldo (3) | |||
| I will give a short info: declare a 'variable' to store a (callback-) procedure: int (*g_callbackProc)(unsigned char*, int); g_callbackProc=variable (unsigned char*, int)=parameters of the (callback-)procedure call of hte procedure: g_callbackProc (Bytes, Length); big thanks to Zaita, the link was very helpfull ;) | |||
This topic is archived - New replies not allowed.
