小编xpn*_*toc的帖子

多线程C应用程序中的访问冲突

我正在尝试编写一个多线程的简单C应用程序.我希望主线程被挂起,直到工作线程设置了一些标志.所以对于我的线程函数参数,我传递一个包含该标志的结构.当我在工作线程中分配标志时,我得到访问冲突.我正在使用Mutex理论上阻止同时访问主应用程序和工作线程之间共享的此结构实例.有人能指出我正确的方向吗?完整的项目代码如下.我在THREADFUNCS.C文件的注释中表示了错误行.

GLOBALS.H

#ifndef _globals_h
#define _globals_h

#include <windows.h>


static HANDLE ghMutex;


#endif
Run Code Online (Sandbox Code Playgroud)

THREADCOM.H

#ifndef _threadcom_h
#define _threadcom_h

typedef struct { 
   char bContinueMain;
} RComData;

#endif
Run Code Online (Sandbox Code Playgroud)

THREADFUNCS.H

#ifndef _threadfuncs_h
#define _threadfuncs_h 

#include <windows.h>

extern DWORD WINAPI ThreadA(LPVOID params);

#endif
Run Code Online (Sandbox Code Playgroud)

THREADFUNCS.C

#include <stdio.h>
#include "threadcom.h"
#include "threadfuncs.h"
#include "globals.h"

DWORD WINAPI ThreadA(LPVOID params)
{
   RComData* pr = (RComData*)params;
   int i;

   printf("You are in thread A.\n");
   WaitForSingleObject(ghMutex, INFINITE);
   pr->bContinueMain = TRUE; /* ACCESS VIOLATION HERE */
   ReleaseMutex(ghMutex);
   for (i=0; i<10; ++i)
   {
      printf("Printing …
Run Code Online (Sandbox Code Playgroud)

c winapi multithreading mutex access-violation

2
推荐指数
1
解决办法
2093
查看次数

标签 统计

access-violation ×1

c ×1

multithreading ×1

mutex ×1

winapi ×1