Jump to content
  • Advertisement
Sign in to follow this  
elih1

boost threads

This topic is 3557 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello I am making two boost threads in my main thread ,like this: boost::thread t1(SimWorkMatlab); boost::thread t2(GetMatlab); The functions SimWorkMatlab and GetMatlab are placed in one dll. The problem is that the threads are not executed in parallel. The first thread SimWorkMatlab is executed always first and when finished the the second thread starts. I am doing the testing with printing messages in log file and I am quite sure that the first thread is executed always first before the second thread even starts with execution. I was expecting that the threads will execute in parallel. What could be the problem? Thanks

Share this post


Link to post
Share on other sites
Advertisement
We would need to know more about what the threads are doing. For example, incorrect use of locks could cause this behaviour.

Share this post


Link to post
Share on other sites
the threads are not using locks or mutexes,nothing.
The threads are independent with each other.

Share this post


Link to post
Share on other sites
Still need to know more about what the threads are doing (i.e. the code), as the usage from that small snippet is correct for concurrency. If the code in the first thread executes quickly enough it could be complete before the second thread is even created. If this is the case your could test it by putting a thread::sleep() call in the first thread, causing it to yield time to the other threads before it continues.

/edit
Also if you are calling any library functions (any library that you do not have source access to, the c++ libs for instance) then your threads are not necessarily independant of each other.

Share this post


Link to post
Share on other sites
Yes I am doing that
"Also if you are calling any library functions (any library that you do not have source access to, the c++ libs for instance) then your threads are not necessarily independant of each other."
But I can't understand what you mean with the threads are not independent of each other

Share this post


Link to post
Share on other sites
The problem is likely in what you (or your libraries) are doing in the threads, and nothing to do with the threads themselves.

It is possible that the external library is thread-aware and will exclude all but one thread from executing portions of code for safety.

Read your post again and this time pretend you can't look at the source of your application. I want you to see how hard it is to give concrete clues/advice when there is so little information about what you are doing. We are not psychic, we need you to post the information only you have access to.

For a discussion on how to get better answers, read up on how to ask smart questions. [smile]

Share this post


Link to post
Share on other sites
If you want to take advantage of cores and actually run two pieces of code in parallel, you need processes, not threads. Threads don't actually run in parallel, the OS sort of multiplexes between them at unspecified times; so there's no speed gain in using them over running, say, GetMatlab first and SimWorkMatlab second.

Share this post


Link to post
Share on other sites
Quote:
Original post by Jotaf
If you want to take advantage of cores and actually run two pieces of code in parallel, you need processes, not threads. Threads don't actually run in parallel, the OS sort of multiplexes between them at unspecified times; so there's no speed gain in using them over running, say, GetMatlab first and SimWorkMatlab second.


Lies.

Well, you are correct that threads can be time sliced on a single core, but then so will processes. And on multiple cores different tasks (task: thread or process) will still be time-sliced as long as the number of tasks is less than the number of cores.

While it is Operating System dependant, most OSs will happily schedule threads from a single process on different cores. For example, in Linux it is my understanding that at the kernel level a thread and a process are very similar data structures.

The main difference is that when a process spawns a task as a thread it will share memory (and other things I am sure) by default, whereas a spawned process will get a copy-on-write copy of the parent's memory at the time of spawning. The same data structures in the kernel are used to deal with both.

I can't say how it is handled internally by Windows, but I have written multi-threaded programs and the threads have taken advantage of all cores and do indeed run in parallel even on a single core.

Share this post


Link to post
Share on other sites
Quote:
Original post by Jotaf
If you want to take advantage of cores and actually run two pieces of code in parallel, you need processes, not threads. Threads don't actually run in parallel, the OS sort of multiplexes between them at unspecified times; so there's no speed gain in using them over running, say, GetMatlab first and SimWorkMatlab second.

That's obviously totally incorrect, as rip-off mentioned.

Seriously, please refrain from posting anything you don't have verified and aren't sure about its validity. Posting such blatantly incorrect advice is only going to create more confusion for the already pretty confused OP.

elih: how did you come to the conclusion that your threads are running sequentially ? What is the code you used to determine that ?

Share this post


Link to post
Share on other sites
Thanks everyone
I was trying to provide as much as possible information as I can when I was asking the question but I couldn't imagine what the problem can be at all.

I think that I found my solution. Actually I am using MATLAB compiled c++ dll(that is compiled .m functions in c++ dll), and I have two functions in the MATLAB compiled c++ dll that need to be run by different thread. But after some searching I found that it is not permited by Matlab to execute the functions from C++ parallel from different threads. Only one thread can enter the MCR (The Matlab Component Runtime ,which need to be installed for the whole thing to work)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!