Multithreaded Programming Note __ Programming

Source: Internet
Author: User

1. Can not be directly in the thread thread, should be set a flag, the threads run when checking this tag

void Cthreadsoap::threadrun ()
{
Resulttype RC (rc_success);
Whether a thread runs a thread
BOOL Is_run (FALSE);
The action that the thread will perform
Long thread_action (0);
Parameters to perform the action
void *lpactionpara (NULL);
Form handle
HWND hwnd (NULL);

Gsaop Object
struct SOAP gSOAP;

Gets whether the thread is running
rc = This->checkstatus (Is_run,thread_action,&hwnd,&lpactionpara);
if (rc_success! = RC)
{
This->setthreaderror (RC);
Return
}
:: Soap_init (&GSOAP);
while (Is_run)
{
Check whether an action is performed
if (0! = thread_action)
{
This->callsoap (Thread_action, Gsoap,hwnd,lpactionpara);
This->resetthreadaction ();
This->safepostmessage (hwnd,wm_saop_done,thread_action);
}
Else
{
:: Sleep (1);
}

Freeing resources
HWnd = NULL;
Cthreadbase::safefreeobject (Lpactionpara);
Gets whether the thread continues to run
rc = This->checkstatus (Is_run,thread_action,&hwnd,&lpactionpara);
if (rc_success! = RC)
{
This->setthreaderror (RC);
Break
}
}
Freeing resources
Cthreadbase::safefreeobject (Lpactionpara);
:: Soap_done (&GSOAP);
}
Is_run is a local variable in a threading method that checkstatus read a class member variable (m_threadrun) every time a loop is passed through the member method of the class to determine whether the thread continues to run. When you want to stop or exit a thread, you only need to m_ the class member variable.   ThreadRun is set to false. Methods for checking thread state
/Gets the parameters of the thread execution action
Resulttype Cthreadbase::checkstatus (bool &is_run,long &thread_action,hwnd *lphwnd,void **lppVal)
{
if (NULL = = This->m_pthread_mutex | | NULL = = This->m_pthread_cond)
return err_initialize_fail;


BOOL Thread_is_run (FALSE); Whether the thread is running
Long thread_cur_action (0); Action being performed by the current thread
void *lpdata (NULL); Data when a thread executes an action
HWND hwnd (NULL); Form handle

Lock the data to be read
::p Thread_mutex_lock (&this->m_pthread_mutex);


if (0! = this->m_actioninfo.action)
{
Check the validity of shared objects
if (this->m_actioninfo.data.dwmemsize > 0 && NULL! = this->m_actioninfo.data.lpdata)
{
Allocating memory
lpdata = malloc (this->m_actioninfo.data.dwmemsize);
if (NULL = = lpdata)
{
::p thread_cond_signal (&this->m_pthread_cond);
::p Thread_mutex_unlock (&this->m_pthread_mutex);
return err_malloc_fail;
}
memset (lpdata,0x0, this->m_actioninfo.data.dwmemsize);
memcpy (Lpdata,this->m_actioninfo.data.lpdata, this->m_actioninfo.data.dwmemsize);
Notifies the main thread to release resources when an action parameter is set, when the currently-copied object completes
::p thread_cond_signal (&this->m_pthread_cond);
}
}
HWnd = this->m_actioninfo.hwnd;
Thread_cur_action = this->m_actioninfo.action;
Thread_is_run = this->m_threadrun;
::p Thread_mutex_unlock (&this->m_pthread_mutex);

*lphwnd = hWnd;
Is_run = Thread_is_run;
Thread_action = thread_cur_action;
*lppval = lpdata;
return rc_success;
}
Stopping a thread
Resulttype Cthreadbase::stop ()
{
if ( NULL = = This->m_pthread_mutex)
return err_initialize_fail;
::p Thread_mutex_lock (&this->m_pthread_mutex);
This->m_threadrun = false;
::p Thread_mutex_unlock (&this->m_pthread_mutex);
Wait for thread to finish running
::p Thread_join (This->m_pthread,null);
return rc_success;
}


2. Multi-threaded objects should be paired, such as pthread_mutex_t and pthread_rwlock_t objects, the same object cannot be locked with a mutex at the time of writing, locked with a read-write lock during read, and should be paired with read-write pthread_mutex_ The T object or pthread_rwlock_t object, such as the Stop method in the previous example, and the checkstatus pair use Pthread_mutex_lock

3. Memory release should follow the principle of who created the release. For example, threads A and two objects, memory allocated on thread A should be released on thread A, do not release threads A to allocate memory on line B


Resulttype cthreadsoap::callmapbasicinfo (const HWND &hwnd,const char *lpcszmapservice)
{
if (NULL = = This->m_pthread_mutex | | NULL = = This->m_pthread_cond)
return err_initialize_fail;
if (NULL = = Lpcszmapservice)
return err_invalid_arguments;

BOOL Thread_is_run;
Long thread_cur_action;
size_t Dwmemsize ((strlen (Lpcszmapservice) + 1) * sizeof (char));
Check the status of a thread
This->checkstatus (thread_is_run,thread_cur_action);
if (false = = Thread_is_run)
return errpt_thread_notrun;
if (0! = thread_cur_action)
return errpt_thread_action;

::p Thread_mutex_lock (&this->m_pthread_mutex);
Note the action action is automatically restored after the call is completed
This->m_actioninfo.hwnd = hWnd;
This->m_actioninfo.action = Pt_action_webservice_mapbasicinfo;
This->m_actioninfo.data.dwmemsize = dwmemsize;
This->m_actioninfo.data.lpdata = malloc (dwmemsize);
if (NULL = = This->m_actioninfo.data.lpdata)
{
this->m_actioninfo.action = 0;
this->m_actioninfo.data.dwmemsize = 0;
::p Thread_mutex_unlock (&this->m_pthread_mutex);
return err_malloc_fail;
}
memset (this->m_actioninfo.data.lpdata,0x0, dwmemsize);
strcpy_s ((char *) this->m_actioninfo.data.lpdata, dwmemsize,lpcszmapservice);
To release resources after waiting for the thread to get the parameters completed
::p thread_cond_wait (&this->m_pthread_cond, &this->m_pthread_mutex);
Freeing resources
The thread state is persisted until the SOAP method call is finished restoring resetthreadaction
This->m_actioninfo.hwnd = NULL;
this->m_actioninfo.action = 0;
this->m_actioninfo.data.dwmemsize = 0;
Free (this->m_actioninfo.data.lpdata);
This->m_actioninfo.data.lpdata = NULL;

::p Thread_mutex_unlock (&this->m_pthread_mutex);
return rc_success;
}

Calling the Callmapbasicinfo method in the main thread invokes the specified soap method (a URL parameter) and saves the incoming URL parameter to the thread data share (THIS->M_ACTIONINFO) in the main thread. It then waits for the thread to read the data in the share (CheckStatus method), and then releases the data in the shared area (because it was created in the main thread) in the main thread 4. Data transfer between threads should use shared memory. Locks memory when reading or writing to shared memory (as in the preceding example). When sending a message, the message parameter can only use the base data type (Long,int,bool, etc.), and the complex data type is taken from the shared memory after the message is notified, ensuring that the memory is freed correctly. Because PostMessage messages are sent to the queue, in extreme cases, If the PostMessage parameter is a pointer, it may cause the memory to be freed correctly.

5. Thread-running methods cannot use the member variables of a class directly, and if you need to read and write class member variables, copy the class member variable to a local variable in the method or copy the local variable to a class member variable through a mutex or read-write lock object. Note that replication is used, albeit slightly less efficient, to ensure thread safety.

6.pThread takes precedence over read-write lock Objects (pthread_rwlock_t), such as the successful return of Callmapbasicinfo, with a write lock (Pthread_rwlock_wrlock) to save the return value to the shared area, obtained by reading the lock ( Pthread_rwlock_rdlock) to copy the information from the shared area.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.