|
That should be the way is works. Each thread should acquire the Mutex (i.e. lock it) in order to protect the data. When they finish the critical section they should Release it. The point being that you only want one thread through this path at any one time.
|
|
|
|
|
Sorry for the delayed response but I have been very busy at work
But can Multiple threads wait on the Mutex with the WaitForSingleObject
And then which is released with the ReleaseMutex the one waiting the longest
|
|
|
|
|
I'm not sure, you would need to run some tests to find out. But in a properly designed application it should not matter, mutexes are not supposed to be FIFO queues.
|
|
|
|
|
|
In the Microsoft example there are 2 threads so ..
1) thread gains ownership of the signaled object and the second waits
In may case they are 4 so If thread 1 gains ownership do threads 2 - 4 waits
if so When the ReleaseMutex are all released or only one at a time
if so Which one
|
|
|
|
|
ForNow wrote: do threads 2 - 4 waits
Yes.
ForNow wrote: When the ReleaseMutex are all released or only one at a time
It depends on the scheduler. Thread scheduling is a very interesting topic in OS design, and the rules can vary from platform. But a general rule is that the waiting thread with the highest priority runs. However some OSs throw in a bit of priority inversion on occasion to free up any deadlocks.
Within the same priority group you might decide on say, 'last thread run', or 'first thread waiting' to decide which to activate and give the CPU to. A bit like FIFO, LRU LIFO etc, there are a lot of designs.
The important point though is that the OS does this for you so no need to worry about it. Use the example from Microsoft and all will be well.
|
|
|
|
|
If all the threads are released it is a problem
|
|
|
|
|
they arent, so you are OK.
|
|
|
|
|
i need help for this project. I would glad if someone could help me with my concern.
Create in C/C++ client and server application for UNIX operating system using BSD
sockets (located at eva.fit.vutbr.cz or merlin.fit.vutbr.cz) which will provide directory
service (small LDAP) .
• Final programs should be compiled into filename called client and
server.
• Create makefile which can be used for automatic compilation of source
files by typing make statement in UNIX shell.
• All errors should be printed on stderr.
• Directory database will be presented as text file (CSV format) with
following syntax:
• “id,login,surname,firstname,faculty” . Each entry (line) is terminated by
\n character.
|
|
|
|
|
We will not do your homework.
|
|
|
|
|
|
We do not do homework, assignment help, because homework is assigned to check how much you have learnt and where you lag in programming.
All of those steps can be also easily answered if you just try to find them on Google.
The sh*t I complain about
It's like there ain't a cloud in the sky and it's raining out - Eminem
~! Firewall !~
|
|
|
|
|
We do not do your HomeWork.
HomeWork is not set to test your skills at begging other people to do your work, it is set to make you think and to help your teacher to check your understanding of the courses you have taken and also the problems you have at applying them.
Any failure of you will help your teacher spot your weaknesses and set remedial actions.
So, give it a try, reread your lessons and start working. If you are stuck on a specific problem, show your code and explain this exact problem, we might help.
Patrice
“Everything should be made as simple as possible, but no simpler.” Albert Einstein
|
|
|
|
|
Is there someon that knows a good tutorial for a 2d game in c++.
I searched for manny tutorials vut didnt find a good one.
|
|
|
|
|
In any case, game development in C++ will be tough, and if you are a beginners don't even bother.
A good one, if you mean the one that builds everything for you, won't be available easily. You need to learn a lot of things them merge them. JavaScript, C# etc. have engines that can supports 2D game development easily and in simple code as compared to C++. So, look into them.
2D breakout game using pure JavaScript - Game development | MDN[^]
Unity - 2D Game Development Walkthrough[^]
The sh*t I complain about
It's like there ain't a cloud in the sky and it's raining out - Eminem
~! Firewall !~
|
|
|
|
|
I learned the basics od c++ and java but i think you can make beter games in c++ . Just ned a tutorial for hwo. To make some games with c++
|
|
|
|
|
If you are a beginner then Java or C++ doesn't make sense or difference. You can develop performance-ready games in Java too. For that, I would recommend having a good understanding of many things, there are books available; tutorial or article won't be enough.
The sh*t I complain about
It's like there ain't a cloud in the sky and it's raining out - Eminem
~! Firewall !~
|
|
|
|
|
We have articles on this topic: 2d game[^].
|
|
|
|
|
<pre lang="C++ MFC">I have a question for making use of multiple cores within a single thread. The scenario is like this, I need to do an intensive analysis behind the scene, so I create a separate thread to do this. As I want this thread to use multiple cores if they are available, OpenMP is used within this thread. Does this thread actually use multiple cores available? I guess it does, then my next question is how to kill this thread safely? i.e. anything needs to take care of compare with single core case? Why I am asking this question is that the killing process crashes the application when nultiple cores are used</pre>
|
|
|
|
|
TQTL wrote: the killing process crashes the application when nultiple cores are used Sorry, but it is impossible to view your code from out here.
|
|
|
|
|
The rule is: maximum of 1 core per thread.
You have to design your program for multi-threading, then launch as many thread as you want, they will spread across cores until you reach the number of cores, after that, some threads will share the same core.
Patrice
“Everything should be made as simple as possible, but no simpler.” Albert Einstein
|
|
|
|
|
Hi,
I countered the unwarranted downvote. The question is not low quality.
Have a look at Parallel Programming in Visual C++[^] and make sure you read everything.
Some thoughts:
1.) Yes, these parallelization libraries take advantage of multiple cores.
2.) Some of the libraries may be using Fibers[^] under the hood.
3.) Each library has a different technique for breaking out of the parallelized algorithm. For example... the concurrency namespace[^] utilizes a cancellation_token[^] while the OpenMP library uses the #pragma omp cancel[^]
Your Crash:
If you are experiencing a crash while using the Microsoft implementation of OpenMP... its probably because the OpenMP cancel directive is not supported. Microsoft only supports a partial subset of the directives[^]. As you can see... cancel is not in the list of supported directives.
If you are *not* using the Microsoft implementation of OpenMP then you probably need to #define OMP_CANCELLATION as the cancel directive is typically disabled for performance reasons.
Best Wishes,
-David Delaune
P.S.
If you want to convert your code from OpenMP to the Microsoft concurrency runtime[^] then here is an example.
How to: Convert an OpenMP Loop that Uses Cancellation to Use the Concurrency Runtime[^]
|
|
|
|
|
TQTL wrote: ...how to kill this thread safely? With few exceptions, this is generally a bad idea. A thread can internally kill itself, but that is a different question.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
Just ask OpenMP how many processors it is using
int omp_get_num_procs();
If you want the answer from windows
SYSTEM_INFO sysinfo;
GetSystemInfo(&sysinfo);
int countCPU = sysinfo.dwNumberOfProcessors;
If you are expecting it to make MFC itself quicker it won't by very much at all unless it's just particular things like graphics or file access that is slowing you down.
To explain why we need to look at MFC which is an event drive system, meaning it fetches messages and dispatches messages that is it's basic behaviour. Part of that behaviour is it needs the events to process in the order they are in the message queue.
For example clicking on the X close button on the top of an application fires off 3 messages
WM_CLOSE
WM_DESTROY
WM_QUIT
Those messages must be processed in that order and the next message can not be started to process until the one before it is completed. So the required ordering of processing of the event queue trumps any multiprocessor threading you want to put on the message queue. Even things like WM_PAINT messages are in order you must paint the windows in that order. It's pretty safe to say most things in the message queue except probably things like WM_MOUSEMOVE are going to have a required order to them and multithreaded or not you can't process the next message without the previous completing. So you can't really speed the MFC message framework up.
The things you can speed up is the single processing of a single message. So in response to a WM_XXXXXXX message you may spawn threads to achieve the required task that is to be executed on that message in a faster time. If what you are doing is safe to windows it is safe to MFC because your threads will all end at exactly the same point as a single threaded program and you will either send back a LRESULT, Send/Post off a message or do some action and MFC would be unaware of the threads.
So basically if you multithread within a single message process or within an action or process that is called you can do it safely from MFC point of view and in fact MFC would be oblivious to the threading.
If you are having problems and crashing you are either not containing yourself to those guidelines and creating order problems for MFC or alternatively what you are doing is just broken on windows and the problem has nothing to do with MFC.
You can split an join threads just using C++11, make a windows console application and paste this code in to see that and mutexing stuff to control. Your OpenMP etc should give better performance but it makes it easy to prototype something.
#include <iostream>
#include <thread>
#include <mutex>
static const int num_threads = 10;
std::mutex mtx;
void call_from_thread(int tid) {
std::cout << "Launched by thread " << tid << std::endl;
}
void call_from_thread_with_mutex (int tid) {
mtx.lock();
std::cout << "Launched by thread " << tid << std::endl;
mtx.unlock();
}
int main() {
std::thread t[num_threads];
for (int i = 0; i < num_threads; ++i) {
t[i] = std::thread(call_from_thread, i);
}
std::cout << "Non mutex threads launched from the main\n";
for (int i = 0; i < num_threads; ++i) {
t[i].join();
}
for (int i = 0; i < num_threads; ++i) {
t[i] = std::thread(call_from_thread_with_mutex, i);
}
mtx.lock();
std::cout << "Mutex threads launched from the main\n";
mtx.unlock();
for (int i = 0; i < num_threads; ++i) {
t[i].join();
}
getchar();
return 0;
}
In vino veritas
modified 23-Nov-16 2:01am.
|
|
|
|
|
Hello All,
Please help out me.
I have enable Arrow Key functionality in MFC application,Now some where i required that if i clicked the checkbox then after Arrow key should not be enable.
Now i required the condition need to be in .rc file not in .cpp file .
I tried " #if(false) #endif " but how i need to provide condition inside into IF() and how it will work at runtime when i will change the condition.
modified 22-Nov-16 5:56am.
|
|
|
|