C ++ multi-thread programming

Source: Internet
Author: User

The multithreading of C ++ is different from that of C language. It is even harder to understand for me to switch from C ++ to C ++; I have been thinking about this during the time when I came to a new company. I have been checkingProgramTo summarize the recent experience in C ++ multi-threaded programming.

The multithreading of C ++ mainly involves two aspects: on the one hand, thread synchronization for global data. Let's look at the following example:

First, we encapsulate a Thread class.

Thread. h file

View code

# Ifndef thread_h
# Define Thread_h

# Include <pthread. h>

ClassThread
{
Public:
Thread ();

IntStart ();

IntStop ();

Virtual Void* Run ();

BoolJoin ();

ConstPthread_t & GETID ()Const{ReturnNtid ;}

Virtual~ Thread (){};

Private:
Thread (ConstThread &);

Static Void* Threadproc (Void*);

Pthread_t ntid;

};

Thread. cpp File

View code

# Include <pthread. h>
# Include " Thread. h "

Thread: thread ()
{
}

Void* Thread: Run ()
{

}

int thread: Start ()
{< br> return pthread_create (
& This -> ntid,
0 ,
threadproc,
static_cast void *> ( This )
);
}

IntThread: Stop ()
{
ReturnPthread_cancel (This-> GETID ());
}

BoolThread: Join ()
{
ReturnPthread_join (This-> GETID (),0);
}

Void* Thread: threadproc (Void* A_param)
{
Thread * pthread = static_cast <thread *> (a_param );
ReturnPthread-> Run ();

Then we create a new mythread class that inherits from the Thread class to implement differentAlgorithm.

Mythread. h

View code

# Ifndef mythread_h
# Define Mythread_h

# Include <String>
# Include"Thread. h"

ClassMythread:PublicThread
{
Public:
Mythread (Const IntN,Const Char* Aname): mcount (N), mname (aname ){};
Virtual Void* Run ();

IntSetcount (Const IntN );

IntSetname (Const Char* Aname );

Private:
IntMcount;
STD ::StringMname;
};

Mythread. cpp

View code

# Include < String >
# Include <stdio. h>
# Include <boost/thread. HPP>
# Include " Mythread. h "

void * mythread: Run ()
{< br> int sum = This -> mcount * 10 ;
for ( int I = 0 ; I {< br> printf ( " % 2D, the name is % s; \ t \ n ", I, This -> mname. c_str ();
}< BR >}

IntMythread: setcount (Const IntN)
{
This-> Mcount = N;
Return 0;
}

IntMythread: setname (Const Char* Aname)
{
STD ::StringAstring (aname );
This-> Mname = astring;
Return 0;

Main. cpp

View code

# Include <stdio. h>
# Include " Mythread. h "

IntMain ()
{

Mythread T1 (15,"Thread 1");
Mythread T2 (12,"Thread 2");

T1.start ();
T2.start ();

Sleep (1);

Return 0;

MAKEFILE file is not well written. Sorry.

View code

Multithread: Main. O mythread. O thread. o
G ++-O multithread main. O mythread. O thread. O-lpthread

Main. O: Main. cpp mythread. h
G ++-C main. cpp

Mythread. O: mythread. cpp mythread. h thread. h
G ++-C mythread. cpp

Thread. O: thread. cpp thread. h
G ++-C thread. cpp

Clean:

After making and running, check the running status. To save space, I only provided a few pieces of data. If you are interested, you can show the following:

View code

....
4 , The name Is Thread 1 ;
0 , The name Is Thread 2 ;
5 , The name Is Thread 1 ;
....
9 , The name Is Thread 1 ;
10 , The name Is Thread 1 ;
1 , The name Is Thread 2 ;
2 , The name Is Thread 2

Threads 1 and 2 are printed on the terminal. But what happens after we add mutex (equivalent to the mutex for Terminal Access?

Modify the mythread. cpp file as follows:

View code

# Include < String >
# Include <stdio. h>
# Include <boost/thread. HPP>
# Include " Mythread. h "

Pthread_mutex_t mutex = pthread_mutex_initializer; // Add mutex

Void * Mythread: Run ()
{
Pthread_mutex_lock (& mutex );// Lock
Int Sum = This -> Mcount * 10 ;
For ( Int I = 0 ; I <sum; I ++)
{
Printf ( " % 2D, the name is % s; \ t \ n " , I, This -> Mname. c_str ());
}
Pthread_mutex_unlock (& mutex ); // Unlock
}

IntMythread: setcount (Const IntN)
{
This-> Mcount = N;
Return 0;
}

IntMythread: setname (Const Char* Aname)
{
STD ::StringAstring (aname );
This-> Mname = astring;
Return 0;

We can run the command to check the printed information. There is no data conflict. Because the data volume is too large, it is not listed here.

 

The concept of C ++ encapsulation makes private data between different objects not staggered. This concept is hard to understand from C to C ++, especially when multithreading occurs, however, this does not indicate that private data does not need to be locked, because different methods in the class may be involved in simultaneously accessing or modifying data. See the following example.

Modify the main. cpp file as follows,

View code

# Include <stdio. h>
# Include " Mythread. h "

IntMain ()
{

Mythread T1 (150,"Thread 1");
//Mythread T2 (12, "thread 2 ");

T1.start ();
T1.setcount (12);
//T2.start ();

Sleep (1);

Return 0;

 

Slightly modify the run function in the mythread. cpp file.

View code

# Include < String >
# Include <stdio. h>
# Include <boost/thread. HPP>
# Include " Mythread. h "

void * mythread: Run ()
{< br> for ( int I = 0 ; I This -> mcount * 10000 ; I ++)
{< br> printf ( " % 2D, the name is % s; \ t \ n " , I, This -> mname. c_str ();
}< BR >}

IntMythread: setcount (Const IntN)
{
This-> Mcount = N;
Return 0;
}

IntMythread: setname (Const Char* Aname)
{
STD ::StringAstring (aname );
This-> Mname = astring;
Return 0;

 

Let's take a look at the running results, so we will not describe them in detail here;

To solve the above conflicts, we need to add locks to the class. To modify mythread. h mythread. cpp and Main. cpp functions

Mythread. h file

View code

# Ifndef mythread_h
# Define Mythread_h

# Include <String>
# Include"Thread. h"

ClassMythread:PublicThread
{
Public:
Mythread (Const IntN,Const Char* Aname): mcount (N), mname (aname)
{
Pthread_mutex_init (& mutex, null );
}
Virtual Void* Run ();

IntSetcount (Const IntN );

IntSetname (Const Char* Aname );

Private:
IntMcount;
STD ::StringMname;
Pthread_mutex_t mutex;
};

 

Mythread. cpp File

View code

# Include < String >
# Include <stdio. h>
# Include <boost/thread. HPP>
# Include " Mythread. h "

Void * Mythread: Run ()
{
Pthread_mutex_lock (& mutex );
// Int sum = This-> mcount * 10;
For ( Int I = 0 ; I <This -> Mcount * 10000 ; I ++)
{
Printf ( " % 2D, the name is % s; \ t \ n " , I, This -> Mname. c_str ());
}
Pthread_mutex_unlock (& mutex );
}

int mythread: setcount ( const int N)
{< br> pthread_mutex_lock (& mutex );
This -> mcount = N;
pthread_mutex_unlock (& mutex );
return 0 ;
}

IntMythread: setname (Const Char* Aname)
{
STD ::StringAstring (aname );
This-> Mname = astring;
Return 0;

 

Main. cpp File

View code

# Include <stdio. h>
# Include " Mythread. h "

IntMain ()
{

Mythread T1 (150,"Thread 1");

T1.start ();
Sleep (1);
T1.setcount (12);
T1.start ();

Return 0;

 

If you are interested, you can check the running effect. After adding the internal lock of the class, you can effectively synchronize data.

Welcome to the discussion.

 

Related Article

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.