Anonymous
Почему sem_post() выглядит заблокированным при использовании WinAPI и Semaphore для создания программы, которая запускае
Сообщение
Anonymous » 28 мар 2024, 12:05
Код: Выделить всё
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
void ErrorExit(LPTSTR lpszFunction)
{
// Retrieve the system error message for the last-error code
LPVOID lpMsgBuf;
LPVOID lpDisplayBuf;
DWORD dw = GetLastError();
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0, NULL );
// Display the error message and exit the process
lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
(lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)lpszFunction) + 40) * sizeof(TCHAR));
StringCchPrintf((LPTSTR)lpDisplayBuf,
LocalSize(lpDisplayBuf) / sizeof(TCHAR),
TEXT("%s failed with error %d: %s"),
lpszFunction, dw, lpMsgBuf);
MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK);
LocalFree(lpMsgBuf);
LocalFree(lpDisplayBuf);
ExitProcess(dw);
}
typedef struct {
sem_t edited;
sem_t sus;
sem_t running;
} sem_res_sus;
sem_res_sus sem_rs;
int checked = 0;
int isSuspend = 0;
int rollbackPoint = -1;
// producer
DWORD WINAPI thread(LPVOID arg) {
// edited isSuspend to 1 then can running and then suspended by the process
sem_wait(&sem_rs.edited);
sem_post(&sem_rs.sus);
while(isSuspend);
sem_post(&sem_rs.running);
std::cout
Подробнее здесь: [url]https://stackoverflow.com/questions/78236716/why-sem-post-looks-like-blocked-when-using-winapi-and-semaphore-to-create-a-pr[/url]
1711616738
Anonymous
[code]#include #include #include #include #include #include #include #include #include #include void ErrorExit(LPTSTR lpszFunction) { // Retrieve the system error message for the last-error code LPVOID lpMsgBuf; LPVOID lpDisplayBuf; DWORD dw = GetLastError(); FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dw, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &lpMsgBuf, 0, NULL ); // Display the error message and exit the process lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT, (lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)lpszFunction) + 40) * sizeof(TCHAR)); StringCchPrintf((LPTSTR)lpDisplayBuf, LocalSize(lpDisplayBuf) / sizeof(TCHAR), TEXT("%s failed with error %d: %s"), lpszFunction, dw, lpMsgBuf); MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK); LocalFree(lpMsgBuf); LocalFree(lpDisplayBuf); ExitProcess(dw); } typedef struct { sem_t edited; sem_t sus; sem_t running; } sem_res_sus; sem_res_sus sem_rs; int checked = 0; int isSuspend = 0; int rollbackPoint = -1; // producer DWORD WINAPI thread(LPVOID arg) { // edited isSuspend to 1 then can running and then suspended by the process sem_wait(&sem_rs.edited); sem_post(&sem_rs.sus); while(isSuspend); sem_post(&sem_rs.running); std::cout Подробнее здесь: [url]https://stackoverflow.com/questions/78236716/why-sem-post-looks-like-blocked-when-using-winapi-and-semaphore-to-create-a-pr[/url]