कक्षा सदस्यों के कार्यों के धागे बनाने के लिए आप CreateThread()
का उपयोग कैसे करते हैं?आप क्लास सदस्यों के कार्यों के लिए CreateThread का उपयोग कैसे करते हैं?
36
A
उत्तर
62
आपको वास्तविक थ्रेड स्टार्ट फ़ंक्शन के रूप में उपयोग करने के लिए एक स्थिर विधि बनाने की आवश्यकता है, और उदाहरण के लिए पॉइंटर को lpParameter
CreateThread
पर तर्क दें। यह स्थैतिक विधि को पारित कर दिया जाएगा, जो इसे ऑब्जेक्ट पॉइंटर पर डाला जा सकता है और सदस्य फ़ंक्शन से कॉल कर सकता है।
class MyClass
{
static DWORD WINAPI StaticThreadStart(void* Param)
{
MyClass* This = (MyClass*) Param;
return This->ThreadStart();
}
DWORD ThreadStart(void)
{
// Do stuff
}
void startMyThread()
{
DWORD ThreadID;
CreateThread(NULL, 0, StaticThreadStart, (void*) this, 0, &ThreadID);
}
};
9
जो लोग आते हैं, वे मेरे समाधान का उपयोग करना चाहेंगे। यह VS2008 के साथ संकलित एक पूर्ण स्रोत फ़ाइल है। उपयोग करने के तरीके पर एक संक्षिप्त निर्देश उदाहरण के लिए नीचे देखें!
// Thread.h
#ifndef __THREAD_H__
#define __THREAD_H__
// #############################################################################
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
// =============================================================================
template<class T>
class Thread
{
// new type Method: pointer to a object's method (this call)
typedef DWORD (T::* Method)(void);
// -----------------------------------------------------------------------------
protected:
HANDLE hThread; // unique handle to the thread
private:
DWORD threadID; // thread id - 0 until started
T* object; // the object which owns the method
Method method; // the method of the object
HANDLE hInterrupt; // mutex to signal an interrupt via ReleaseSemaphore()
HANDLE hSingleStart; // only one thread allowed to call start() mutex
// -----------------------------------------------------------------------------
private:
// This function gets executed by a concurrent thread.
static DWORD run(LPVOID thread_obj)
{
Thread<T>* thread = (Thread<T>*)thread_obj;
return (thread->object->*thread->method)();
}
// Prevent copying of threads: No sensible implementation!
Thread(const Thread<T>& other) {}
// Prevent assignment of threads: No sensible implementation!
Thread<T>& operator =(const Thread<T>& other) {}
// -----------------------------------------------------------------------------
public:
/* Creates a new Thread object. object: the one which method should be
executed. method: pointer to the object's method. */
explicit Thread(T* object, DWORD (T::* method)(void))
{
this->hThread = NULL;
this->object = object;
this->method = method;
this->threadID = 0;
this->hInterrupt = CreateSemaphore(NULL, 1, 1, NULL);
this->hSingleStart = CreateMutex(NULL, FALSE, NULL);
// this->hInterrupt = CreateMutex(NULL, FALSE, NULL);
}
// -----------------------------------------------------------------------------
~Thread(void)
{
if (hInterrupt)
CloseHandle(hInterrupt);
if (hThread)
CloseHandle(hThread);
}
// -----------------------------------------------------------------------------
/* Starts executing the objects method in a concurrent thread. True if the
thread was started successfully; otherwise false. */
bool start()
{
__try {
if (WaitForSingleObject(hSingleStart, 0) != WAIT_OBJECT_0)
return false;
if (hThread) // Thread had been started sometime in the past
{
if (WaitForSingleObject(hThread, 0) == WAIT_TIMEOUT)
{ // if thread's still running deny new start
return false;
}
CloseHandle(hThread);
}
// (Re-)Set not interrupted semaphore state
WaitForSingleObject(hInterrupt, 0);
hThread = CreateThread(
NULL,
0,
(LPTHREAD_START_ROUTINE) Thread<T>::run,
this,
0,
&this->threadID
);
if (hThread)
return true;
return false;
}
__finally
{
ReleaseMutex(hSingleStart);
}
}
// -----------------------------------------------------------------------------
// Blocks the calling thread until this thread has stopped.
inline void join()
{
WaitForSingleObject(hThread, INFINITE);
}
// -----------------------------------------------------------------------------
/* Asks the thread to exit nicely. Thread function must implement checks.
return value indicates if the interrupt could be placed not if the thread
reacts on the interrupt. true indicates success, false an error. */
inline bool interrupt()
{
if (hInterrupt)
{
return ((ReleaseSemaphore(hInterrupt, 1, NULL) == FALSE) ?
false : true);
}
return false;
}
// -----------------------------------------------------------------------------
/* True if an interrupt request was set, otherwise false. */
inline bool isInterrupted()
{
return this->isInterrupted(0);
}
// -----------------------------------------------------------------------------
/* True if an interrupt request was set, otherwise false. Waits for millisec
milliseconds for the interrupt to take place. */
inline bool isInterrupted(DWORD millisec)
{
if (WaitForSingleObject(hInterrupt, millisec) == WAIT_TIMEOUT)
{
return false;
}
ReleaseSemaphore(hInterrupt, 1, NULL); // keep interrupted state
return true;
}
// -----------------------------------------------------------------------------
inline bool isRunning()
{
DWORD exitCode = 0;
if (hThread)
GetExitCodeThread(hThread, &exitCode);
if (exitCode == STILL_ACTIVE)
return true;
return false;
}
// -----------------------------------------------------------------------------
// Getter & Setter
// -----------------------------------------------------------------------------
__declspec(property(get = getThreadHandle)) HANDLE ThreadHandle;
inline HANDLE getThreadHandle()
{
return hThread;
}
// -----------------------------------------------------------------------------
__declspec(property(get = getThreadID)) DWORD ThreadID;
inline DWORD getThreadID()
{
return threadID;
}
// -----------------------------------------------------------------------------
};
// #############################################################################
#endif // __THREAD_H__
लघु उदाहरण:
// main.cpp
#include <iostream>
#include <string>
#include "Thread.h"
class HelloWorld
{
public:
DWORD print()
{
std::cout << "Hello World!" << std::endl;
return 0;
}
};
int main(void)
{
// Random object with DWORD method (void)
HelloWorld world;
// thread should call print method of world.
Thread<HelloWorld> thread(&world, &HelloWorld::print);
if (thread.start())
std::cout << "Thread start()" << std::endl;
thread.join(); // wait for thread
return 0;
}
संबंधित मुद्दे
- 1. आप PowerShell का उपयोग कैसे करते हैं?
- 2. आप स्क्वाक का उपयोग कैसे करते हैं?
- 3. आप CTEST_CUSTOM_PRE_TEST का उपयोग कैसे करते हैं?
- 4. आप आईएमई का उपयोग कैसे करते हैं?
- 5. आप जेएसटीएल का उपयोग कैसे करते हैं?
- 6. आप i18n का उपयोग Node.js के साथ कैसे करते हैं?
- 7. आप gnuplot के अंतर्निहित फोंट का उपयोग कैसे करते हैं?
- 8. आप SQLQ के साथ LINQ का उपयोग कैसे करते हैं
- 9. जटिल निर्माण प्रक्रिया के लिए आप क्या उपयोग करते हैं?
- 10. अभिव्यक्ति के लिए लॉग इन करने के लिए आप scalaz.WriterT का उपयोग कैसे करते हैं?
- 11. आप जावा में रूबी का उपयोग कैसे करते हैं?
- 12. क्या आप एनडपेन्स का उपयोग करते हैं?
- 13. क्या आप फ़िंग का उपयोग करते हैं?
- 14. सी में, आप संरचना के सदस्यों को अस्थिर के रूप में कैसे घोषित करते हैं?
- 15. आप एक पायथन कक्षा को विरासत का उपयोग किए बिना एक और नाम रखने के लिए कैसे उपनाम करते हैं?
- 16. पायथन क्लास के सदस्यों की शुरुआत
- 17. PHP: आप इसे लिखने के लिए क्या उपयोग करते हैं?
- 18. आप केट का उपयोग कैसे करते हैं? टिप्स/ट्रिक्स/वर्कफ़्लो
- 19. जब आप अनुवादित पाठ में लिंक करते हैं तो आप गेटटेक्स्ट का उपयोग कैसे करते हैं?
- 20. फ्रेम के भीतर जावास्क्रिप्ट निष्पादित करने के लिए आप सेलेनियम का उपयोग कैसे करते हैं?
- 21. सशर्त संकलन करने के लिए आप प्रकार के लक्षणों का उपयोग कैसे करते हैं?
- 22. क्या आप दावे का उपयोग करते हैं?
- 23. कोडिंग कार्यों के लिए आप बहुत तेज़ (और गंदे) अनुमान कैसे करते हैं?
- 24. फ्लोचार्ट बनाने के लिए आप क्या उपयोग करते हैं?
- 25. आप सिनात्रा के लिए क्या उपयोग करते हैं?
- 26. आप एंड्रॉइड टर्मिनल एमुलेटर के लिए क्या उपयोग करते हैं?
- 27. नेस्टेड क्लास के निजी सदस्यों तक पहुंच कैसे प्राप्त करें?
- 28. सी #: आप कक्षा 'बेस क्लास कैसे प्राप्त करते हैं?
- 29. Grails में आप डोमेन क्लास को कैसे संशोधित करते हैं?
- 30. क्या आप अतिरिक्त स्कोपिंग के लिए घुंघराले ब्रेसिज़ का उपयोग करते हैं?