學習了幾天多線程技術,做個總結,便於記憶。
一般 多線程傳遞參數 為 void* 所以會有一個強制轉換過程 (int*) (void *)等,傳遞多個參數選擇 結構體指針。為了避免多個線程訪問數據沖突 會有一個 叫做 “臨界區”CRITICALSECTION“ 類 ,防止讀寫數據沖突,
大概流程是:
CRITICAL_SECTION cs;
init CS(cs);
the one process
enter CS
.....
leaveCS
DELETE(cs);
在利用多線程時候,會遇到數據分割的問題 一般的規定是:
假設 data = N process_num = M;
N 能整除M 簡單 N/M
N 不能整除 M 則 M-1 個進程的數據處理數 為 N/(M-1) 最后一個進程處理數為N - (N/(M-1)*(M-1))
一般利用全局變量完成線程間的簡單通信 。當然也有timer定時器控制線程啟動 ,和event事件觸發線程。
WINDOWS下 多線程頭文件為 process.h
LINUX下 。。。 pthread.h 另外編譯時候 加上 -lpthread
WINDOWS 新建一個線程 有 CreateThread _beginthread (略過參數)
LINUX下 。。。 pthread_create
WINDOWS 凍結解凍線程為 SuspendThread() ResumeThread()
LINUX下 。。。一般用線程鎖 pthread_mutex_lock pthread_mutex_unlock
/*程序流程為:主線程創建子線程(當前子線程狀態為stop停止狀態),5秒后主線程喚醒子線程,10秒后主線程掛起子線程,15秒后主線程再次喚醒子線程,20秒后主線程執行完畢等待子線程退出。
代碼如下:*/
#include "stdio.h"
#include "unistd.h"
#include "pthread.h"
#include "string.h"
#include "time.h"
#define RUN 1
#define STOP 0
pthread_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 == 20)
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 == 10)
thread_pause();
if (i == 15)
thread_resume();
if (i == 20)
break;
}
if (0 == pthread_join(child_thread, NULL))
printf("child thread is over\n");
return 0;
}
。。。。。涉及到線程間的同步與異步一般會用到如下函數。。。。。。。。。。。。
windows 等待線程結束 WaitForSingleObject() WaitForMultipleObjects()
Linux 。。。 pthread_join()
windows 退出線程 ExitThread() TerminateThread()/強制結束線程
linux 退出線程 pthread_exit()
還有關鍵的 SIGNAL 沒有學習,再UPDATE吧。
最后附上linux 和windows 多線程測試代碼
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();
printf("process test %d\n",end-start);
return ((void *)0);
}
void* test1(void *P){
start = clock();
int i;
#pragma omp parallel for
for(i = 0;i<100000;i++)
usleep(1);
end = clock();
printf("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);
printf("Main process\n");
pthread_join(ntid,out);
pthread_join(ntid1,out);
return 0;
}
windows :
#include <windows.h>
#include <iostream>
#include <process.h>
#include <time.h>
#define _CRT_SECURE_NO_WARNINGS
using namespace std;
CRITICAL_SECTION cs;
int i = 0;
void run(void *){
char num[30];
while (1){
sprintf(num,"title %d",i++);
system(num);
Sleep(1000);
//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, true);
system("pause");
return 0;
}
參考文獻:
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
