Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tiny c compiler cannot run in multi-threads,how to solve it? #1

Open
sunnyinchina opened this issue Nov 26, 2023 · 1 comment
Open

Comments

@sunnyinchina
Copy link

tiny c compiler cannot run in multi-threads,how to solve it?

@sunnyinchina
Copy link
Author

示例代码如下,程序会崩溃,报0xC0000005

#define NUM_THREADS 10

void compile_thread(char *filename)
{
TCCState *s = tcc_new();
if (!s)
{
fprintf(stderr, "Could not create TCC state\n");
exit(1);
}

tcc_set_output_type(s, TCC_OUTPUT_EXE);

if (tcc_compile_string(s, "int main() { return 0; }") == -1)
{
fprintf(stderr, "Failed to compile code\n");
exit(1);
}
tcc_delete(s);
}

DWORD WINAPI compile_worker(LPVOID arg)
{
char filename[100];
sprintf(filename, "output_%d.c", (int)arg);
compile_thread(filename);
return 0;
}

int _tmain(int argc, char* argv[])
{
/运行代码/
HANDLE hThread[NUM_THREADS];
for (int i = 0; i < NUM_THREADS; i++)
{
hThread[i] = CreateThread(NULL, 0, compile_worker, (LPVOID)i, 0, NULL);
}

for (int i = 0; i < NUM_THREADS; i++)
{
WaitForSingleObject(hThread[i], INFINITE);
CloseHandle(hThread[i]);

printf("线程%d结束\n", i + 1);

}

getchar();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant