मैं एक वर्ग बनाना चाहता हूं जिनकी विधियों को एकाधिक धागे से बुलाया जा सकता है। लेकिन जिस धागे से इसे बुलाया गया था, उस विधि को निष्पादित करने के बजाय, इसे सभी को अपने स्वयं के धागे में करना चाहिए। किसी भी परिणाम को वापस करने की आवश्यकता नहीं है और इसे कॉलिंग थ्रेड को अवरुद्ध नहीं करना चाहिए।इवेंट/टास्क कतार मल्टीथ्रेडिंग सी ++
एक पहला प्रयास कार्यान्वयन मैंने नीचे शामिल किया है। सार्वजनिक विधियां एक कार्य सूचक और डेटा को नौकरी में डालती हैं कतार, जिसे कार्यकर्ता धागा तब उठाता है। हालांकि यह विशेष रूप से अच्छा कोड नहीं है और नई विधियां जोड़ना बोझिल है।
आदर्श रूप से मैं इसे बेस क्लास के रूप में उपयोग करना चाहता हूं, जिसे मैं न्यूनतम जल्दी और कोड डुप्लिकेशंस के साथ विधियों को जोड़ सकता हूं (तर्कों की एक चर संख्या के साथ)।
ऐसा करने का बेहतर तरीका क्या है? क्या कोई मौजूदा कोड उपलब्ध है जो कुछ समान करता है? धन्यवाद
#include <queue>
using namespace std;
class GThreadObject
{
class event
{
public:
void (GThreadObject::*funcPtr)(void *);
void * data;
};
public:
void functionOne(char * argOne, int argTwo);
private:
void workerThread();
queue<GThreadObject::event*> jobQueue;
void functionOneProxy(void * buffer);
void functionOneInternal(char * argOne, int argTwo);
};
#include <iostream>
#include "GThreadObject.h"
using namespace std;
/* On a continuous loop, reading tasks from queue
* When a new event is received it executes the attached function pointer
* It should block on a condition, but Thread code removed to decrease clutter
*/
void GThreadObject::workerThread()
{
//New Event added, process it
GThreadObject::event * receivedEvent = jobQueue.front();
//Execute the function pointer with the attached data
(*this.*receivedEvent->funcPtr)(receivedEvent->data);
}
/*
* This is the public interface, Can be called from child threads
* Instead of executing the event directly it adds it to a job queue
* Then the workerThread picks it up and executes all tasks on the same thread
*/
void GThreadObject::functionOne(char * argOne, int argTwo)
{
//Malloc an object the size of the function arguments
int argumentSize = sizeof(char*)+sizeof(int);
void * myData = malloc(argumentSize);
//Copy the data passed to this function into the buffer
memcpy(myData, &argOne, argumentSize);
//Create the event and push it on to the queue
GThreadObject::event * myEvent = new event;
myEvent->data = myData;
myEvent->funcPtr = >hreadObject::functionOneProxy;
jobQueue.push(myEvent);
//This would be send a thread condition signal, replaced with a simple call here
this->workerThread();
}
/*
* This handles the actual event
*/
void GThreadObject::functionOneInternal(char * argOne, int argTwo)
{
cout << "We've made it to functionTwo char*:" << argOne << " int:" << argTwo << endl;
//Now do the work
}
/*
* This is the function I would like to remove if possible
* Split the void * buffer into arguments for the internal Function
*/
void GThreadObject::functionOneProxy(void * buffer)
{
char * cBuff = (char*)buffer;
functionOneInternal((char*)*((unsigned int*)cBuff), (int)*(cBuff+sizeof(char*)));
};
int main()
{
GThreadObject myObj;
myObj.functionOne("My Message", 23);
return 0;
}
मैं बूस्ट :: वायदा की तलाश में था, लेकिन चूंकि यह रिलीज़ किए गए बूस्ट संस्करण का हिस्सा नहीं है, इसलिए मुझे अपने विश्वसनीय एसीई में वापस आना पड़ा :-) – lothar
वायदा लाइब्रेरी बूस्ट 1.41 का हिस्सा होगा। यह http://www.stdthread.co.uk –
पर धन्यवाद, एंथनी पर मेरे सी ++ 0x थ्रेड लाइब्रेरी कार्यान्वयन के हिस्से के रूप में भी उपलब्ध है। आपसे सुनकर अच्छा लगा :) –