Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <windows.h>
- void printSome (int i)
- {
- std::cout << i << std::endl;
- }
- void spawnThread (void (*threadName)(int i))
- {
- CreateThread
- (
- 0, // default security attributes
- 0, // use default stack size
- (LPTHREAD_START_ROUTINE)threadName, // thread function name
- (LPVOID)i, // argument to thread function
- 0, // use default creation flags
- 0 // returns the thread identifier
- );
- }
- int main ()
- {
- spawnThread(printSome(155));
- }
- class print_some : public basic_thread {
- private:
- int i;
- public:
- print_some (int i) : i(i) {};
- action_callback () {
- std::cout << i << std::endl;
- }
- }
- int main () {
- print_some printer (155);
- }
- class basic_thread :
- {
- public:
- basic_thread();
- protected:
- unsigned long m_ThreadId;
- virtual void action_callback () {};
- // Internal routine used to bridge between OS callback format and
- // action_callback. *Must* be static for the OS.
- static unsigned long __stdcall self_calling_callback (void *parameter);
- }
- unsigned long __stdcall basic_thread::self_calling_callback (void *parameter) {
- if (parameter) {
- basic_thread * thread = reinterpret_cast<basic_thread *>(parameter);
- thread->action_callback();
- }
- return 0; // The value returned only matters if someone starts calling GetExitCodeThread
- // to retrieve it.
- }
- basic_thread::basic_thread () {
- // Start thread.
- m_Handle = CreateThread(NULL,
- 0,
- self_calling_callback,
- (PVOID)this,
- 0,
- &m_ThreadId );
- if( !IsHandleValid() )
- throw StartException("CreateThread() failed", GetLastError());
- }
- void spawnThread(void (*threadEntryPoint)(int), int argument)
- {
- CreateThread(0,0,
- (LPTHREAD_START_ROUTINE)threadEntryPoint,
- (LPVOID)argument,
- 0,0);
- }
- spawnThread(printSome, 155);
- #include <iostream>
- #include <process.h>
- #include <windows.h>
- void
- printSome(int i)
- {
- std::cout << i << std::endl;
- }
- HANDLE
- spawnThread(void (*threadName)(int), int i)
- {
- return (HANDLE) _beginthread((void (*)(void*)) threadName, 0, (LPVOID) i);
- }
- int
- main(int argc, char *argv[])
- {
- HANDLE threadHandle;
- threadHandle = spawnThread(printSome, 155);
- WaitForSingleObject(threadHandle, INFINITE);
- return 0;
- }
- #include <iostream>
- #include <process.h>
- #include <windows.h>
- class Thread {
- static void proxy(void *arg) { (*(reinterpret_cast<Thread *> (arg)))(); }
- HANDLE thread_;
- public:
- virtual ~Thread() {}
- virtual void operator()() = 0;
- void start() { thread_ = (HANDLE) _beginthread(Thread::proxy, 0, this);}
- void waitForExit() { WaitForSingleObject(thread_, INFINITE); }
- };
- class Printer : public Thread {
- int i_;
- public:
- Printer(int i) : i_(i) {}
- void operator()() { std::cout << i_ << std::endl; }
- };
- int
- main(int argc, char *argv[])
- {
- Printer p(155);
- p.start();
- p.waitForExit();
- return 0;
- }
- spawnThread(printSome(155));
- //------------------------------------------------------------------------------------
- // 2.6 How to Pass a Function Pointer
- // <pt2Func> is a pointer to a function which returns an int and takes a float and two char
- void PassPtr(int (*pt2Func)(float, char, char))
- {
- int result = (*pt2Func)(12, 'a', 'b'); // call using function pointer
- cout << result << endl;
- }
- // execute example code - 'DoIt' is a suitable function like defined above in 2.1-4
- void Pass_A_Function_Pointer()
- {
- cout << endl << "Executing 'Pass_A_Function_Pointer'" << endl;
- PassPtr(&DoIt);
- }
Add Comment
Please, Sign In to add comment