| |

VerySource

 Forgot password?
 Register
Search
View: 668|Reply: 8

Multi-threading problem

[Copy link]

1

Threads

5

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 United States

Post time: 2020-3-19 10:00:02
| Show all posts |Read mode
#include "stdafx.h"
#include <windows.h>
#include <iostream.h>
volatile int index = 0;
DWORD WINAPI Fun1Proc (LPVOID lpParameter);
int main (int argc, char * argv [])
{
Ranch
HANDLE hThread1;
hThread1 = CreateThread (NULL, 0, Fun1Proc, NULL, 0, NULL);
CloseHandle (hThread1);
while (index ++ <1000)
{
cout << "Main thread is runing" << endl;
}


return 0;
}

DWORD WINAPI Fun1Proc (LPVOID lpParameter)
{
while (index ++ <1000)
{
cout << "Thread1 is running" << endl;
}
return 0;
}

How does this program run wrong, read wrong memory
Reply

Use magic Report

0

Threads

22

Posts

18.00

Credits

Newbie

Rank: 1

Credits
18.00

 Invalid IP Address

Post time: 2020-6-23 15:00:02
| Show all posts
volatile change to static
Reply

Use magic Report

0

Threads

22

Posts

18.00

Credits

Newbie

Rank: 1

Credits
18.00

 Invalid IP Address

Post time: 2020-6-23 17:45:01
| Show all posts
HANDLE hThread1;
hThread1=CreateThread(NULL,0,Fun1Proc,NULL,0,NULL);
CloseHandle(hThread1);
WaitForSingleObject(hThread1,INFINITE);
Reply

Use magic Report

1

Threads

5

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-6-29 15:30:01
| Show all posts
What is the reason?
Reply

Use magic Report

0

Threads

22

Posts

18.00

Credits

Newbie

Rank: 1

Credits
18.00

 Invalid IP Address

Post time: 2020-6-29 22:00:02
| Show all posts
ok
1. The main thread and the sub-thread simultaneously read and write the index, which is a race condition, a very wrong way of writing, but it is not the main reason for the error
2. I personally think that the purpose of the loop in the main thread is just to block the main thread without directly exiting. This way of writing is even more wrong.
3. When the loop of the main thread ends, the main thread is going to return, your child thread may not have exited, you may still have to access the index, and at this time the main thread is releasing various variables and resources, memory read errors may occur

The purpose of using waitforsingleobject is to replace the role of the main thread loop. He will always wait for the child thread to launch before returning. index has only one thread to read and write at this time, so it is correct

Your code's use and way of threading is really a big deal
Reply

Use magic Report

0

Threads

22

Posts

18.00

Credits

Newbie

Rank: 1

Credits
18.00

 Invalid IP Address

Post time: 2020-6-29 23:30:01
| Show all posts
HANDLE hThread1;
hThread1=CreateThread(NULL,0,Fun1Proc,NULL,0,NULL);
WaitForSingleObject(hThread1,INFINITE);
CloseHandle(hThread1);
Reply

Use magic Report

1

Threads

5

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-6-30 09:45:01
| Show all posts
Thank you very much, just learned multithreading
Reply

Use magic Report

1

Threads

5

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-6-30 10:45:01
| Show all posts
#include "stdafx.h"
#include <windows.h>
#include <iostream.h>
volatile static int index=0;
DWORD WINAPI Fun1Proc(LPVOID lpParameter);
int main(int argc, char* argv[])
{
The
HANDLE hThread1;
hThread1=CreateThread(NULL,0,Fun1Proc,NULL,0,NULL);
CloseHandle(hThread1);
WaitForSingleObject(hThread1,INFINITE);
while(index++<1000)
{
cout<<"Main thread is runing"<<endl;
}


return 0;
}

DWORD WINAPI Fun1Proc(LPVOID lpParameter)
{
while(index++<1000)
{
cout<<"Thread1 is running"<<endl;
}
return 0;
}
It is still wrong to change to the above.
But I can’t lose anything by changing to the following

// MultThread.cpp: Defines the entry point for the console application.
//

#include "stdafx.h"
#include <windows.h>
#include <iostream.h>
volatile static int index=0;
DWORD WINAPI Fun1Proc(LPVOID lpParameter);
int main(int argc, char* argv[])
{
The
HANDLE hThread1;
hThread1=CreateThread(NULL,0,Fun1Proc,NULL,0,NULL);
CloseHandle(hThread1);
WaitForSingleObject(hThread1,INFINITE);
/* while(index++<1000)
{
cout<<"Main thread is runing"<<endl;
}
*/

return 0;
}

DWORD WINAPI Fun1Proc(LPVOID lpParameter)
{
while(index++<1000)
{
cout<<"Thread1 is running"<<endl;
}
return 0;
}
Multi-threaded
Reply

Use magic Report

1

Threads

5

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-6-30 11:45:02
| Show all posts
I was wrong. . Sorry.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list