当前位置: 代码迷 >> 多核软件开发 >> 线程同步有关问题
  详细解决方案

线程同步有关问题

热度:8958   发布时间:2013-02-26 00:00:00.0
线程同步问题
C/C++ code
#include <Windows.h>#include <string.h>#include <stdio.h>#define BUFSIZE 80HANDLE g_mutex;DWORD WINAPI ShowBiosInfo(LPVOID n){            char szProductType[BUFSIZE];            HKEY hKey=NULL;            DWORD dwType=REG_MULTI_SZ;             DWORD dwBufLen=BUFSIZE;            long lRet;            int i=0;            WaitForSingleObject(g_mutex,INFINITE);             lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE,TEXT("HARDWARE\\DESCRIPTION\\System"),0,KEY_QUERY_VALUE,&hKey );            lRet = RegQueryValueEx( hKey, TEXT("SystemBiosVersion"), NULL, &dwType,(LPBYTE) szProductType, &dwBufLen);                        if (szProductType[0]!=0)            {                for (i;i<dwBufLen;i++)                {                    if (szProductType[i]==0)                    {                        szProductType[i]=0x20;                    }                                    }                szProductType[i]=0;                printf("9.SystemBiosVersion :%s\n",szProductType);            }             szProductType[0]=0,dwBufLen=BUFSIZE;            RegQueryValueEx( hKey, TEXT("SystemBiosDate"), NULL, &dwType,(LPBYTE) szProductType, &dwBufLen);            printf("10 .SystemBiosDate :%s\n",szProductType);                                    szProductType[0]=0,dwBufLen=BUFSIZE;            lRet=RegQueryValueEx(hKey, TEXT("Identifier"), NULL, NULL,(LPBYTE) szProductType, &dwBufLen);            printf("11 .Identifier :%s\n",szProductType);                                    szProductType[0]=0,dwBufLen=BUFSIZE;            lRet=RegQueryValueEx(hKey, TEXT("VideoBiosVersion"), NULL, NULL,(LPBYTE) szProductType, &dwBufLen);            printf("12 .VideoBiosVersion :%s\n",szProductType);            ReleaseMutex(g_mutex);            return 0;}DWORD WINAPI ShowWindowInfo(LPVOID n){                DWORD dwVersion;    SYSTEM_INFO system_info;    WaitForSingleObject(g_mutex,INFINITE);         printf("\n");        GetSystemInfo(&system_info);    dwVersion=GetVersion();    if (dwVersion < 0x80000000)    {             printf("Windows NT/2000/Xp\n");    }    else    {         printf("Windows Me/98/95");    }        printf(" 1. Hardware information: \n");      printf(" 2. OEM ID: %u\n", system_info.dwOemId);    printf(" 3. Number of processors: %u\n",         system_info.dwNumberOfProcessors);      printf(" 4. Page size: %u\n", system_info.dwPageSize);      printf(" 5. Processor type: %u\n", system_info.dwProcessorType);      printf(" 6. Minimum application address: %lx\n",         system_info.lpMinimumApplicationAddress);      printf(" 7. Maximum application address: %lx\n",         system_info.lpMaximumApplicationAddress);       printf(" 8. Active processor mask: %u\n",         system_info.dwActiveProcessorMask);     ReleaseMutex(g_mutex);    return 0;}int main(){    HANDLE hShowWindowInfoThread;    HANDLE hShowBiosInfo;    DWORD threadID;    DWORD exitCode;    g_mutex=CreateMutex(NULL,FALSE,NULL);    hShowWindowInfoThread=CreateThread(NULL,0,ShowWindowInfo,NULL,0,&threadID);    hShowBiosInfo=CreateThread(NULL,0,ShowBiosInfo,NULL,0,&threadID);    WaitForSingleObject(hShowBiosInfo,INFINITE);    CloseHandle(g_mutex);    CloseHandle(hShowBiosInfo);    CloseHandle(hShowWindowInfoThread);}



我按了无数次编译运行。就会出现一次线程不同步问题。不明白哪里没有处理好

------解决方案--------------------------------------------------------
探讨

引用:
你可以循环测一下,ReleaseMutex和WaitforSingleObject的耗时是临界区函数的100倍左右


就是用临界区 的同步方式吧。?
  相关解决方案