-5

let me appologize in advance for being so vague about the problem. I can not share any details. It is a computer vision problem and a bunch of images are scanned and processed in main(). Every 100 (or so) frames the main() function spawns another thread, which scans the images in a different way (but the first step was requires).

#include <thread>
...

void myFunction(ImageVector, arg2, arg3)
{
    for (i = 0; i < ImageVector.size < ++i)
    {
        process(ImageVector[i])
    }
}

int main(void)
{

std::vector<std::string> images; // paths to the images, please consider it filled somehow, e.g. multiple push_backs



for (i = 0; i < Images.size ; ++i)
     {
         Scan(Images[i]). // Scans the image
         Process(Images[i]) // Processes the image

         if (Something)
         {
            std::vector<std::string> ImageVector(&Images[10], &Images[100]);

            //Spawn the tread and put it into a vector.
            MyThreads.push_back(std::thread(myFunction, ImageVector, arg2, arg3));
         }

     }

     MyThreads[0].join(); // assuming only the first one is filled

}

The images are not the same and read from disk. Nevertheless I checked the reading logic and that is not the problem. If I execute the tasks after another, it works fine. Neither ImageVector, arg2, arg3 are pointers. They are just copies of variables.

Functions myFunction(), Scan(), Process() use similar code-pieces but none of these pieces has shared variables in them. There is also no malloc() as I was told this is not threadsafe, just "new" now.

I realize it is not much to go on but maybe I just forgot something simple.

Thank you

PS: I switched the threading library to boost with no effect,´.

pAt84
  • 213
  • 3
  • 10
  • 3
    This doesn't even compile. Don't post bogus code to get help. – πάντα ῥεῖ Jan 31 '16 at 14:36
  • 2
    ' There is also no malloc() as I was told this is not threadsafe,' - who told you that? Though it's to be avoided if possible in C++, being threadsafe is not one of its issues. – Martin James Jan 31 '16 at 14:41
  • @MartinJames: I just read it here on stackexchange, can't find it again though. – pAt84 Jan 31 '16 at 14:47
  • @πάνταῥεῖ : Please read the thread before you complain. I can't release more details than this. I am hoping I made a simple mistake, having a loop in the main() and starting a thread from this loop for example. Given that I am new to threads I thought others could see this or have experienced similar issues. So please, if you have nothing to contribute, please don't post. Again, I am sorry for the limited detail I can provide. – pAt84 Jan 31 '16 at 14:50
  • What is actually the problem? And how do you expect someone to help you if they can't compile your code? – RobClucas Jan 31 '16 at 14:52
  • @user3231315 I've been reading it. Anyhow it's your responsibility to make a [MCVE] when asking. – πάντα ῥεῖ Jan 31 '16 at 14:52
  • 'So please, if you have nothing to contribute, please don't post' - OK, so, a down and close vote, then? I can help with that. – Martin James Jan 31 '16 at 14:58
  • 3
    I'm sorry, but you cannot expect the skilled and experienced developers on SO to spend time staring at chunks of incomplete code, expecially multithreaded code, in some vague hope that they might spot something. You have ALL the code, environment, compiler, debugger logger etc. and you post some incomplete snippets? Well, that's not very helpful to anyone:( – Martin James Jan 31 '16 at 15:02
  • @nabla If the main thread and the myFunction-thread run in parallel, they produce wrong results. I thought there could be a simple problem in here as I have debugged every line of code and logged the output of almost all data structures as well and found no mistake, As I said before, I can't release the code. Even if I would do so, it is a large project and much of it advanced computer vision and machine learning. – pAt84 Jan 31 '16 at 15:05
  • @MartinJames: It is the best I can provide and posts that state the obvious are useless -- it is fairly clear that it does not compile. If you have a problem with that, vote it down and close it. Seems like stackoverflow has lost its spirit. – pAt84 Jan 31 '16 at 15:10
  • 1
    @user3231315 _"Seems like stackoverflow has lost its spirit."_ No, we're the ones keeping it up. Stack Overflow is mean't to be helpful for future research about concrete, clearly stated programming problems. – πάντα ῥεῖ Jan 31 '16 at 15:16
  • What if the problem lies within the process function? Or the scan function? Or the fact that main calls scan and the myFunction function doesn't? Or that in main process takes an Image and in myFunction process takes an Image vector? Your problem could literally be anything, would you like people to guess suggestions? – RobClucas Jan 31 '16 at 15:28
  • "Seems like stackoverflow has lost its spirit." A phrase that could only be uttered by a newcomer. If this had been asked in 2009 it would've been closed in a fraction of the time. – Voo Jan 31 '16 at 15:30
  • @nable: The the obvious answer is that it can't be answered with this little information (much better than "this doesn't even compile, or "bogus code". I did my best to check theses functions. As I said there are no shared variables or so. In fact they could be independet programs to begin with, Is there anything else I should look out for? I specifically asked if I may have overlooked something simple. Obviously I don't expect anyone to come up with code for the non-defined functions. Even more obviously, it seems it is easier to just flame and not help at all. – pAt84 Jan 31 '16 at 15:39
  • [sigh] 'As I said there are no shared variables or so. In fact they could be independent programs to begin with,' - prove it. Your word is not good enough because you may have made a mistake. Please understand that writing code is easy, (yes, it is). Designing the data is much harder. TESTING AND DEBUGGING IS VERY HARD INDEED and, often, every possible tool and item of information is required to fix problems. ATM, the only one with all the information is you, so you have to do the debugging. It's that simple. – Martin James Jan 31 '16 at 22:44
  • ' I thought there could be a simple problem in here' - it may well be, but you are hiding the simple problem from us. – Martin James Jan 31 '16 at 22:47

1 Answers1

0

If a program works with no threading and not working with threading then likely reason could be the synchronization. In this specific case, with limited problem description and code snippet available, may be sync issue when accessing ImageVector.

Umamahesh P
  • 1,224
  • 10
  • 14
  • 1
    An answer as vague as the question. Very nice. – πάντα ῥεῖ Jan 31 '16 at 15:17
  • Thanks Umamahesh P. I forgot to mention, and I could have put this into the code. ImageVector is a part of Images. I updated the code. – pAt84 Jan 31 '16 at 15:35
  • Could you please provide little more details on what is not working? For example, you are getting memory exception or an appl error etc. Other easy option would be to add debugging statements appending to a file file using "write" system call to better understand the flow when threads are used. That would be easy to find the cause. – Umamahesh P Feb 01 '16 at 01:55
  • Hi, the problem is that when both threads (the main thread and the myFunction) run in parallel they produce wrongful results. If I execute myFunction() directly after Scan() and Process(). So sequential this is fine (I think). There are not exception or apple errors, nothing that indicates something went wrong. I walked through the program with logfiles in both threads. Everything looked fine. Obviously apart from the functions that get two different images and therefore produce different results but most likely the problem lies in there. – pAt84 Feb 01 '16 at 07:45
  • Hi, I suspect the problem could be because of sharing the Images array. The ImageVector is created using the address of the Images array. That is the only thing I can spot from the sample provided. Thanks,mahesh – Umamahesh P Feb 01 '16 at 08:46