Results 1 to 10 of 10

Thread: Multithreading

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    England, Buckingham
    Posts
    1,341

    Multithreading

    i need some code that allows me to use the CreateThread API function to spin off a new thread and run a function. i also need to know how i should properly declasre the function if it has to be different from normal. i also need to know how to close the thread and delete it.

  2. #2
    Zaei
    Guest
    I hear that you should use beginthread instead of CreateThread(). There should be info in the MSDN. I will try to give an example later, if you still need it.

    Z.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    England, Buckingham
    Posts
    1,341
    cheers zaei, will have a look their in a tick. i have been working on server stuff recently as you may have noticed

    i probably will need a demo coz im that constipated.

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    From my current code:
    Code:
    	class thread {
    	public:
    		thread(void (__cdecl *func)(void*) = thread::run, void *param = 0);
    
    		static void sleep(uint32 milliseconds);
    
    	private:
    		static void run(void *param);
    
    		uint32 m_thread;
    	};
    Code:
    	thread::thread(void (__cdecl *func)(void*), void *param) {
    		m_thread = _beginthread(func, 0, param);
    		if(!m_thread) {
    			throw std::runtime_error("Could not create thread");
    		}
    	}
    
    	void thread::run(void *param) {
    		_endthread(); // exit
    	}
    
    	void thread::sleep(uint32 milliseconds) {
    		return Sleep(milliseconds);
    	}
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    England, Buckingham
    Posts
    1,341
    lol, i got this going now but i used a CreateThread version which works.

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    If you use anything from the Standard Library (whether C or C++) you need to use _beginthread (or _beginthreadex) otherwise you'll get a memory leak.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  7. #7
    Zaei
    Guest
    Yupper =).

    Z.

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    England, Buckingham
    Posts
    1,341
    ok i changed it to _beginthread and to work like you have done in your code, the only problem is that it cannot find the functions.

    i added process.h to includes but still no luck, i looked in the h and saw it had #if defined _MT so i then added #define _MT before the include and it says it cannot link to _beginthread whereas endthread works fine.

  9. #9
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Take out your #define _MT, and in Project Settings you need to change it from Single threaded to Multithreaded (under C++ I think).

    You'll need to change it for both Debug and Release.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  10. #10

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    England, Buckingham
    Posts
    1,341
    lol, i changed the threading code to what you said, then i changed the project settings and now the server dont work properly lol

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width