Windows and Linux Multithreading

Source: Internet
Author: User

Learn a few days multithreading technology, do a summary, easy to remember.

General multi-threaded pass parameter is void* so there will be a casting process (int*) (void *), etc., Passing multiple parameters to select the struct pointer. To prevent multiple threads from accessing data conflicts there is a class called "critical section" criticalsection, which prevents read and write data conflicts,

The approximate process is:

critical_section cs;

Init CS (cs);

The one process

Enter CS

.....

Leavecs

DELETE (cs);

In the use of multithreading, you will encounter the problem of data segmentation general provisions Are:

Suppose data = N Process_num = M;

N can divide M simple n/m

n cannot be divisible by M the data processing number of M-1 processes is n/(M-1) the last process number is n-(n/(M-1) * (M-1))

generally, Global variables are used to complete simple communication between THREADS. Of course there are timer timers that control thread initiation, and event events trigger THREADS.

Multi-threaded header files under Windows Process.h

Linux under ... Pthread.h Additional compile time Plus-lpthread

WINDOWS new thread has createthread _beginthread (skip parameter)

Linux under ... Pthread_create

WINDOWS freeze Thaw thread for SuspendThread () resumethread ()

Linux under ... General thread Lock Pthread_mutex_lock Pthread_mutex_unlock

/* the process is: The main thread creates a child thread (the current child thread state is the stop stop state), 5 seconds after the main thread wakes the child thread, 10 seconds after the main thread hangs the driver thread, 15 seconds after the main thread wakes the child thread again, 20 seconds after the main thread execution waits for the child thread to Exit. The code is as Follows: */#include "stdio.h" #include "unistd.h" #include "pthread.h" #include "string.h" #include "time.h" #define RUN # Define STOP 0pthread_mutex_t Mut = pthread_mutex_initializer;pthread_cond_t cond = pthread_cond_initializer;int status =    Stop;void * thread_function (void) {static int i = 0;        While (1) {pthread_mutex_lock (&mut);        While (!status) {pthread_cond_wait (&cond, &mut);            } Pthread_mutex_unlock (&mut);        printf ("child pthread%d\n", i++);        If (i = =) break;    Sleep (1);        }}void thread_resume () {if (status = = STOP) {pthread_mutex_lock (&mut);        Status = RUN;        Pthread_cond_signal (&cond);        printf ("pthread run!\n");    Pthread_mutex_unlock (&mut);    } else {printf ("pthread run already\n"); }}void Thread_pause () {   if (status = = RUN) {pthread_mutex_lock (&mut);        Status = STOP;        printf ("thread stop!\n");    Pthread_mutex_unlock (&mut);    } else {printf ("pthread pause already\n");    }}int main () {int err;    static int i = 0;    pthread_t child_thread; #if 0 if (pthread_mutex_init (&mut, NULL)! = 0) printf ("mutex init error\n"); If (pthread_cond_init (&cond, NULL)! = 0) printf ("cond init error\n"); #endif err = pthread_create (&child_    thread, null, (void *) thread_function, null);    If (err! = 0) printf ("can ' t create thread:%s\n", strerror (err));        While (1) {printf ("father pthread%d\n", i++);        Sleep (1);        if (i = = 5) Thread_resume ();        if (i = = Ten) Thread_pause ();        If (i = =) Thread_resume ();    If (i = =) break;    } if (0 = = Pthread_join (child_thread, NULL)) printf ("child thread is over\n"); Return 0;} 

。。。。。 The following functions are commonly used when it comes to synchronization and asynchrony between threads ....

Windows waits for thread end WaitForSingleObject () waitformultipleobjects ()

Linux... Pthread_join ()

Windows exit thread ExitThread () terminatethread ()/force End Thread

Linux exit thread Pthread_exit ()

There are key SIGNAL not to learn, and then update it.

Finally attach Linux and Windows multithreaded test code

LINUX:GCC Test.c-fopenmp-lpthread-o Test

#include "stdio.h" #include "omp.h" #include "time.h" #include "unistd.h" #include "pthread.h" clock_t start,end;void* Test (void *p) {start = clock (), int i;for (i=0;i<100000;i++) usleep (1), end = Clock ();p rintf ("process test    %d\n", end-start); return ((void *) 0);} void* test1 (void *p) {start = clock (); int i; #pragma omp parallel forfor (i = 0;i<100000;i++) usleep (1); end = Clock ();p rint F ("process test1   %d\n", end-start); return ((void *) 0);} int main () {int err;pthread_t ntid;pthread_t ntid1;void** out;err = pthread_create (&ntid,0,test,0); if (err!=0) Putchar (' n '), err =pthread_create (&ntid1,0,test1,0), if (err!=0) putchar (' n ');//test (0);//test1 (0);p rintf ("Main process\n ");p thread_join (ntid,out);p thread_join (ntid1,out); return 0;}

  

Windows:

#include <windows.h> #include <iostream> #include <process.h> #include <time.h> #define _crt_ Secure_no_warningsusing namespace std; critical_section Cs;int i = 0;void run (void *) {char num[30];while (1) {sprintf (num, "title%d", i++); system (num); Sleep (//messagebox) (0, (lpctstr) num, (lpctstr) num, 0);}} int main () {int hd[4]; MessageBoxA (0, "1", "1", 0);//for (int i = 0; i < 4; i++) {hd[i] = _beginthread (run, 0, 0);//}waitforsingleobject (hd, TR ue); System ("pause"); return 0;}

  

Reference Documents:

Http://www.cnblogs.com/gnuhpc/archive/2012/12/07/2807484.html

http://blog.csdn.net/zhouruifu2015/article/details/47833985

Http://blog.chinaunix.net/uid-29145190-id-4341878.html

Http://edu.51cto.com/lesson/id-86087.html

Windows and Linux multithreading

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.