Thread: Multithreading

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    4

    Multithreading

    I am making an application wherein on clicking a button, a thread is created if its not already created. If the thread is already created then the current instance of the thread is deleted and the new instance of the thread is created. Now my problem is that i am not able to check if the thread is already active and how to kill that thread from the main thread.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Which version of "threads" are you using for which OS and Compiler?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    something of my own...
    For windows there's the WaitForSingleObject function..
    for linux.. I asked that question like.. 4 days ago, and CodePlug told me to use the pthread_cleanup_push... which basicly I emulated...
    This code is incomplete.. only serves as example.
    First there's the header then the body
    Code:
    #ifdef WIN32
    	#include<windows.h>
    	
    	typedef DWORD THREADRETTYPE;
    	typedef DWORD pthread_t;
    
    	typedef struct _THREAD_ATTRS{
    		int n_refs;
    		pthread_t thread_id;
    		HANDLE hwthread;
    		DWORD  (WINAPI *func)(void*);
    	} THREAD_ATTRS;
    
    #else
    	#include <pthread.h>
    	#define WINAPI
    
    	typedef struct _THREAD_ATTRS{
    		int n_refs;
    		pthread_t thread_id;
    		void* (*func)(void*);
    		bool ended;
    	} THREAD_ATTRS;
    
    	typedef void* THREADRETTYPE;
    
    #endif
    
    typedef THREADRETTYPE  (WINAPI *THREADCALLBACKFUNC)(void*);
    class Thread{
    private:
    #ifndef WIN32	
    	friend void* pthread_entry_func(void*);
    #endif
    	THREAD_ATTRS *attrs;
    public:
    	Thread(THREADCALLBACKFUNC = NULL);
    	Thread(const Thread&);
    	~Thread();
    
    	Thread& operator=(const Thread&);
    
    	pthread_t id();
    	bool run(void *args);
    	bool isRunning();
    	bool hasExited();
    	bool waitExit(THREADRETTYPE* retval=NULL);
    	bool terminate();
    
    };
    and the cpp you need
    Code:
    #ifndef WIN32
    struct pthread_args{
    	Thread *thread;
    	void *args;
    	void *(*f)(void*);
    };
    
    void* pthread_entry_func(void* args){
    	pthread_args* p = (pthread_args*)args;
    	
    	Thread& t = *p->thread;
    	THREADCALLBACKFUNC f = p->f;
    	void *params = p->args;
    	
    	delete p;
    	
    	if(t.attrs) t.attrs->ended=false;
    	void *res=NULL;
    	try{
    		res = f(params);
    	}catch(...){
    		if(t.attrs)	t.attrs->ended=true;
    		throw;
    	}
    	if(t.attrs)	t.attrs->ended=true;	
    	return res;
    }
    
    #endif
    
    bool Thread::run(void *args){
    	assert(attrs != NULL);
    	if(isRunning() || attrs->func == NULL )
    		return false;
    #ifdef WIN32
    	attrs->hwthread = CreateThread(NULL,0,attrs->func,args,0,&attrs->thread_id);
    	return attrs->hwthread!=NULL;
    #else
    	pthread_args *p = new pthread_args;
    	p->thread=this;	
    	p->f=attrs->func;	
    	p->args=args;	
    	
    	return pthread_create(&attrs->thread_id,NULL,pthread_entry_func,p) == 0;
    #endif
    }
    
    bool Thread::hasExited(){
    	if(attrs == NULL) return true;
    #ifdef WIN32
    	bool res = WaitForSingleObject(attrs->hwthread,0) == WAIT_OBJECT_0;
    #else
    	bool res = attrs->ended;
    #endif
    	if(res)
    		clean_THREAD_ATTRS(attrs);
    	return res;
    }
    
    bool Thread::terminate(){
    #ifdef WIN32
    	bool ret = TerminateThread(attrs->hwthread,1)==TRUE;
    #else
    	bool ret = pthread_cancel(attrs->thread_id)==0;
    #endif
    	clean_THREAD_ATTRS(attrs);
    	return ret;
    }

  4. #4
    Registered User
    Join Date
    May 2005
    Posts
    4

    Multithreading

    Quote Originally Posted by Salem
    Which version of "threads" are you using for which OS and Compiler?
    I am using MFC threads in Windows XP and Visual Studio 6.0

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    I am using MFC threads in Windows XP and Visual Studio 6.0
    Worker threads or user interface threads? Anyway, you'll probably will use some state variable that will tell whether the thread has initialized or has exited. It'll have to be protected by using CSingleLock and a CCriticalSection. I don't know, hoiwever, if you actually need to be destroying your thread and then recreating it. You might just want to switch the direction of the processing going on in this thread.

  6. #6
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Visit the link in my sig (http://www.geocities.com/lucky760) and download my thread class. It's just a simple little thing, but allows you, among other things, to check if a child thread is running.

  7. #7
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    The proper way to check if a thread is running is to call one of the wait functions using the thread handle. If the thread handle is "signaled", then it is running.

    Here's a thread object you can use in your MFC apps. It's much betting than CWinThread.

    http://cboard.cprogramming.com/showthread.php?t=66229

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multithreading (flag stopping a thread, ring buffer) volatile
    By ShwangShwing in forum C Programming
    Replies: 3
    Last Post: 05-19-2009, 07:27 AM
  2. multithreading in C++
    By manzoor in forum C++ Programming
    Replies: 19
    Last Post: 11-28-2008, 12:20 PM
  3. Question on Multithreading
    By ronan_40060 in forum C Programming
    Replies: 1
    Last Post: 08-23-2006, 07:58 AM
  4. Client/Server and Multithreading
    By osal in forum Windows Programming
    Replies: 2
    Last Post: 07-17-2004, 03:53 AM
  5. Multithreading
    By JaWiB in forum Game Programming
    Replies: 7
    Last Post: 08-24-2003, 09:28 PM