Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!
Werbeanzeige
Zitat von »"$nooc"«
eine frage..
kann man typedef in einer klasse verwenden?
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
class blub { public: // ... DWORD ThreadProc(LPVOID); // ... typedef DWORD (blub::*ptrToThreadProc) (LPVOID); }; // ... blub::ptrToThreadProc hallo = &blub::ThreadProc; |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
class blub { public: // ... static DWORD WINAPI ThreadProc(LPVOID); // ... typedef DWORD (WINAPI *ptrToThreadProc) (LPVOID); }; // ... blub::ptrToThreadProc hallo = &blub::ThreadProc; |
Zitat von »"dot"«
achja: um dir kopfzerbrechen zu ersparen verrat ich dir an dieser stelle gleich, dass du eine nicht statische memberfunktion nicht an CreateThread() übergeben kannst (weil normale funktionspointer und pointer auf member nicht kompatibel sind, eben aufgrund dieser sache mit dem this pointer)...
C-/C++-Quelltext |
|
1 2 3 4 5 6 |
MSG msg; while(GetMessage (&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } |
Zitat von »"$nooc"«
also, ich hab das jetzt zusammengebracht.. hab der funktion CreateThread() einen gültigen funktionszeiger übergeben..
mein problem:
die übergebene funktion wird nur 1mal aufgerufen, und dann ist schluss..
also.. zumindest im debug modus kehr das programm kein zweites mal mehr zu dieser funktion zurück [...]
C-/C++-Quelltext |
|
1 2 3 4 |
while(bedingung) { // ... } |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
// klassenmember WinsockServer *pWinsockServer; // ------------------------------------------------------------------- // Name: frmServer_ServerThreadProc() // Desc: Thread procedure for the winsock server. // ------------------------------------------------------------------- DWORD WINAPI frmServer::frmServer_ServerThreadProc (LPVOID lpPAram) { int i = GetCurrentThreadId(); while(1) { pWinsockServer->process(); } return i; } // frmServer_ServerThreadProc() |
Zitat von »"$nooc"«
und ich hab gemerkt dass ich anscheinend keine funktion eines member-objektes darin aufrufen kann..
Zitat von »"$nooc"«
verrär mit jemand wie ich das bewerkstelligen kann?
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
// ------------------------------------------------------------------- // Name: Create() // Desc: Creates a new thread. // ------------------------------------------------------------------- template<typename T> int Thread::Create(LPSECURITY_ATTRIBUTES lpThreadAttributes, SIZE_T dwStackSize, T* instance, LPVOID lpParameter, DWORD dwCreationFlags) { // define function pointer DWORD (WINAPI *ThreadProc) (LPVOID) = &instance->frmServer_ThreadProc; if((m_hThreadHandle = CreateThread(lpThreadAttributes, dwStackSize, ThreadProc, lpParameter, dwCreationFlags, &m_dwThreadID)) == NULL) return THREAD_CREATING_ERROR; return THREAD_OK; } // Create() // functor: (einfach irgendwas) template<typename T> void functor(T object) { object->process(); } // aufruf Thread.Create(a, b, instanz, functor, e); |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
#include <windows.h> #include <tchar.h> #include <strsafe.h> #define MAX_THREADS 3 #define BUF_SIZE 255 typedef struct MyData { int val1; int val2; } MYDATA, *PMYDATA; DWORD WINAPI MyThread( LPVOID lpParam ) { HANDLE hStdout; PMYDATA pData; TCHAR msgBuf[BUF_SIZE]; size_t cchStringSize; DWORD dwChars; hStdout = GetStdHandle(STD_OUTPUT_HANDLE); if( hStdout == INVALID_HANDLE_VALUE ) return 1; // Cast the parameter to the correct data type. pData = (PMYDATA)lpParam; // Print the parameter values using thread-safe functions. StringCchPrintf(msgBuf, BUF_SIZE, TEXT("Parameters = %d, %d\n"), pData->val1, pData->val2); StringCchLength(msgBuf, BUF_SIZE, &cchStringSize); WriteConsole(hStdout, msgBuf, cchStringSize, &dwChars, NULL); return 0; } int _tmain() { PMYDATA pData; DWORD dwThreadId[MAX_THREADS]; HANDLE hThread[MAX_THREADS]; int i; // Create MAX_THREADS worker threads. for( i=0; i<MAX_THREADS; i++ ) { // Allocate memory for thread data. pData = (PMYDATA) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(MYDATA)); if( pData == NULL ) ExitProcess(2); // Generate unique data for each thread. pData->val1 = i; pData->val2 = i+100; hThread[i] = CreateThread( NULL, // default security attributes 0, // use default stack size MyThread, // thread function pData, // argument to thread function 0, // use default creation flags &dwThreadId[i]); // returns the thread identifier // Check the return value for success. // If failure, close existing thread handles, // free memory allocation, and exit. if (hThread[i] == NULL) { for(i=0; i<MAX_THREADS; i++) { if (hThread[i] != NULL) { CloseHandle(hThread[i]); } } HeapFree(GetProcessHeap(), 0, pData); ExitProcess(i); } } // Wait until all threads have terminated. WaitForMultipleObjects(MAX_THREADS, hThread, TRUE, INFINITE); // Close all thread handles and free memory allocation. for(i=0; i<MAX_THREADS; i++) { CloseHandle(hThread[i]); } HeapFree(GetProcessHeap(), 0, pData); return 0; } |
Werbeanzeige